react-chartjs
react-chartjs copied to clipboard
v2: How do I add or remove a dataset to or from the datasets of a chart
Background
There is two way to update datasets of a chart.
- Set a new datasets by
this.setState - Update the
this.state.datasetsand callthis.forceUpdate
1 works well only update datasets without changing a number of datasets. 2 works well all manipulation.
Implementation
The source code below is effective when 1.
But it is not effective when 2. Because the chart.data.datasets is already updated.
assign all from the next datasets to the current chart
// assign all of the properites from the next datasets to the current chart
nextProps.data.datasets.forEach(function(set, setIndex) {
var chartDataset = chart.data.datasets[setIndex];
for (var property in set) {
if (set.hasOwnProperty(property)) {
chartDataset[property] = set[property];
}
}
});
Which method is better to change the datasets of a chart?
I think this is similar to #38.