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

New regions are not rendered when updating props

Open myktra opened this issue 8 years ago • 4 comments

Seems like unlike properties like axis and grid, changing regions neither removes any previous regions that were drawn the first time the C3Chart component was rendered nor renders the new ones.

Adding this.chart.regions.remove() to componentWillUpdateProps removes the regions but then new regions that are specified are not redrawn when C3Chart is re-rendered.

In a perhaps related observation, passing the unloadBeforeLoad prop doesn't seem to work for me...rendering seems unpredictable.

myktra avatar Mar 09 '17 11:03 myktra

Potentially related to https://github.com/c3js/c3/issues/1527.

myktra avatar Mar 09 '17 12:03 myktra

I found a quick workaround for a demo. Until c3 fixes this on their end, this will at least cause the regions to update as expected.

updateChart(config) {
    if (!this.chart) {
      this.chart = this.generateChart(findDOMNode(this), config);
    }

    if (config.unloadBeforeLoad) {
        this.unloadData();
    }
    
    // If there are regions in the config, update them.
    // If there are regions on the chart, and the config specifies none 
    // then delete the existing regions.
    if (config.regions) {
        this.chart.regions(config.regions);
    } else if (this.chart.regions && !config.regions){
        this.chart.regions([]);
    }

    this.loadNewData(config.data);
  }

nels-o avatar Mar 20 '17 18:03 nels-o

The c3 docs state that calling unload just before load will not work properly, as it will occur in the midst of the animation.

http://c3js.org/reference.html#api-unload

If you call load API soon after/before unload, unload param of load should be used. Otherwise chart will not be rendered properly because of cancel of animation.

I haven't tested this yet, but I think what should be done instead is pass unload: true when loading:

http://c3js.org/reference.html#api-load

If unload given, data will be unloaded before loading new data. If true given, all of data will be unloaded.

Hence it should work like this:

  updateChart(config) {
    if (!this.chart) {
      this.chart = this.generateChart(findDOMNode(this), config);
    }

    let data = config.data;
    if (config.unloadBeforeLoad) {
      data = { ...data, unload: true };
    }
    this.loadNewData(data);
  }

andreasheim avatar Mar 28 '17 00:03 andreasheim

I'm not using regions, so I'm not sure if my changes fix that. However I did verify that using load with unload: true does fix my issue with data not updating properly. Potentially both changes may be needed.

andreasheim avatar Mar 28 '17 16:03 andreasheim