Skip to content

Commit 3c1545f

Browse files
committed
fix(CChart): chart is not reactive
1 parent 27e0f85 commit 3c1545f

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/coreui-vue-chartjs/src/CChart.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
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'
212

313
import Chart, { ChartData, ChartOptions, ChartType, Plugin } from 'chart.js/auto'
414
import * as chartjs from 'chart.js'
@@ -118,13 +128,19 @@ const CChart = defineComponent({
118128
setup(props, { emit, slots }) {
119129
const canvasRef = ref<HTMLCanvasElement>()
120130
let chart: Chart | null
121-
122-
const computedData =
131+
const computedData = computed(() =>
123132
typeof props.data === 'function'
124133
? canvasRef.value
125134
? props.data(canvasRef.value)
126135
: { 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)
128144

129145
const renderChart = () => {
130146
if (!canvasRef.value) return
@@ -138,7 +154,7 @@ const CChart = defineComponent({
138154

139155
chart = new Chart(canvasRef.value, {
140156
type: props.type,
141-
data: computedData,
157+
data: computedData.value,
142158
options: props.options as ChartOptions,
143159
plugins: props.plugins,
144160
})
@@ -172,12 +188,12 @@ const CChart = defineComponent({
172188
}
173189

174190
if (!chart.config.data) {
175-
chart.config.data = computedData
191+
chart.config.data = computedData.value
176192
chart.update()
177193
return
178194
}
179195

180-
const { datasets: newDataSets = [], ...newChartData } = computedData
196+
const { datasets: newDataSets = [], ...newChartData } = computedData.value
181197
const { datasets: currentDataSets = [] } = chart.config.data
182198

183199
// copy values

0 commit comments

Comments
 (0)