@@ -1973,13 +1973,22 @@ var ChartComponent = function (_React$Component) {
1973
1973
return true ;
1974
1974
}
1975
1975
1976
- return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextProps . data ) ;
1976
+ var nextData = this . transformDataProp ( ) ;
1977
+ return ! ( 0 , _lodash2 . default ) ( this . shadowDataProp , nextData ) ;
1977
1978
}
1978
1979
} , {
1979
1980
key : 'componentWillUnmount' ,
1980
1981
value : function componentWillUnmount ( ) {
1981
1982
this . chart_instance . destroy ( ) ;
1982
1983
}
1984
+ } , {
1985
+ key : 'transformDataProp' ,
1986
+ value : function transformDataProp ( ) {
1987
+ var data = this . props . data ;
1988
+
1989
+ var node = _reactDom2 . default . findDOMNode ( this ) ;
1990
+ return typeof data == "function" ? data ( node ) : data ;
1991
+ }
1983
1992
1984
1993
// Chart.js directly mutates the data.dataset objects by adding _meta proprerty
1985
1994
// this makes impossible to compare the current and next data changes
@@ -1989,28 +1998,27 @@ var ChartComponent = function (_React$Component) {
1989
1998
} , {
1990
1999
key : 'memoizeDataProps' ,
1991
2000
value : function memoizeDataProps ( ) {
1992
- var data = this . props . data ;
1993
-
1994
-
1995
- if ( ! data ) {
2001
+ if ( ! this . props . data ) {
1996
2002
return ;
1997
2003
}
1998
2004
2005
+ var data = this . transformDataProp ( ) ;
2006
+
1999
2007
this . shadowDataProp = _extends ( { } , data , {
2000
2008
datasets : data . datasets && data . datasets . map ( function ( set ) {
2001
2009
return _extends ( { } , set ) ;
2002
2010
} )
2003
2011
} ) ;
2012
+
2013
+ return data ;
2004
2014
}
2005
2015
} , {
2006
2016
key : 'updateChart' ,
2007
2017
value : function updateChart ( ) {
2008
- var _props2 = this . props ,
2009
- data = _props2 . data ,
2010
- options = _props2 . options ;
2018
+ var options = this . props . options ;
2011
2019
2012
2020
2013
- this . memoizeDataProps ( ) ;
2021
+ var data = this . memoizeDataProps ( ) ;
2014
2022
2015
2023
if ( ! this . chart_instance ) return ;
2016
2024
@@ -2023,6 +2031,11 @@ var ChartComponent = function (_React$Component) {
2023
2031
var currentDatasets = this . chart_instance . config . data && this . chart_instance . config . data . datasets || [ ] ;
2024
2032
var nextDatasets = data . datasets || [ ] ;
2025
2033
2034
+ // Prevent charting of legend items that no longer exist
2035
+ while ( currentDatasets . length > nextDatasets . length ) {
2036
+ currentDatasets . pop ( ) ;
2037
+ }
2038
+
2026
2039
nextDatasets . forEach ( function ( dataset , sid ) {
2027
2040
if ( currentDatasets [ sid ] && currentDatasets [ sid ] . data ) {
2028
2041
currentDatasets [ sid ] . data . splice ( nextDatasets [ sid ] . data . length ) ;
@@ -2052,16 +2065,14 @@ var ChartComponent = function (_React$Component) {
2052
2065
} , {
2053
2066
key : 'renderChart' ,
2054
2067
value : function renderChart ( ) {
2055
- var _props3 = this . props ,
2056
- data = _props3 . data ,
2057
- options = _props3 . options ,
2058
- legend = _props3 . legend ,
2059
- type = _props3 . type ,
2060
- redraw = _props3 . redraw ;
2068
+ var _props2 = this . props ,
2069
+ options = _props2 . options ,
2070
+ legend = _props2 . legend ,
2071
+ type = _props2 . type ,
2072
+ redraw = _props2 . redraw ;
2061
2073
2062
2074
var node = _reactDom2 . default . findDOMNode ( this ) ;
2063
-
2064
- this . memoizeDataProps ( ) ;
2075
+ var data = this . memoizeDataProps ( ) ;
2065
2076
2066
2077
this . chart_instance = new _chart2 . default ( node , {
2067
2078
type : type ,
@@ -2072,10 +2083,10 @@ var ChartComponent = function (_React$Component) {
2072
2083
} , {
2073
2084
key : 'render' ,
2074
2085
value : function render ( ) {
2075
- var _props4 = this . props ,
2076
- height = _props4 . height ,
2077
- width = _props4 . width ,
2078
- onElementsClick = _props4 . onElementsClick ;
2086
+ var _props3 = this . props ,
2087
+ height = _props3 . height ,
2088
+ width = _props3 . width ,
2089
+ onElementsClick = _props3 . onElementsClick ;
2079
2090
2080
2091
2081
2092
return _react2 . default . createElement ( 'canvas' , {
@@ -2090,7 +2101,7 @@ var ChartComponent = function (_React$Component) {
2090
2101
} ( _react2 . default . Component ) ;
2091
2102
2092
2103
ChartComponent . propTypes = {
2093
- data : _react . PropTypes . object . isRequired ,
2104
+ data : _react . PropTypes . oneOfType ( [ _react . PropTypes . object , _react . PropTypes . func ] ) . isRequired ,
2094
2105
getDatasetAtEvent : _react . PropTypes . func ,
2095
2106
getElementAtEvent : _react . PropTypes . func ,
2096
2107
getElementsAtEvent : _react . PropTypes . func ,
0 commit comments