@@ -23,6 +23,8 @@ import {
23
23
} from '../../src/utils'
24
24
25
25
export function generateChart ( chartId , chartType , chartController ) {
26
+ let _chart = null
27
+
26
28
return {
27
29
props : {
28
30
chartData : {
@@ -62,11 +64,6 @@ export function generateChart(chartId, chartType, chartController) {
62
64
default : ( ) => [ ]
63
65
}
64
66
} ,
65
- data ( ) {
66
- return {
67
- _chart : null
68
- }
69
- } ,
70
67
created ( ) {
71
68
ChartJS . register ( chartController )
72
69
} ,
@@ -83,8 +80,8 @@ export function generateChart(chartId, chartType, chartController) {
83
80
} ,
84
81
methods : {
85
82
renderChart ( data , options ) {
86
- if ( this . $data . _chart !== null ) {
87
- chartDestroy ( this . $data . _chart )
83
+ if ( _chart !== null ) {
84
+ chartDestroy ( _chart )
88
85
this . $emit ( ChartEmits . ChartDestroyed )
89
86
}
90
87
@@ -96,7 +93,7 @@ export function generateChart(chartId, chartType, chartController) {
96
93
const canvasEl2DContext = this . $refs . canvas . getContext ( '2d' )
97
94
98
95
if ( canvasEl2DContext !== null ) {
99
- this . $data . _chart = new ChartJS ( canvasEl2DContext , {
96
+ _chart = new ChartJS ( canvasEl2DContext , {
100
97
type : chartType ,
101
98
data : chartData ,
102
99
options,
@@ -110,32 +107,30 @@ export function generateChart(chartId, chartType, chartController) {
110
107
const oldData = { ...oldValue }
111
108
112
109
if ( Object . keys ( oldData ) . length > 0 ) {
113
- const chart = this . $data . _chart
114
-
115
110
const isEqualLabelsAndDatasetsLength = compareData ( newData , oldData )
116
111
117
- if ( isEqualLabelsAndDatasetsLength && chart !== null ) {
118
- setChartDatasets ( chart . data , newData , this . datasetIdKey )
112
+ if ( isEqualLabelsAndDatasetsLength && _chart !== null ) {
113
+ setChartDatasets ( _chart . data , newData , this . datasetIdKey )
119
114
120
115
if ( newData . labels !== undefined ) {
121
- setChartLabels ( chart , newData . labels )
116
+ setChartLabels ( _chart , newData . labels )
122
117
this . $emit ( ChartEmits . LabelsUpdated )
123
118
}
124
119
125
- chartUpdate ( chart )
120
+ chartUpdate ( _chart )
126
121
this . $emit ( ChartEmits . ChartUpdated )
127
122
} else {
128
- if ( chart !== null ) {
129
- chartDestroy ( chart )
123
+ if ( _chart !== null ) {
124
+ chartDestroy ( _chart )
130
125
this . $emit ( ChartEmits . ChartDestroyed )
131
126
}
132
127
133
128
chartCreate ( this . renderChart , this . chartData , this . chartOptions )
134
129
this . $emit ( ChartEmits . ChartRendered )
135
130
}
136
131
} else {
137
- if ( this . $data . _chart !== null ) {
138
- chartDestroy ( this . $data . _chart )
132
+ if ( _chart !== null ) {
133
+ chartDestroy ( _chart )
139
134
this . $emit ( ChartEmits . ChartDestroyed )
140
135
}
141
136
@@ -145,8 +140,8 @@ export function generateChart(chartId, chartType, chartController) {
145
140
}
146
141
} ,
147
142
beforeDestroy ( ) {
148
- if ( this . $data . _chart !== null ) {
149
- chartDestroy ( this . $data . _chart )
143
+ if ( _chart !== null ) {
144
+ chartDestroy ( _chart )
150
145
this . $emit ( ChartEmits . ChartDestroyed )
151
146
}
152
147
} ,
0 commit comments