1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2026-08-04 23:09:17 +08:00

#139 fix deselection in multi-trace charts

This commit is contained in:
lana-k
2026-07-26 20:34:08 +02:00
parent acea6090e8
commit 529c3390b3
3 changed files with 34 additions and 7 deletions

View File

@@ -105,10 +105,10 @@ export default {
}, },
watch: { watch: {
dataSources() { dataSources() {
chartHelper.clearSelection(this.state.data, this.state.layout)
// we need to update state.data in order to update the graph // we need to update state.data in order to update the graph
// https://github.com/plotly/react-chart-editor/issues/948 // https://github.com/plotly/react-chart-editor/issues/948
if (this.dataSources) { if (this.dataSources) {
chartHelper.clearSelection(this.state.data, this.state.layout)
dereference.default(this.state.data, this.dataSources) dereference.default(this.state.data, this.dataSources)
this.updatePlotly() this.updatePlotly()
} }
@@ -152,14 +152,11 @@ export default {
this.$refs.plotlyEditor?.$el?.querySelector('.js-plotly-plot') this.$refs.plotlyEditor?.$el?.querySelector('.js-plotly-plot')
plotlyDiv?.on('plotly_selected', selectionEvent => { plotlyDiv?.on('plotly_selected', selectionEvent => {
if (selectionEvent) { if (selectionEvent) {
this.selectedItems = chartHelper.getRowsByIndexFromDataSources( this.selectedItems = this.getSelectedData()
this.dataSources,
selectionEvent.points.map(point => point.pointIndex)
)
} }
}) })
plotlyDiv?.on('plotly_deselect', () => { plotlyDiv?.on('plotly_deselect', () => {
this.selectedItems = null this.selectedItems = this.getSelectedData()
}) })
}, },
activated() { activated() {
@@ -219,6 +216,12 @@ export default {
}, },
prepareCopy(type = 'png') { prepareCopy(type = 'png') {
return chartHelper.getImageDataUrl(this.$refs.plotlyEditor.$el, type) return chartHelper.getImageDataUrl(this.$refs.plotlyEditor.$el, type)
},
getSelectedData() {
return chartHelper.getRowsByIndexFromDataSources(
this.dataSources,
chartHelper.getSelectedPointsIndexes(this.state.data)
)
} }
} }
} }

View File

@@ -13,6 +13,16 @@ export function getOptionsFromDataSources(dataSources) {
})) }))
} }
export function getSelectedPointsIndexes(stateData) {
if (!stateData) {
return []
}
return Array.from(
new Set(stateData.flatMap(data => data.selectedpoints || []))
)
}
export function getRowsByIndexFromDataSources(dataSources, rowIndexes) { export function getRowsByIndexFromDataSources(dataSources, rowIndexes) {
if (!dataSources) { if (!dataSources) {
return [] return []
@@ -98,5 +108,6 @@ export default {
getHtml, getHtml,
getChartData, getChartData,
getRowsByIndexFromDataSources, getRowsByIndexFromDataSources,
clearSelection clearSelection,
getSelectedPointsIndexes
} }

View File

@@ -21,6 +21,19 @@ describe('chartHelper.js', () => {
]) ])
}) })
it('getSelectedPointsIndexes returns unique selected point indexes', () => {
const stateData = [
{ selectedpoints: [1, 2, 3] },
{ selectedpoints: [3, 4] },
{ selectedpoints: [] },
{}
]
const indexes = chartHelper.getSelectedPointsIndexes(stateData)
expect(indexes).to.eql([1, 2, 3, 4])
})
it('getOptionsForSave', () => { it('getOptionsForSave', () => {
const state = { const state = {
data: [ data: [