@@ -58,7 +58,7 @@ class ChartComponent extends React.Component {
58
58
59
59
componentDidUpdate ( ) {
60
60
if ( this . props . redraw ) {
61
- this . chartInstance . destroy ( ) ;
61
+ this . destroyChart ( ) ;
62
62
this . renderChart ( ) ;
63
63
return ;
64
64
}
@@ -109,7 +109,7 @@ class ChartComponent extends React.Component {
109
109
}
110
110
111
111
componentWillUnmount ( ) {
112
- this . chartInstance . destroy ( ) ;
112
+ this . destroyChart ( ) ;
113
113
}
114
114
115
115
transformDataProp ( props ) {
@@ -142,6 +142,8 @@ class ChartComponent extends React.Component {
142
142
} )
143
143
} ;
144
144
145
+ this . saveCurrentDatasets ( ) ; // to remove the dataset metadata from this chart when the chart is destroyed
146
+
145
147
return data ;
146
148
}
147
149
@@ -164,6 +166,18 @@ class ChartComponent extends React.Component {
164
166
}
165
167
}
166
168
169
+ getCurrentDatasets ( ) {
170
+ return ( this . chartInstance && this . chartInstance . config . data && this . chartInstance . config . data . datasets ) || [ ] ;
171
+ }
172
+
173
+ saveCurrentDatasets ( ) {
174
+ this . datasets = this . datasets || { } ;
175
+ var currentDatasets = this . getCurrentDatasets ( ) ;
176
+ currentDatasets . forEach ( d => {
177
+ this . datasets [ this . props . datasetKeyProvider ( d ) ] = d ;
178
+ } )
179
+ }
180
+
167
181
updateChart ( ) {
168
182
const { options} = this . props ;
169
183
@@ -177,7 +191,7 @@ class ChartComponent extends React.Component {
177
191
178
192
// Pipe datasets to chart instance datasets enabling
179
193
// seamless transitions
180
- let currentDatasets = ( this . chartInstance . config . data && this . chartInstance . config . data . datasets ) || [ ] ;
194
+ let currentDatasets = this . getCurrentDatasets ( ) ;
181
195
const nextDatasets = data . datasets || [ ] ;
182
196
this . checkDatasets ( currentDatasets ) ;
183
197
@@ -238,6 +252,19 @@ class ChartComponent extends React.Component {
238
252
} ) ;
239
253
}
240
254
255
+ destroyChart ( ) {
256
+ // Put all of the datasets that have existed in the chart back on the chart
257
+ // so that the metadata associated with this chart get destroyed.
258
+ // This allows the datasets to be used in another chart. This can happen,
259
+ // for example, in a tabbed UI where the chart gets created each time the
260
+ // tab gets switched to the chart and uses the same data).
261
+ this . saveCurrentDatasets ( ) ;
262
+ const datasets = Object . values ( this . datasets ) ;
263
+ this . chartInstance . config . data . datasets = datasets ;
264
+
265
+ this . chartInstance . destroy ( ) ;
266
+ }
267
+
241
268
handleOnClick = ( event ) => {
242
269
const instance = this . chartInstance ;
243
270
0 commit comments