Skip to content

Commit fe97040

Browse files
authored
fix(legacy-charts): fix render multiple charts at one page (#803)
* fix(legacy-charts): fix render multiple charts at one page fix the error, when multiple charts can't render on one page fix #800 #801 * fix: change syntax
1 parent 3ad8bc2 commit fe97040

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

legacy/src/Charts.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '../../src/utils'
2424

2525
export function generateChart(chartId, chartType, chartController) {
26-
let _chart = null
26+
let _chartRef = null
2727

2828
return {
2929
props: {
@@ -68,6 +68,8 @@ export function generateChart(chartId, chartType, chartController) {
6868
ChartJS.register(chartController)
6969
},
7070
mounted() {
71+
_chartRef = { current: null }
72+
7173
if ('datasets' in this.chartData && this.chartData.datasets.length > 0) {
7274
chartCreate(this.renderChart, this.chartData, this.chartOptions)
7375
this.$emit(ChartEmits.ChartRendered)
@@ -80,8 +82,8 @@ export function generateChart(chartId, chartType, chartController) {
8082
},
8183
methods: {
8284
renderChart(data, options) {
83-
if (_chart !== null) {
84-
chartDestroy(_chart)
85+
if (_chartRef?.current !== null) {
86+
chartDestroy(_chartRef.current)
8587
this.$emit(ChartEmits.ChartDestroyed)
8688
}
8789

@@ -93,7 +95,7 @@ export function generateChart(chartId, chartType, chartController) {
9395
const canvasEl2DContext = this.$refs.canvas.getContext('2d')
9496

9597
if (canvasEl2DContext !== null) {
96-
_chart = new ChartJS(canvasEl2DContext, {
98+
_chartRef.current = new ChartJS(canvasEl2DContext, {
9799
type: chartType,
98100
data: chartData,
99101
options,
@@ -109,28 +111,28 @@ export function generateChart(chartId, chartType, chartController) {
109111
if (Object.keys(oldData).length > 0) {
110112
const isEqualLabelsAndDatasetsLength = compareData(newData, oldData)
111113

112-
if (isEqualLabelsAndDatasetsLength && _chart !== null) {
113-
setChartDatasets(_chart.data, newData, this.datasetIdKey)
114+
if (isEqualLabelsAndDatasetsLength && _chartRef?.current !== null) {
115+
setChartDatasets(_chartRef.current.data, newData, this.datasetIdKey)
114116

115117
if (newData.labels !== undefined) {
116-
setChartLabels(_chart, newData.labels)
118+
setChartLabels(_chartRef.current, newData.labels)
117119
this.$emit(ChartEmits.LabelsUpdated)
118120
}
119121

120-
chartUpdate(_chart)
122+
chartUpdate(_chartRef.current)
121123
this.$emit(ChartEmits.ChartUpdated)
122124
} else {
123-
if (_chart !== null) {
124-
chartDestroy(_chart)
125+
if (_chartRef?.current !== null) {
126+
chartDestroy(_chartRef.currentt)
125127
this.$emit(ChartEmits.ChartDestroyed)
126128
}
127129

128130
chartCreate(this.renderChart, this.chartData, this.chartOptions)
129131
this.$emit(ChartEmits.ChartRendered)
130132
}
131133
} else {
132-
if (_chart !== null) {
133-
chartDestroy(_chart)
134+
if (_chartRef?.current !== null) {
135+
chartDestroy(_chartRef.current)
134136
this.$emit(ChartEmits.ChartDestroyed)
135137
}
136138

@@ -140,8 +142,8 @@ export function generateChart(chartId, chartType, chartController) {
140142
}
141143
},
142144
beforeDestroy() {
143-
if (_chart !== null) {
144-
chartDestroy(_chart)
145+
if (_chartRef?.current !== null) {
146+
chartDestroy(_chartRef.current)
145147
this.$emit(ChartEmits.ChartDestroyed)
146148
}
147149
},

0 commit comments

Comments
 (0)