1
- import { defineComponent , h , onMounted , onUnmounted , onUpdated , PropType , ref , Ref } from 'vue'
1
+ import {
2
+ computed ,
3
+ defineComponent ,
4
+ h ,
5
+ onMounted ,
6
+ onUnmounted ,
7
+ onUpdated ,
8
+ PropType ,
9
+ ref ,
10
+ Ref ,
11
+ } from 'vue'
2
12
3
13
import Chart , { ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto'
4
14
import * as chartjs from 'chart.js'
@@ -118,13 +128,19 @@ const CChart = defineComponent({
118
128
setup ( props , { emit, slots } ) {
119
129
const canvasRef = ref < HTMLCanvasElement > ( )
120
130
let chart : Chart | null
121
-
122
- const computedData =
131
+ const computedData = computed ( ( ) =>
123
132
typeof props . data === 'function'
124
133
? canvasRef . value
125
134
? props . data ( canvasRef . value )
126
135
: { datasets : [ ] }
127
- : merge ( { } , props . data )
136
+ : merge ( { } , props . data ) ,
137
+ )
138
+ // const computedData =
139
+ // typeof props.data === 'function'
140
+ // ? canvasRef.value
141
+ // ? props.data(canvasRef.value)
142
+ // : { datasets: [] }
143
+ // : merge({}, props.data)
128
144
129
145
const renderChart = ( ) => {
130
146
if ( ! canvasRef . value ) return
@@ -138,7 +154,7 @@ const CChart = defineComponent({
138
154
139
155
chart = new Chart ( canvasRef . value , {
140
156
type : props . type ,
141
- data : computedData ,
157
+ data : computedData . value ,
142
158
options : props . options as ChartOptions ,
143
159
plugins : props . plugins ,
144
160
} )
@@ -172,12 +188,12 @@ const CChart = defineComponent({
172
188
}
173
189
174
190
if ( ! chart . config . data ) {
175
- chart . config . data = computedData
191
+ chart . config . data = computedData . value
176
192
chart . update ( )
177
193
return
178
194
}
179
195
180
- const { datasets : newDataSets = [ ] , ...newChartData } = computedData
196
+ const { datasets : newDataSets = [ ] , ...newChartData } = computedData . value
181
197
const { datasets : currentDataSets = [ ] } = chart . config . data
182
198
183
199
// copy values
0 commit comments