Skip to content

Commit 543774c

Browse files
authored
fix: change legacy chart instance type (#789)
changing vue2 charts instance type to be compatible with chartjs-plugin-annotation #782
1 parent ad1ec69 commit 543774c

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

legacy/src/Charts.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
} from '../../src/utils'
2424

2525
export function generateChart(chartId, chartType, chartController) {
26+
let _chart = null
27+
2628
return {
2729
props: {
2830
chartData: {
@@ -62,11 +64,6 @@ export function generateChart(chartId, chartType, chartController) {
6264
default: () => []
6365
}
6466
},
65-
data() {
66-
return {
67-
_chart: null
68-
}
69-
},
7067
created() {
7168
ChartJS.register(chartController)
7269
},
@@ -83,8 +80,8 @@ export function generateChart(chartId, chartType, chartController) {
8380
},
8481
methods: {
8582
renderChart(data, options) {
86-
if (this.$data._chart !== null) {
87-
chartDestroy(this.$data._chart)
83+
if (_chart !== null) {
84+
chartDestroy(_chart)
8885
this.$emit(ChartEmits.ChartDestroyed)
8986
}
9087

@@ -96,7 +93,7 @@ export function generateChart(chartId, chartType, chartController) {
9693
const canvasEl2DContext = this.$refs.canvas.getContext('2d')
9794

9895
if (canvasEl2DContext !== null) {
99-
this.$data._chart = new ChartJS(canvasEl2DContext, {
96+
_chart = new ChartJS(canvasEl2DContext, {
10097
type: chartType,
10198
data: chartData,
10299
options,
@@ -110,32 +107,30 @@ export function generateChart(chartId, chartType, chartController) {
110107
const oldData = { ...oldValue }
111108

112109
if (Object.keys(oldData).length > 0) {
113-
const chart = this.$data._chart
114-
115110
const isEqualLabelsAndDatasetsLength = compareData(newData, oldData)
116111

117-
if (isEqualLabelsAndDatasetsLength && chart !== null) {
118-
setChartDatasets(chart.data, newData, this.datasetIdKey)
112+
if (isEqualLabelsAndDatasetsLength && _chart !== null) {
113+
setChartDatasets(_chart.data, newData, this.datasetIdKey)
119114

120115
if (newData.labels !== undefined) {
121-
setChartLabels(chart, newData.labels)
116+
setChartLabels(_chart, newData.labels)
122117
this.$emit(ChartEmits.LabelsUpdated)
123118
}
124119

125-
chartUpdate(chart)
120+
chartUpdate(_chart)
126121
this.$emit(ChartEmits.ChartUpdated)
127122
} else {
128-
if (chart !== null) {
129-
chartDestroy(chart)
123+
if (_chart !== null) {
124+
chartDestroy(_chart)
130125
this.$emit(ChartEmits.ChartDestroyed)
131126
}
132127

133128
chartCreate(this.renderChart, this.chartData, this.chartOptions)
134129
this.$emit(ChartEmits.ChartRendered)
135130
}
136131
} else {
137-
if (this.$data._chart !== null) {
138-
chartDestroy(this.$data._chart)
132+
if (_chart !== null) {
133+
chartDestroy(_chart)
139134
this.$emit(ChartEmits.ChartDestroyed)
140135
}
141136

@@ -145,8 +140,8 @@ export function generateChart(chartId, chartType, chartController) {
145140
}
146141
},
147142
beforeDestroy() {
148-
if (this.$data._chart !== null) {
149-
chartDestroy(this.$data._chart)
143+
if (_chart !== null) {
144+
chartDestroy(_chart)
150145
this.$emit(ChartEmits.ChartDestroyed)
151146
}
152147
},

0 commit comments

Comments
 (0)