Skip to content

Commit d4ef908

Browse files
authored
Store and reuse selection in between draws (#1243)
When the gchart redraws (either from resizing the window or the parent encompassing component redraws it), it drops the selected data points. This change fixes that.
1 parent 5da4821 commit d4ef908

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

frontend/src/static/js/components/test/webstatus-gchart.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ describe('webstatus-gchart', () => {
6363
'webstatus-gchart',
6464
) as WebstatusGChart;
6565

66+
component.currentSelection = [{row: 0, column: 1}];
67+
6668
await loaderComponent.updateComplete;
6769
await loaderComponent.waitForGChartsLibraryLoaded();
6870
await component.updateComplete;
@@ -77,8 +79,10 @@ describe('webstatus-gchart', () => {
7779
});
7880

7981
it('redraws the chart on resize', async () => {
80-
// Spy on the chartWrapper.draw method (make sure chartWrapper is initialized)
82+
// Spy on the chartWrapper.draw and setSelection methods (make sure chartWrapper is initialized)
8183
const drawSpy = sinon.spy(component.chartWrapper!, 'draw');
84+
const mockChart = sinon.createStubInstance(google.visualization.LineChart);
85+
sinon.stub(component.chartWrapper!, 'getChart').returns(mockChart);
8286

8387
// Simulate a resize
8488
const resizeObserverCallback = mockResizeObserver.args[0][0];
@@ -90,6 +94,7 @@ describe('webstatus-gchart', () => {
9094

9195
// Assert that chartWrapper.draw was called
9296
assert.isTrue(drawSpy.calledOnce);
97+
assert.isTrue(mockChart.setSelection.calledOnceWith([{row: 0, column: 1}]));
9398
});
9499

95100
describe('Selection events', () => {

frontend/src/static/js/components/webstatus-gchart.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export class WebstatusGChart extends LitElement {
9191
})
9292
hasMax = true;
9393

94+
// Selected data points on the chart.
95+
// If the chart ever re-draws due to resize or the encompassing component
96+
// re-drawing, we need to manually set the current selection.
97+
currentSelection: google.visualization.ChartSelection[] | undefined;
98+
9499
@property({state: true, type: Object})
95100
dataTable:
96101
| google.visualization.DataTable
@@ -129,13 +134,18 @@ export class WebstatusGChart extends LitElement {
129134

130135
private _resizeObserver: ResizeObserver | undefined;
131136

137+
draw() {
138+
if (this.chartWrapper) {
139+
this.chartWrapper.draw();
140+
this.chartWrapper?.getChart()?.setSelection(this.currentSelection);
141+
}
142+
}
143+
132144
firstUpdated() {
133145
// 1. Create the ResizeObserver
134146
this._resizeObserver = new ResizeObserver(() => {
135147
// 2. Redraw the chart when a resize occurs
136-
if (this.chartWrapper) {
137-
this.chartWrapper.draw();
138-
}
148+
this.draw();
139149
});
140150

141151
// 3. Start observing the chart container element
@@ -298,13 +308,17 @@ export class WebstatusGChart extends LitElement {
298308
);
299309
this._chartClickListenerAdded = true; // Set the flag after adding the listener
300310
}
301-
this.chartWrapper.draw();
311+
this.draw();
302312
}
303313
}
304314
private _handleChartClick() {
305315
const selection = this.chartWrapper?.getChart()?.getSelection();
306-
if (selection === undefined) return;
316+
if (selection === undefined) {
317+
this.currentSelection = undefined;
318+
return;
319+
}
307320
if (selection.length > 0) {
321+
this.currentSelection = selection;
308322
// TODO: For now only look at the first selection since we only configure for one selection at a time.
309323
const item = selection[0];
310324
const row = item.row;
@@ -332,6 +346,7 @@ export class WebstatusGChart extends LitElement {
332346
this.dispatchEvent(chartClickEvent);
333347
}
334348
} else if (selection.length === 0) {
349+
this.currentSelection = [];
335350
const chartDeselectEvent: ChartDeselectPointEvent = new CustomEvent(
336351
'point-deselected',
337352
{detail: undefined},

0 commit comments

Comments
 (0)