Skip to content

Commit fd26660

Browse files
committed
bug #1944 [Chart.js] Listen to Stimulus disconnect event to destroy the chart (Shadow-Devil)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Chart.js] Listen to Stimulus `disconnect` event to destroy the chart | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #1408 | License | MIT Adds an extra check if the chart is already connected and short circuits, since otherwise creating the new Chart would throw an error. Commits ------- 22d1cee [Chart.js] Listen to Stimulus `disconnect` event to destroy the chart
2 parents 507c89c + 22d1cee commit fd26660

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
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.23.0
4+
5+
- Listen to Stimulus `disconnect` event to destroy the chart #1944
6+
37
## 2.18.0
48

59
- Replace `chartjs/auto` import with explicit `Chart.register()` call #1263

src/Chartjs/assets/dist/controller.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default class extends Controller {
66
};
77
private chart;
88
connect(): void;
9+
disconnect(): void;
910
viewValueChanged(): void;
1011
private dispatchEvent;
1112
}

src/Chartjs/assets/dist/controller.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class default_1 extends Controller {
3535
this.chart = new Chart(canvasContext, payload);
3636
this.dispatchEvent('connect', { chart: this.chart });
3737
}
38+
disconnect() {
39+
this.dispatchEvent('disconnect', { chart: this.chart });
40+
if (this.chart) {
41+
this.chart.destroy();
42+
this.chart = null;
43+
}
44+
}
3845
viewValueChanged() {
3946
if (this.chart) {
4047
const viewValue = { data: this.viewValue.data, options: this.viewValue.options };

src/Chartjs/assets/src/controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export default class extends Controller {
5757
this.dispatchEvent('connect', { chart: this.chart });
5858
}
5959

60+
disconnect() {
61+
this.dispatchEvent('disconnect', { chart: this.chart });
62+
63+
if (this.chart) {
64+
this.chart.destroy();
65+
this.chart = null;
66+
}
67+
}
68+
6069
/**
6170
* If the underlying data or options change, let's update the chart!
6271
*/

0 commit comments

Comments
 (0)