Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 70f47f8

Browse files
committed
Merge pull request #8 from jefffriesen/updating
all chart types update with new data passed in
2 parents c4593a4 + 91a3a4f commit 70f47f8

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var MyComponent = React.createClass({
3535
* ```data``` represents the chart data (see [chart.js](http://www.chartjs.org/) for details)
3636
* ```options``` represents the chart options (see [chart.js](http://www.chartjs.org/) for details)
3737
* all other parameters will be passed through to the ```canvas``` element
38-
38+
* if data passed into the component changes, points will animate between values using chart.js' ```.update()```. If you want the chart destroyed and redrawn on every change, pass in ```redraw``` as a prop. For example ```<LineChart data={this.state.chartData} redraw />```
3939

4040
### Other React projects that may interest you
4141

lib/core.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ module.exports = {
3434

3535
classData.componentWillReceiveProps = function(nextProps) {
3636
var chart = this.state.chart;
37-
chart.destroy();
38-
this.initializeChart(nextProps);
37+
if (this.props.redraw) {
38+
chart.destroy();
39+
this.initializeChart(nextProps);
40+
} else {
41+
updatePoints(nextProps, chart);
42+
chart.update();
43+
}
3944
};
4045

4146
classData.initializeChart = function(nextProps) {
@@ -58,3 +63,30 @@ module.exports = {
5863
return React.createClass(classData);
5964
}
6065
};
66+
67+
var dataKeys = {
68+
'Line': 'points',
69+
'Radar': 'points',
70+
'Bar': 'bars'
71+
};
72+
73+
var updatePoints = function(nextProps, chart) {
74+
var name = chart.name;
75+
76+
if (name === 'PolarArea' || name === 'Pie') {
77+
nextProps.data.forEach(function(segment, segmentIndex) {
78+
chart.segments[segmentIndex].value = segment.value;
79+
});
80+
} else {
81+
var dataKey = dataKeys[name];
82+
nextProps.data.datasets.forEach(function(set, setIndex) {
83+
set.data.forEach(function(val, pointIndex) {
84+
chart.datasets[setIndex][dataKey][pointIndex].value = val;
85+
});
86+
});
87+
}
88+
};
89+
90+
91+
92+

0 commit comments

Comments
 (0)