@@ -119,13 +119,22 @@ var ChartComponent = function (_React$Component) {
119119 return true ;
120120 }
121121
122- return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextProps . data ) ;
122+ var nextData = this . transformDataProp ( ) ;
123+ return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextData ) ;
123124 }
124125 } , {
125126 key : 'componentWillUnmount' ,
126127 value : function componentWillUnmount ( ) {
127128 this . chart_instance . destroy ( ) ;
128129 }
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+ }
129138
130139 // Chart.js directly mutates the data.dataset objects by adding _meta proprerty
131140 // this makes impossible to compare the current and next data changes
@@ -135,28 +144,27 @@ var ChartComponent = function (_React$Component) {
135144 } , {
136145 key : 'memoizeDataProps' ,
137146 value : function memoizeDataProps ( ) {
138- var data = this . props . data ;
139-
140-
141- if ( ! data ) {
147+ if ( ! this . props . data ) {
142148 return ;
143149 }
144150
151+ var data = this . transformDataProp ( ) ;
152+
145153 this . shadowDataProp = _extends ( { } , data , {
146154 datasets : data . datasets && data . datasets . map ( function ( set ) {
147155 return Object . assign ( { } , set ) ;
148156 } )
149157 } ) ;
158+
159+ return data ;
150160 }
151161 } , {
152162 key : 'updateChart' ,
153163 value : function updateChart ( ) {
154- var _props2 = this . props ,
155- data = _props2 . data ,
156- options = _props2 . options ;
164+ var options = this . props . options ;
157165
158166
159- this . memoizeDataProps ( ) ;
167+ var data = this . memoizeDataProps ( ) ;
160168
161169 if ( ! this . chart_instance ) return ;
162170
@@ -169,6 +177,11 @@ var ChartComponent = function (_React$Component) {
169177 var currentDatasets = this . chart_instance . config . data && this . chart_instance . config . data . datasets || [ ] ;
170178 var nextDatasets = data . datasets || [ ] ;
171179
180+ // Prevent charting of legend items that no longer exist
181+ while ( currentDatasets . length > nextDatasets . length ) {
182+ currentDatasets . pop ( ) ;
183+ }
184+
172185 nextDatasets . forEach ( function ( dataset , sid ) {
173186 if ( currentDatasets [ sid ] && currentDatasets [ sid ] . data ) {
174187 currentDatasets [ sid ] . data . splice ( nextDatasets [ sid ] . data . length ) ;
@@ -198,16 +211,14 @@ var ChartComponent = function (_React$Component) {
198211 } , {
199212 key : 'renderChart' ,
200213 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 ;
207219
208220 var node = _reactDom2 . default . findDOMNode ( this ) ;
209-
210- this . memoizeDataProps ( ) ;
221+ var data = this . memoizeDataProps ( ) ;
211222
212223 this . chart_instance = new _chart2 . default ( node , {
213224 type : type ,
@@ -218,10 +229,10 @@ var ChartComponent = function (_React$Component) {
218229 } , {
219230 key : 'render' ,
220231 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 ;
225236
226237
227238 return _react2 . default . createElement ( 'canvas' , {
@@ -236,7 +247,7 @@ var ChartComponent = function (_React$Component) {
236247} ( _react2 . default . Component ) ;
237248
238249ChartComponent . propTypes = {
239- data : _react . PropTypes . object . isRequired ,
250+ data : _react . PropTypes . oneOfType ( [ _react . PropTypes . object , _react . PropTypes . func ] ) . isRequired ,
240251 getDatasetAtEvent : _react . PropTypes . func ,
241252 getElementAtEvent : _react . PropTypes . func ,
242253 getElementsAtEvent : _react . PropTypes . func ,
0 commit comments