@@ -119,13 +119,22 @@ var ChartComponent = function (_React$Component) {
119
119
return true ;
120
120
}
121
121
122
- return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextProps . data ) ;
122
+ var nextData = this . transformDataProp ( ) ;
123
+ return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextData ) ;
123
124
}
124
125
} , {
125
126
key : 'componentWillUnmount' ,
126
127
value : function componentWillUnmount ( ) {
127
128
this . chart_instance . destroy ( ) ;
128
129
}
130
+ } , {
131
+ key : 'transformDataProp' ,
132
+ value : function transformDataProp ( ) {
133
+ var data = this . props . data ;
134
+
135
+ var node = _reactDom2 . default . findDOMNode ( this ) ;
136
+ return typeof data == "function" ? data ( node ) : data ;
137
+ }
129
138
130
139
// Chart.js directly mutates the data.dataset objects by adding _meta proprerty
131
140
// this makes impossible to compare the current and next data changes
@@ -135,28 +144,27 @@ var ChartComponent = function (_React$Component) {
135
144
} , {
136
145
key : 'memoizeDataProps' ,
137
146
value : function memoizeDataProps ( ) {
138
- var data = this . props . data ;
139
-
140
-
141
- if ( ! data ) {
147
+ if ( ! this . props . data ) {
142
148
return ;
143
149
}
144
150
151
+ var data = this . transformDataProp ( ) ;
152
+
145
153
this . shadowDataProp = _extends ( { } , data , {
146
154
datasets : data . datasets && data . datasets . map ( function ( set ) {
147
155
return Object . assign ( { } , set ) ;
148
156
} )
149
157
} ) ;
158
+
159
+ return data ;
150
160
}
151
161
} , {
152
162
key : 'updateChart' ,
153
163
value : function updateChart ( ) {
154
- var _props2 = this . props ,
155
- data = _props2 . data ,
156
- options = _props2 . options ;
164
+ var options = this . props . options ;
157
165
158
166
159
- this . memoizeDataProps ( ) ;
167
+ var data = this . memoizeDataProps ( ) ;
160
168
161
169
if ( ! this . chart_instance ) return ;
162
170
@@ -169,6 +177,11 @@ var ChartComponent = function (_React$Component) {
169
177
var currentDatasets = this . chart_instance . config . data && this . chart_instance . config . data . datasets || [ ] ;
170
178
var nextDatasets = data . datasets || [ ] ;
171
179
180
+ // Prevent charting of legend items that no longer exist
181
+ while ( currentDatasets . length > nextDatasets . length ) {
182
+ currentDatasets . pop ( ) ;
183
+ }
184
+
172
185
nextDatasets . forEach ( function ( dataset , sid ) {
173
186
if ( currentDatasets [ sid ] && currentDatasets [ sid ] . data ) {
174
187
currentDatasets [ sid ] . data . splice ( nextDatasets [ sid ] . data . length ) ;
@@ -198,16 +211,14 @@ var ChartComponent = function (_React$Component) {
198
211
} , {
199
212
key : 'renderChart' ,
200
213
value : function renderChart ( ) {
201
- var _props3 = this . props ,
202
- data = _props3 . data ,
203
- options = _props3 . options ,
204
- legend = _props3 . legend ,
205
- type = _props3 . type ,
206
- redraw = _props3 . redraw ;
214
+ var _props2 = this . props ,
215
+ options = _props2 . options ,
216
+ legend = _props2 . legend ,
217
+ type = _props2 . type ,
218
+ redraw = _props2 . redraw ;
207
219
208
220
var node = _reactDom2 . default . findDOMNode ( this ) ;
209
-
210
- this . memoizeDataProps ( ) ;
221
+ var data = this . memoizeDataProps ( ) ;
211
222
212
223
this . chart_instance = new _chart2 . default ( node , {
213
224
type : type ,
@@ -218,10 +229,10 @@ var ChartComponent = function (_React$Component) {
218
229
} , {
219
230
key : 'render' ,
220
231
value : function render ( ) {
221
- var _props4 = this . props ,
222
- height = _props4 . height ,
223
- width = _props4 . width ,
224
- onElementsClick = _props4 . onElementsClick ;
232
+ var _props3 = this . props ,
233
+ height = _props3 . height ,
234
+ width = _props3 . width ,
235
+ onElementsClick = _props3 . onElementsClick ;
225
236
226
237
227
238
return _react2 . default . createElement ( 'canvas' , {
@@ -236,7 +247,7 @@ var ChartComponent = function (_React$Component) {
236
247
} ( _react2 . default . Component ) ;
237
248
238
249
ChartComponent . propTypes = {
239
- data : _react . PropTypes . object . isRequired ,
250
+ data : _react . PropTypes . oneOfType ( [ _react . PropTypes . object , _react . PropTypes . func ] ) . isRequired ,
240
251
getDatasetAtEvent : _react . PropTypes . func ,
241
252
getElementAtEvent : _react . PropTypes . func ,
242
253
getElementsAtEvent : _react . PropTypes . func ,
0 commit comments