8
8
PropType ,
9
9
ref ,
10
10
Ref ,
11
+ shallowRef ,
11
12
} from 'vue'
12
13
13
14
import Chart , { ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto'
@@ -53,7 +54,7 @@ const CChart = defineComponent({
53
54
type : String ,
54
55
} ,
55
56
/**
56
- * The options object that is passed into the Chart.js chart .
57
+ * The options object that is passed into the Chart.js chartRef.value .
57
58
*
58
59
* {@link https://www.chartjs.org/docs/latest/general/options.html More Info}
59
60
*/
@@ -114,9 +115,10 @@ const CChart = defineComponent({
114
115
*/
115
116
'getElementsAtEvent' ,
116
117
] ,
117
- setup ( props , { emit, slots } ) {
118
- const canvasRef = ref < HTMLCanvasElement > ( )
119
- let chart : Chart | null
118
+ setup ( props , { expose, emit, slots } ) {
119
+ const canvasRef = ref < HTMLCanvasElement | null > ( null )
120
+ const chartRef = shallowRef < Chart | null > ( null )
121
+
120
122
const computedData = computed ( ( ) =>
121
123
typeof props . data === 'function'
122
124
? canvasRef . value
@@ -135,7 +137,7 @@ const CChart = defineComponent({
135
137
chartjs . defaults . plugins . tooltip . external = cuiCustomTooltips
136
138
}
137
139
138
- chart = new Chart ( canvasRef . value , {
140
+ chartRef . value = new Chart ( canvasRef . value , {
139
141
type : props . type ,
140
142
data : computedData . value ,
141
143
options : props . options as ChartOptions ,
@@ -144,44 +146,44 @@ const CChart = defineComponent({
144
146
}
145
147
146
148
const handleOnClick = ( e : Event ) => {
147
- if ( ! chart ) return
149
+ if ( ! chartRef . value ) return
148
150
149
151
emit (
150
152
'getDatasetAtEvent' ,
151
- chart . getElementsAtEventForMode ( e , 'dataset' , { intersect : true } , false ) ,
153
+ chartRef . value . getElementsAtEventForMode ( e , 'dataset' , { intersect : true } , false ) ,
152
154
e ,
153
155
)
154
156
emit (
155
157
'getElementAtEvent' ,
156
- chart . getElementsAtEventForMode ( e , 'nearest' , { intersect : true } , false ) ,
158
+ chartRef . value . getElementsAtEventForMode ( e , 'nearest' , { intersect : true } , false ) ,
157
159
e ,
158
160
)
159
161
emit (
160
162
'getElementsAtEvent' ,
161
- chart . getElementsAtEventForMode ( e , 'index' , { intersect : true } , false ) ,
163
+ chartRef . value . getElementsAtEventForMode ( e , 'index' , { intersect : true } , false ) ,
162
164
e ,
163
165
)
164
166
}
165
167
166
168
const updateChart = ( ) => {
167
- if ( ! chart ) return
169
+ if ( ! chartRef . value ) return
168
170
169
171
if ( props . options ) {
170
- chart . options = { ...props . options }
172
+ chartRef . value . options = { ...props . options }
171
173
}
172
174
173
- if ( ! chart . config . data ) {
174
- chart . config . data = computedData . value
175
- chart . update ( )
175
+ if ( ! chartRef . value . config . data ) {
176
+ chartRef . value . config . data = computedData . value
177
+ chartRef . value . update ( )
176
178
return
177
179
}
178
180
179
181
const { datasets : newDataSets = [ ] , ...newChartData } = computedData . value
180
- const { datasets : currentDataSets = [ ] } = chart . config . data
182
+ const { datasets : currentDataSets = [ ] } = chartRef . value . config . data
181
183
182
184
// copy values
183
- assign ( chart . config . data , newChartData )
184
- chart . config . data . datasets = newDataSets . map ( ( newDataSet : any ) => {
185
+ assign ( chartRef . value . config . data , newChartData )
186
+ chartRef . value . config . data . datasets = newDataSets . map ( ( newDataSet : any ) => {
185
187
// given the new set, find it's current match
186
188
const currentDataSet = find (
187
189
currentDataSets ,
@@ -208,11 +210,11 @@ const CChart = defineComponent({
208
210
}
209
211
} )
210
212
211
- chart && chart . update ( )
213
+ chartRef . value && chartRef . value . update ( )
212
214
}
213
215
214
216
const destroyChart = ( ) => {
215
- if ( chart ) chart . destroy ( )
217
+ if ( chartRef . value ) chartRef . value . destroy ( )
216
218
}
217
219
218
220
onMounted ( ( ) => {
@@ -234,7 +236,7 @@ const CChart = defineComponent({
234
236
}
235
237
} )
236
238
237
- const canvas = ( ref : Ref < HTMLCanvasElement | undefined > ) =>
239
+ const canvas = ( ref : Ref < HTMLCanvasElement | null > ) =>
238
240
h (
239
241
'canvas' ,
240
242
{
@@ -250,6 +252,8 @@ const CChart = defineComponent({
250
252
} ,
251
253
)
252
254
255
+ expose ( { chart : chartRef } )
256
+
253
257
return ( ) =>
254
258
props . wrapper ? h ( 'div' , { class : 'chart-wrapper' } , canvas ( canvasRef ) ) : canvas ( canvasRef )
255
259
} ,
0 commit comments