Skip to content

Commit 76ad2ff

Browse files
committed
fixes
1 parent bcd940d commit 76ad2ff

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

lib/index.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

238249
ChartComponent.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,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-chartjs-2",
3-
"version": "2.0.0",
3+
"version": "2.0.4",
44
"description": "react-chartjs-2",
55
"main": "lib/index.js",
66
"author": "Goran Udosic",

src/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,17 @@ class ChartComponent extends React.Component {
9090
}
9191

9292
transformDataProp() {
93-
const { dataProp } = this.props;
93+
const { data } = this.props;
9494
const node = ReactDOM.findDOMNode(this);
95-
return (typeof(dataProp) == "function") ? dataProp(node) : dataProp;
95+
return (typeof(data) == "function") ? data(node) : data;
9696
}
9797

9898
// Chart.js directly mutates the data.dataset objects by adding _meta proprerty
9999
// this makes impossible to compare the current and next data changes
100100
// therefore we memoize the data prop while sending a fake to Chart.js for mutation.
101101
// see https://github.com/chartjs/Chart.js/blob/master/src/core/core.controller.js#L615-L617
102102
memoizeDataProps() {
103-
const { dataProp } = this.props;
104-
105-
if (!dataProp) {
103+
if (!this.props.data) {
106104
return;
107105
}
108106

@@ -131,7 +129,7 @@ class ChartComponent extends React.Component {
131129
// seamless transitions
132130
let currentDatasets = (this.chart_instance.config.data && this.chart_instance.config.data.datasets) || [];
133131
const nextDatasets = data.datasets || [];
134-
132+
135133
// Prevent charting of legend items that no longer exist
136134
while (currentDatasets.length > nextDatasets.length) {
137135
currentDatasets.pop();
@@ -170,7 +168,6 @@ class ChartComponent extends React.Component {
170168
renderChart() {
171169
const {options, legend, type, redraw} = this.props;
172170
const node = ReactDOM.findDOMNode(this);
173-
174171
const data = this.memoizeDataProps();
175172

176173
this.chart_instance = new Chart(node, {

0 commit comments

Comments
 (0)