Skip to content

Commit 130f7dc

Browse files
IndraGunawankbond
authored andcommitted
[Chart.js] add view-value-change event
1 parent 554ebba commit 130f7dc

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

src/Chartjs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.17.0
4+
5+
- Add `chartjs:view-value-change` event #1605
6+
37
## 2.15.0
48

59
- Remove restriction that prevented Chart.js 3.9 #1518

src/Chartjs/assets/dist/controller.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ class default_1 extends Controller {
3434
}
3535
viewValueChanged() {
3636
if (this.chart) {
37-
this.chart.data = this.viewValue.data;
38-
this.chart.options = this.viewValue.options;
37+
const viewValue = { data: this.viewValue.data, options: this.viewValue.options };
38+
if (Array.isArray(viewValue.options) && 0 === viewValue.options.length) {
39+
viewValue.options = {};
40+
}
41+
this.dispatchEvent('view-value-change', viewValue);
42+
this.chart.data = viewValue.data;
43+
this.chart.options = viewValue.options;
3944
this.chart.update();
4045
const parentElement = this.element.parentElement;
4146
if (parentElement && this.chart.options.responsive) {

src/Chartjs/assets/src/controller.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ export default class extends Controller {
5959
*/
6060
viewValueChanged(): void {
6161
if (this.chart) {
62-
this.chart.data = this.viewValue.data;
63-
this.chart.options = this.viewValue.options;
62+
const viewValue = { data: this.viewValue.data, options: this.viewValue.options };
63+
if (Array.isArray(viewValue.options) && 0 === viewValue.options.length) {
64+
viewValue.options = {};
65+
}
66+
this.dispatchEvent('view-value-change', viewValue);
67+
this.chart.data = viewValue.data;
68+
this.chart.options = viewValue.options;
69+
6470
this.chart.update();
6571

6672
// Updating the chart seems to sometimes result in a chart that is

src/Chartjs/assets/test/controller.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const startChartTest = async (canvasHtml: string): Promise<{ canvas: HTMLCanvasE
4646
});
4747

4848
if (!chart) {
49-
throw 'Missing TomSelect instance';
49+
throw 'Missing ChartJS instance';
5050
}
5151

5252
return { canvas: canvasElement, chart };
@@ -105,6 +105,33 @@ describe('ChartjsController', () => {
105105
});
106106
});
107107

108+
it('will update when the view data changes without options', async () => {
109+
let viewValueChangeCallCount = 0;
110+
111+
document.body.addEventListener('chartjs:view-value-change', (event: any) => {
112+
viewValueChangeCallCount++;
113+
event.detail.options.showLines = true;
114+
});
115+
116+
const { chart, canvas } = await startChartTest(`
117+
<canvas
118+
data-testid='canvas'
119+
data-controller='check chartjs'
120+
data-chartjs-view-value="&#x7B;&quot;type&quot;&#x3A;&quot;line&quot;,&quot;data&quot;&#x3A;&#x7B;&quot;labels&quot;&#x3A;&#x5B;&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;&#x5D;,&quot;datasets&quot;&#x3A;&#x5B;&#x7B;&quot;label&quot;&#x3A;&quot;My&#x20;First&#x20;dataset&quot;,&quot;backgroundColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;borderColor&quot;&#x3A;&quot;rgb&#x28;255,&#x20;99,&#x20;132&#x29;&quot;,&quot;data&quot;&#x3A;&#x5B;0,10,5,2,20,30,45&#x5D;&#x7D;&#x5D;&#x7D;,&quot;options&quot;&#x3A;&#x5B;&#x5D;&#x7D;"
121+
></canvas>
122+
`);
123+
124+
expect(chart.options.showLines).toBeUndefined();
125+
// change label: January -> NewDataJanuary
126+
const currentViewValue = JSON.parse('{"type":"line","data":{"labels":["NewDataJanuary","February","March","April","May","June","July"],"datasets":[{"label":"My First dataset","backgroundColor":"rgb(255, 99, 132)","borderColor":"rgb(255, 99, 132)","data":[0,10,5,2,20,30,45]}]},"options":[]}');
127+
canvas.dataset.chartjsViewValue = JSON.stringify(currentViewValue);
128+
129+
await waitFor(() => {
130+
expect(chart.options.showLines).toBe(true);
131+
expect(viewValueChangeCallCount).toBe(1);
132+
});
133+
});
134+
108135
it('dispatches the events correctly', async () => {
109136
let preConnectCallCount = 0;
110137
let preConnectDetail: any = null;

0 commit comments

Comments
 (0)