Skip to content

Commit 04fb6d6

Browse files
committed
feat: expose chart.js object
1 parent c3286d6 commit 04fb6d6

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/CChart.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
PropType,
99
ref,
1010
Ref,
11+
shallowRef,
1112
} from 'vue'
1213

1314
import Chart, { ChartData, ChartOptions, ChartType, Plugin } from 'chart.js/auto'
@@ -53,7 +54,7 @@ const CChart = defineComponent({
5354
type: String,
5455
},
5556
/**
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.
5758
*
5859
* {@link https://www.chartjs.org/docs/latest/general/options.html More Info}
5960
*/
@@ -114,9 +115,10 @@ const CChart = defineComponent({
114115
*/
115116
'getElementsAtEvent',
116117
],
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+
120122
const computedData = computed(() =>
121123
typeof props.data === 'function'
122124
? canvasRef.value
@@ -135,7 +137,7 @@ const CChart = defineComponent({
135137
chartjs.defaults.plugins.tooltip.external = cuiCustomTooltips
136138
}
137139

138-
chart = new Chart(canvasRef.value, {
140+
chartRef.value = new Chart(canvasRef.value, {
139141
type: props.type,
140142
data: computedData.value,
141143
options: props.options as ChartOptions,
@@ -144,44 +146,44 @@ const CChart = defineComponent({
144146
}
145147

146148
const handleOnClick = (e: Event) => {
147-
if (!chart) return
149+
if (!chartRef.value) return
148150

149151
emit(
150152
'getDatasetAtEvent',
151-
chart.getElementsAtEventForMode(e, 'dataset', { intersect: true }, false),
153+
chartRef.value.getElementsAtEventForMode(e, 'dataset', { intersect: true }, false),
152154
e,
153155
)
154156
emit(
155157
'getElementAtEvent',
156-
chart.getElementsAtEventForMode(e, 'nearest', { intersect: true }, false),
158+
chartRef.value.getElementsAtEventForMode(e, 'nearest', { intersect: true }, false),
157159
e,
158160
)
159161
emit(
160162
'getElementsAtEvent',
161-
chart.getElementsAtEventForMode(e, 'index', { intersect: true }, false),
163+
chartRef.value.getElementsAtEventForMode(e, 'index', { intersect: true }, false),
162164
e,
163165
)
164166
}
165167

166168
const updateChart = () => {
167-
if (!chart) return
169+
if (!chartRef.value) return
168170

169171
if (props.options) {
170-
chart.options = { ...props.options }
172+
chartRef.value.options = { ...props.options }
171173
}
172174

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()
176178
return
177179
}
178180

179181
const { datasets: newDataSets = [], ...newChartData } = computedData.value
180-
const { datasets: currentDataSets = [] } = chart.config.data
182+
const { datasets: currentDataSets = [] } = chartRef.value.config.data
181183

182184
// 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) => {
185187
// given the new set, find it's current match
186188
const currentDataSet = find(
187189
currentDataSets,
@@ -208,11 +210,11 @@ const CChart = defineComponent({
208210
}
209211
})
210212

211-
chart && chart.update()
213+
chartRef.value && chartRef.value.update()
212214
}
213215

214216
const destroyChart = () => {
215-
if (chart) chart.destroy()
217+
if (chartRef.value) chartRef.value.destroy()
216218
}
217219

218220
onMounted(() => {
@@ -234,7 +236,7 @@ const CChart = defineComponent({
234236
}
235237
})
236238

237-
const canvas = (ref: Ref<HTMLCanvasElement | undefined>) =>
239+
const canvas = (ref: Ref<HTMLCanvasElement | null>) =>
238240
h(
239241
'canvas',
240242
{
@@ -250,6 +252,8 @@ const CChart = defineComponent({
250252
},
251253
)
252254

255+
expose({ chart: chartRef })
256+
253257
return () =>
254258
props.wrapper ? h('div', { class: 'chart-wrapper' }, canvas(canvasRef)) : canvas(canvasRef)
255259
},

0 commit comments

Comments
 (0)