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

Commit 2806031

Browse files
committed
Merge branch 'master' of github.com:jhudson8/react-chartjs
2 parents e255385 + 70f47f8 commit 2806031

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
Chart References
4141
----------------

lib/core.js

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

3737
classData.componentWillReceiveProps = function(nextProps) {
3838
var chart = this.state.chart;
39-
chart.destroy();
40-
this.initializeChart(nextProps);
39+
if (this.props.redraw) {
40+
chart.destroy();
41+
this.initializeChart(nextProps);
42+
} else {
43+
updatePoints(nextProps, chart);
44+
chart.update();
45+
}
4146
};
4247

4348
classData.initializeChart = function(nextProps) {
@@ -70,3 +75,30 @@ module.exports = {
7075
return React.createClass(classData);
7176
}
7277
};
78+
79+
var dataKeys = {
80+
'Line': 'points',
81+
'Radar': 'points',
82+
'Bar': 'bars'
83+
};
84+
85+
var updatePoints = function(nextProps, chart) {
86+
var name = chart.name;
87+
88+
if (name === 'PolarArea' || name === 'Pie') {
89+
nextProps.data.forEach(function(segment, segmentIndex) {
90+
chart.segments[segmentIndex].value = segment.value;
91+
});
92+
} else {
93+
var dataKey = dataKeys[name];
94+
nextProps.data.datasets.forEach(function(set, setIndex) {
95+
set.data.forEach(function(val, pointIndex) {
96+
chart.datasets[setIndex][dataKey][pointIndex].value = val;
97+
});
98+
});
99+
}
100+
};
101+
102+
103+
104+

0 commit comments

Comments
 (0)