react-chartjs icon indicating copy to clipboard operation
react-chartjs copied to clipboard

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

Open ledsun opened this issue 9 years ago • 1 comments

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 avatar Jul 08 '16 02:07 ledsun

I think this is similar to #38.

ledsun avatar Jul 11 '16 13:07 ledsun