@@ -23,7 +23,7 @@ import {
23
23
} from '../../src/utils'
24
24
25
25
export function generateChart ( chartId , chartType , chartController ) {
26
- let _chart = null
26
+ let _chartRef = null
27
27
28
28
return {
29
29
props : {
@@ -68,6 +68,8 @@ export function generateChart(chartId, chartType, chartController) {
68
68
ChartJS . register ( chartController )
69
69
} ,
70
70
mounted ( ) {
71
+ _chartRef = { current : null }
72
+
71
73
if ( 'datasets' in this . chartData && this . chartData . datasets . length > 0 ) {
72
74
chartCreate ( this . renderChart , this . chartData , this . chartOptions )
73
75
this . $emit ( ChartEmits . ChartRendered )
@@ -80,8 +82,8 @@ export function generateChart(chartId, chartType, chartController) {
80
82
} ,
81
83
methods : {
82
84
renderChart ( data , options ) {
83
- if ( _chart !== null ) {
84
- chartDestroy ( _chart )
85
+ if ( _chartRef ?. current !== null ) {
86
+ chartDestroy ( _chartRef . current )
85
87
this . $emit ( ChartEmits . ChartDestroyed )
86
88
}
87
89
@@ -93,7 +95,7 @@ export function generateChart(chartId, chartType, chartController) {
93
95
const canvasEl2DContext = this . $refs . canvas . getContext ( '2d' )
94
96
95
97
if ( canvasEl2DContext !== null ) {
96
- _chart = new ChartJS ( canvasEl2DContext , {
98
+ _chartRef . current = new ChartJS ( canvasEl2DContext , {
97
99
type : chartType ,
98
100
data : chartData ,
99
101
options,
@@ -109,28 +111,28 @@ export function generateChart(chartId, chartType, chartController) {
109
111
if ( Object . keys ( oldData ) . length > 0 ) {
110
112
const isEqualLabelsAndDatasetsLength = compareData ( newData , oldData )
111
113
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 )
114
116
115
117
if ( newData . labels !== undefined ) {
116
- setChartLabels ( _chart , newData . labels )
118
+ setChartLabels ( _chartRef . current , newData . labels )
117
119
this . $emit ( ChartEmits . LabelsUpdated )
118
120
}
119
121
120
- chartUpdate ( _chart )
122
+ chartUpdate ( _chartRef . current )
121
123
this . $emit ( ChartEmits . ChartUpdated )
122
124
} else {
123
- if ( _chart !== null ) {
124
- chartDestroy ( _chart )
125
+ if ( _chartRef ?. current !== null ) {
126
+ chartDestroy ( _chartRef . currentt )
125
127
this . $emit ( ChartEmits . ChartDestroyed )
126
128
}
127
129
128
130
chartCreate ( this . renderChart , this . chartData , this . chartOptions )
129
131
this . $emit ( ChartEmits . ChartRendered )
130
132
}
131
133
} else {
132
- if ( _chart !== null ) {
133
- chartDestroy ( _chart )
134
+ if ( _chartRef ?. current !== null ) {
135
+ chartDestroy ( _chartRef . current )
134
136
this . $emit ( ChartEmits . ChartDestroyed )
135
137
}
136
138
@@ -140,8 +142,8 @@ export function generateChart(chartId, chartType, chartController) {
140
142
}
141
143
} ,
142
144
beforeDestroy ( ) {
143
- if ( _chart !== null ) {
144
- chartDestroy ( _chart )
145
+ if ( _chartRef ?. current !== null ) {
146
+ chartDestroy ( _chartRef . current )
145
147
this . $emit ( ChartEmits . ChartDestroyed )
146
148
}
147
149
} ,
0 commit comments