Skip to content

Commit 45cb362

Browse files
authored
fix(legacycharts): fix update for multiple reactive charts in one page (#818)
Fix bug updating multiple reactive charts on one page, except when using the chartjs-plugin-annotation and Vue 2 simultaneously.
1 parent 51170b5 commit 45cb362

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

legacy/src/Charts.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
ChartEmits
2323
} from '../../src/utils'
2424

25+
const ANNOTATION_PLUGIN_KEY = 'annotation'
26+
2527
export function generateChart(chartId, chartType, chartController) {
2628
let _chartRef = null
2729

@@ -64,6 +66,21 @@ export function generateChart(chartId, chartType, chartController) {
6466
default: () => []
6567
}
6668
},
69+
data() {
70+
return {
71+
_chart: null
72+
}
73+
},
74+
computed: {
75+
hasAnnotationPlugin() {
76+
return (
77+
Object.keys(this.chartOptions).length > 0 &&
78+
'plugins' in this.chartOptions &&
79+
Object.keys(this.chartOptions.plugins).length > 0 &&
80+
ANNOTATION_PLUGIN_KEY in this.chartOptions.plugins
81+
)
82+
}
83+
},
6784
created() {
6885
ChartJS.register(chartController)
6986
},
@@ -82,8 +99,10 @@ export function generateChart(chartId, chartType, chartController) {
8299
},
83100
methods: {
84101
renderChart(data, options) {
85-
if (_chartRef?.current !== null) {
86-
chartDestroy(_chartRef.current)
102+
const currentChart = this.getCurrentChart()
103+
104+
if (currentChart !== null) {
105+
chartDestroy(currentChart)
87106
this.$emit(ChartEmits.ChartDestroyed)
88107
}
89108

@@ -95,55 +114,68 @@ export function generateChart(chartId, chartType, chartController) {
95114
const canvasEl2DContext = this.$refs.canvas.getContext('2d')
96115

97116
if (canvasEl2DContext !== null) {
98-
_chartRef.current = new ChartJS(canvasEl2DContext, {
99-
type: chartType,
100-
data: chartData,
101-
options,
102-
plugins: this.plugins
103-
})
117+
this.setCurrentChart(
118+
new ChartJS(canvasEl2DContext, {
119+
type: chartType,
120+
data: chartData,
121+
options,
122+
plugins: this.plugins
123+
})
124+
)
104125
}
105126
}
106127
},
107128
chartDataHandler(newValue, oldValue) {
108129
const newData = { ...newValue }
109130
const oldData = { ...oldValue }
131+
const currentChart = this.getCurrentChart()
110132

111133
if (Object.keys(oldData).length > 0) {
112134
const isEqualLabelsAndDatasetsLength = compareData(newData, oldData)
113135

114-
if (isEqualLabelsAndDatasetsLength && _chartRef?.current !== null) {
115-
setChartDatasets(_chartRef.current.data, newData, this.datasetIdKey)
136+
if (isEqualLabelsAndDatasetsLength && currentChart !== null) {
137+
setChartDatasets(currentChart.data, newData, this.datasetIdKey)
116138

117139
if (newData.labels !== undefined) {
118-
setChartLabels(_chartRef.current, newData.labels)
140+
setChartLabels(currentChart, newData.labels)
119141
this.$emit(ChartEmits.LabelsUpdated)
120142
}
121143

122-
chartUpdate(_chartRef.current)
144+
chartUpdate(currentChart)
123145
this.$emit(ChartEmits.ChartUpdated)
124146
} else {
125-
if (_chartRef?.current !== null) {
126-
chartDestroy(_chartRef.current)
147+
if (currentChart !== null) {
148+
chartDestroy(currentChart)
127149
this.$emit(ChartEmits.ChartDestroyed)
128150
}
129151

130152
chartCreate(this.renderChart, this.chartData, this.chartOptions)
131153
this.$emit(ChartEmits.ChartRendered)
132154
}
133155
} else {
134-
if (_chartRef?.current !== null) {
135-
chartDestroy(_chartRef.current)
156+
if (currentChart !== null) {
157+
chartDestroy(currentChart)
136158
this.$emit(ChartEmits.ChartDestroyed)
137159
}
138160

139161
chartCreate(this.renderChart, this.chartData, this.chartOptions)
140162
this.$emit(ChartEmits.ChartRendered)
141163
}
164+
},
165+
getCurrentChart() {
166+
return this.hasAnnotationPlugin ? _chartRef.current : this.$data._chart
167+
},
168+
setCurrentChart(chart) {
169+
this.hasAnnotationPlugin
170+
? (_chartRef.current = chart)
171+
: (this.$data._chart = chart)
142172
}
143173
},
144174
beforeDestroy() {
145-
if (_chartRef?.current !== null) {
146-
chartDestroy(_chartRef.current)
175+
const currentChart = this.getCurrentChart()
176+
177+
if (currentChart !== null) {
178+
chartDestroy(currentChart)
147179
this.$emit(ChartEmits.ChartDestroyed)
148180
}
149181
},

website/src/guide/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ Charts will emit events if the data changes. You can listen to them in the chart
243243
- `chart:updated` - if the update handler performs an update instead of a re-render
244244
- `labels:updated` - if new labels were set
245245

246+
### chartjs-plugin-annotation
247+
248+
When using [chartjs-plugin-annotation](https://www.chartjs.org/chartjs-plugin-annotation/latest/) and **Vue 2** simultaneously, you will not be able to place multiple reactive charts on one page.
249+
246250
## Examples
247251

248252
### Chart with props

0 commit comments

Comments
 (0)