reactjs / react-chartjs

common react charting components using chart.js
MIT License
2.93k stars 299 forks source link

v2: How do I add or remove a dataset to or from the datasets of a chart #128

Open ledsun opened 8 years ago

ledsun commented 8 years ago

Background

There is two way to update datasets of a chart.

  1. Set a new datasets by this.setState
  2. Update the this.state.datasets and call this.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]; } } }); ```

https://github.com/jhudson8/react-chartjs/blob/chartjs-v2/lib/core.js#L57

Question

Which method is better to change the datasets of a chart?

ledsun commented 8 years ago

I think this is similar to #38.