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

Re-rendering plot when any state of the component changes

Open vaasu070 opened this issue 5 years ago • 54 comments

This behaviour was not observed with older version of chartjs. i.e version less than 3. When ever the state of the component changes , entire plot re renders again as if it is rendering for the first time. sample code to this behaviour is here

vaasu070 avatar Apr 22 '21 13:04 vaasu070

Yeah, thanks. i will work with it issue.

YakovlevCoded avatar Apr 22 '21 14:04 YakovlevCoded

@vaasu070 I have released a test package that solves this problem. Could I ask to try it out. And if everything is OK, I will release a minor version with an edit.

YakovlevCoded avatar Apr 22 '21 16:04 YakovlevCoded

@YakovlevCoded yeah now It is working as expected. Thanks for looking into it.

vaasu070 avatar Apr 23 '21 04:04 vaasu070

@YakovlevCoded , I observed one more issue with test-react-chartjs-2 package, whenever data is updated, the chart fails to show the new data. It is always showing the initial data. This can be observed here . Hope this will be addressed soon. Thanks

vaasu070 avatar Apr 24 '21 08:04 vaasu070

@vaasu070 Thank you! I will work on it.

YakovlevCoded avatar Apr 24 '21 08:04 YakovlevCoded

@YakovlevCoded A new version 3.0.3 is rolled out, But the above bug still exists in it here. If its not too early to ask, when it will be fixed and released? Its quite critical for out project Thanks.

vaasu070 avatar Apr 29 '21 09:04 vaasu070

@vaasu070 If it's critical I'll try to finish it in the next couple of days. I'll let you know as soon as I'm done with this.

YakovlevCoded avatar Apr 29 '21 14:04 YakovlevCoded

@vaasu070 If it's critical I'll try to finish it in the next couple of days. I'll let you know as soon as I'm done with this.

Would really appreciate this!

vaasu070 avatar Apr 30 '21 04:04 vaasu070

The re-rendering still seems to be an issue on current version 3.0.3. This was not an issue in chart.js v2.9.4

When the state changes the doughnut is redrawn/reanimated. Desired behavior is to rotate the doughnut without redrawing the graph.

export default function KChart() {
  const [rotation, setRotation] = useState(0);
  const onClick = () => {
    setRotation(rotation + 20);
  };
  const data = {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [
      {
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        borderColor: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        borderWidth: 1,
      },
    ],
  };
  return <Doughnut type="doughnut" data={data} options={{ rotation, onClick }} />;
}

revroad-dev avatar May 06 '21 15:05 revroad-dev

The above happens on all chart types whether or not the state is passed to the component or not. (in other words, taking rotation out of the options objects still results in the same re-rendering behavior)

revroad-dev avatar May 06 '21 15:05 revroad-dev

I'm using the following patch as a band-aid until the issue is fixed properly. This probably does not cover all use-cases (it definitely doesn't handle datasets being removed from the chart), but it works for me with a simple bar chart that is refreshed every second:

diff --git a/node_modules/react-chartjs-2/dist/index.modern.js b/node_modules/react-chartjs-2/dist/index.modern.js
index f39264d..f8636d6 100644
--- a/node_modules/react-chartjs-2/dist/index.modern.js
+++ b/node_modules/react-chartjs-2/dist/index.modern.js
@@ -117,11 +117,14 @@ var ChartComponent = forwardRef(function (props, ref) {
     var _chart$config$data$da = chart.config.data.datasets,
         currentDataSets = _chart$config$data$da === void 0 ? [] : _chart$config$data$da;
     assign(chart.config.data, newChartData);
-    chart.config.data.datasets = newDataSets.map(function (newDataSet) {
+    newDataSets.forEach(function (newDataSet) {
       var currentDataSet = find(currentDataSets, function (d) {
         return d.label === newDataSet.label && d.type === newDataSet.type;
       });
-      if (!currentDataSet || !newDataSet.data) return newDataSet;
+      if (!currentDataSet) {
+        currentDataSet = {};
+        currentDataSets.push(currentDataSet);
+      }

       if (!currentDataSet.data) {
         currentDataSet.data = [];
@@ -130,7 +133,7 @@ var ChartComponent = forwardRef(function (props, ref) {
       }

       assign(currentDataSet.data, newDataSet.data);
-      return _extends({}, currentDataSet, newDataSet, {
+      assign(currentDataSet, newDataSet, {
         data: currentDataSet.data
       });
     });

ntamas avatar May 07 '21 20:05 ntamas

Hi @YakovlevCoded, thanks for all your hard work so far. Is this fix getting close to being released? Critical for our project.

revroad-dev avatar May 13 '21 20:05 revroad-dev

I'm having same issue. Can I ask about release time ?

maidinhkhoa92 avatar May 19 '21 16:05 maidinhkhoa92

Hi @YakovlevCoded any update on this?

vaasu070 avatar May 19 '21 16:05 vaasu070

I have also run into this issue ( I posted about it here: https://github.com/reactchartjs/react-chartjs-2/issues/678 ) The test-react-chartjs-2 branch DID fix the issue. However, there seems to be a separate issue with that branch, in that all my bars stopped drawing. It is because I was using data as a function: data: (canvas) => { return { /*...*/ } } so that I could use canvas patterns as a backgroundColor. If I change data to a normal object: data: { /*...*/ }, the bars draw fine, and the animation works.

marcusround avatar Jun 03 '21 11:06 marcusround

I have also run into this issue ( I posted about it here: #678 ) The test-react-chartjs-2 branch DID fix the issue. However, there seems to be a separate issue with that branch, in that all my bars stopped drawing. It is because I was using data as a function: data: (canvas) => { return { /*...*/ } } so that I could use canvas patterns as a backgroundColor. If I change data to a normal object: data: { /*...*/ }, the bars draw fine, and the animation works.

To add to your observation on test-react-chartjs-2 , whenever data is updated, the chart fails to show the new data. It always shows initial data. This can be observed here

vaasu070 avatar Jun 03 '21 13:06 vaasu070

To add to your observation on test-react-chartjs-2 , whenever data is updated, the chart fails to show the new data. It always shows initial data. This can be observed here

I am not sure what you mean exactly but I think its an issue with your code - see my working fork here

For my patterns issue, I have illustrated it here

marcusround avatar Jun 04 '21 07:06 marcusround

To add to your observation on test-react-chartjs-2 , whenever data is updated, the chart fails to show the new data. It always shows initial data. This can be observed here

I am not sure what you mean exactly but I think its an issue with your code - see my working fork here

For my patterns issue, I have illustrated it here

I was saying with test-react-chartjs-2 package in the code sample heres, initial data is {} and later data is changed to

{
  labels: [1, 2, 3, 4],
  datasets: [
    {
      data: [1, 2, 3, 4]
    }
  ]
}

But this is not reflected on the plot. with react-chartjs-2 it works perfectly fine.

vaasu070 avatar Jun 04 '21 08:06 vaasu070

I have a similar issue and am awaiting a conclusion.

joey-carlisle4 avatar Jun 15 '21 22:06 joey-carlisle4

I have a similar issue and am awaiting a conclusion.

For what its worth I was able to fix the issue (without introducing the patterns bug) by using the fix suggested by @ntamas in this thread.

marcusround avatar Jun 16 '21 01:06 marcusround