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

Problem using chartjs-plugin-datalabels (and recommended fix) #118

Open
Whobeu opened this issue Sep 10, 2022 · 2 comments
Open

Problem using chartjs-plugin-datalabels (and recommended fix) #118

Whobeu opened this issue Sep 10, 2022 · 2 comments

Comments

@Whobeu
Copy link

Whobeu commented Sep 10, 2022

I am current using using Chart.js 3.9.1. I was having a problem with chartjs-plugin-datalabels not working correctly after an initial call of 'new ChartJSNodeCanvas'. Digging through the chartjs-plugin-datalabels code I found there was an issue with 'instanceof' not working correctly on the subsequent calls. In my code, every call to my graphing method called 'new ChartJSNodeCanvas'. I was able to work around the issue by making a single global call to 'new ChartJSNodeCanvas' in my module and reusing it. It also had the side benefit of reducing the overhead of creating new canvases.

I reported the issue to the chartjs-plugin-datalabels maintainer and I was pointed to a similar issue at chartjs/chartjs-plugin-datalabels#251. Reading through the thread it seems there is a minor change needed to the Chart.js require statement to fix the issue. I edited line 149 of the chartjs-node-canvas index.js and changed

const chartJs = require('chart.js');

to

const chartJs = require('chart.js/auto');

With this change, my original code works fine. I will probably leave my code with the single global call as it does reduce the overhead of creating multiple canvases. But this change to the require statement appears to be something needed with the 3.x.x Chart.js to register the controllers, elements, scales and plugins you are going to use, as documented at https://www.chartjs.org/docs/latest/getting-started/integration.html.

@mj-mueller
Copy link

I'm running into the same issue and can confirm that above fix solves it. Would be great to have this fix included soon.

@askdesigners
Copy link

Yep this is the fix. Can we get a PR? 🙏🏻

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

3 participants