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

TypeError after unmounting chart #177

Open
Robloche opened this issue Nov 15, 2022 · 22 comments
Open

TypeError after unmounting chart #177

Robloche opened this issue Nov 15, 2022 · 22 comments

Comments

@Robloche
Copy link

Robloche commented Nov 15, 2022

I have a <Line> chart in my React app.
Since I updated chart.js to v4.0.1, I get the following error when I unmount my chart:

Uncaught TypeError: Cannot set properties of null (setting '_setStyle')
    at chartjs-plugin-streaming.esm.js:787:1
    at each (helpers.core.ts:149:1)
    at Chart.update (chartjs-plugin-streaming.esm.js:785:1)
    at chartjs-plugin-streaming.esm.js:601:1
    at callback (helpers.core.ts:109:1)
    at chartjs-plugin-streaming.esm.js:68:1

This error is triggered repeatedly and infinitely. It seems that some kind of timer has not been cleared.

Here's my code:

<Line
  data={{datasets}}
  options={{
    scales: {
      x: {
        display: false,
        realtime: {
          delay: 1000,
          onRefresh: (chart) => {
            const x = Date.now();
            const y = getY();
            chart.data.datasets.forEach((dataset, i) => {
              dataset.data.push({
                x,
                y: y[i]
              });
            });
          }
        },
        type: 'realtime'
      },
      y: yOptions
    }
  }} />
@Robloche
Copy link
Author

Is this project even maintained?

@Dola97
Copy link

Dola97 commented Dec 9, 2022

Did You Find Any Solution @Robloche

@Robloche
Copy link
Author

Robloche commented Dec 9, 2022

Unfortunately, no.
I'd like to give a try at fixing it myself but I'm afraid there would be nobody to approve my PR... 😔

@Dola97
Copy link

Dola97 commented Dec 9, 2022

I find a temporary solution just downgrade the chart js version and it will work

@Robloche
Copy link
Author

Robloche commented Dec 9, 2022

Yes, that's obviously what I did. But that's not sustainable.

@Robloche
Copy link
Author

Looking at the code of this plugin and chartjs, it seems that the plugin uses a deprecated event to clean everything: destroy.
From Chartjs' doc, it appears the correct event is now: afterDestroy.

@Robloche
Copy link
Author

@Dola97 If you're still interested, I forked the project and fixed this issue. I also bumped all dependencies versions.

https://www.npmjs.com/package/@robloche/chartjs-plugin-streaming

@Dola97
Copy link

Dola97 commented Dec 20, 2022

Thanks Appreciate it 😍😍

@SID9-HoneyBee
Copy link

SID9-HoneyBee commented Jan 3, 2023

@Robloche
You are referring to the 3.0.4 version you are working on.

could you please help me?

Even the 2.0.0 version has the same problem.

I made a sample using simple chart.js and chartjs-plugin-streaming.

When I hover over the chart, I get Cannot read properties 'getDatasetMeta' error.

Have you ever had this experience?

This happens when using the latest version(4.*) of chart.js .

@Robloche
Copy link
Author

Robloche commented Jan 4, 2023

@Robloche You are referring to the 3.0.4 version you are working on.
could you please help me?
Yes. Since this project does not seem to be maintained any longer, I forked it and fixed the destroy issue.

Even the 2.0.0 version has the same problem.
I made a sample using simple chart.js and chartjs-plugin-streaming.
When I hover over the chart, I get Cannot read properties 'getDatasetMeta' error.
Have you ever had this experience?
Nope, sorry. I don't use this feature.

If you want to give it a try, fix it and create a PR on my forked project.

@SID9-HoneyBee
Copy link

@Robloche
Thank you for answer. I don't have the experience of forking a project and modifying and applying it yet. But I'll give it a try with your 3.0.4 version.

@Robloche
Copy link
Author

Robloche commented Jan 4, 2023

You don't have to fork it.
Just clone my forked project, fix your issue, and submit a pull request.
Or at least, tell me what to change and I'll do the PR myself.

@SID9-HoneyBee
Copy link

@Robloche
I forked your project.

I am using chart.js 4.1.1 version and also using vue-chartjs.

Your repo has a closed issues section.

So we communicate here.

The realtime chart works fine, but the tooltip doesn't appear when I hover over it, and it says getDatasetMeta is undefined.

And I reproduced and corrected the problem.

Can I add a branch and request a PR?

@Robloche
Copy link
Author

Robloche commented Jan 4, 2023

Sure.

@Robloche
Copy link
Author

Robloche commented Jan 4, 2023

(I opened the "issues" section, btw.)

@jsteenkamp
Copy link

@Robloche - thanks for fixing this issue with your fork.

There are 2 PRs on your fork that fix tooltips - without tooltip fix installing your fork fixes the unmount problem but breaks ChartJS tooltips.

@Robloche
Copy link
Author

Robloche commented Mar 9, 2023

For some reason, I wasn't notified of these waiting PR...
I merged yours, thanks!

@Robloche Robloche closed this as completed Mar 9, 2023
@Robloche Robloche reopened this Mar 9, 2023
@jsteenkamp
Copy link

@Robloche many thanks

@Robloche
Copy link
Author

Robloche commented Mar 9, 2023

I've just released v3.1.0 which includes your fix.

@kogratte
Copy link

If like me you don't like to use something else than the official package (which seems unmaintained, Robloche is right), you can fix this issue with something like that:

const chart = ref<LineController | null>(null);

onBeforeUnmount(() => {
    if (chart.value !== null) {
        chart.value.chart.notifyPlugins('destroy');
        chart.value.chart.destroy();
    }
});

@thangnq1007
Copy link

For now, we were using Chartjs 3.9.1 and it still has that problem. @Robloche can you help me?

@Robloche
Copy link
Author

For now, we were using Chartjs 3.9.1 and it still has that problem. @Robloche can you help me?

I'm sorry but I don't work on this project. I simply forked it to make fix a bug for my own use.
I suggest you do the same if you really need something to be done.

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

No branches or pull requests

6 participants