Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Bar Chart with multiple dataset stacks reanimating when data changes #1233

Open
1 task
MigdaliaBrito opened this issue Nov 5, 2024 · 6 comments
Open
1 task
Labels

Comments

@MigdaliaBrito
Copy link

MigdaliaBrito commented Nov 5, 2024

Would you like to work on a fix?

  • Check this if you would like to implement a PR, we are more than happy to help you go through the process.

Current and expected behavior

I see this only occur with bar charts with data sets that have multiple stack ids. But when the data changes, it does not animate gracefully the difference for the stacks. It does for the first stack collection, but the rest it resets the animation. I found an example in the chartjs documentation that does what I expect it to do but when using react-chartjs-2 it does not replicate the expected behavior.

https://www.chartjs.org/docs/latest/samples/bar/stacked-groups.html

Reproduction

https://codesandbox.io/p/devbox/silly-aj-gqvwvr?file=%2FApp.tsx%3A60%2C16&workspaceId=d2c104b9-4ce0-406f-9621-548a17948260

chart.js version

4.0.0

react-chartjs-2 version

5.0.0

Possible solution

No response

@rafrocha
Copy link

rafrocha commented Nov 5, 2024

+1

@MigdaliaBrito
Copy link
Author

Some additional context around the use case:
We have a component that changes data inside of the dataset. Its just a state update. I would think this to be handled by the library.

@MigdaliaBrito
Copy link
Author

In the codesandbox I modified it to do this:

  const handleClick = () => {
    const chart = chartRef.current;

    if (chart) {
      chart.data.datasets.forEach((dataset) => {
        dataset.data = labels.map(() => Math.floor(Math.random() * 201) - 100);
      });
      chart.update();
    }
  };

Updating the data directly seems to prevent this but I am thinking react-chartjs-2 should be smart enough to handle state changes and change appropriately.

@MigdaliaBrito
Copy link
Author

Hmm after further investigation if I just modify the data inside the dataset (not through chart update) then it behaves as expected. Perhaps this is not a library issue just how the data is architected to be passed down to the chart. Also my code link has been modified to include the version where the data changes smoothly

@MigdaliaBrito
Copy link
Author

I believe the issue we face likely can be solved as it was solved before here: #648

I think perhaps the addition of two stacks confuses the library?

@MigdaliaBrito
Copy link
Author

Original sandbox example with the animation bounce: https://codesandbox.io/p/devbox/green-wood-sxw48y?workspaceId=d2c104b9-4ce0-406f-9621-548a17948260

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants