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

How to make plugin not be global? #121

Open
babakir opened this issue Feb 22, 2020 · 4 comments
Open

How to make plugin not be global? #121

babakir opened this issue Feb 22, 2020 · 4 comments

Comments

@babakir
Copy link

babakir commented Feb 22, 2020

I have many chart in any type in my application and I'm using chartjs-plugin-labels to write label on pie chartjs but that affect other charts, I can use of plugins: {labels: false} and its work but I need to a way that to make plugin private just to specific charts.
I'm using of this in angular8
import * as Chart from 'chart.js';
import 'chartjs-plugin-labels';
How can I do it?
thanks

@maranmaran
Copy link

Have the same issue, thanks for workaround

@BrotherZach
Copy link

Having a similar issue. I am unable to disable the plugin on other charts on the page with plugins: false. I have also tried:
plugins: {
labels: {
render: false
}
}
I also tried plugins: {labels: false} but that breaks the page.
What is the recommended way to disable the plugin for individual charts?

@jernchr11
Copy link

jernchr11 commented Jul 11, 2021

I had the same problem. If you look at the source code in chartjs-plugin-labels.js, the plugin is registered as global:

Chart.plugins.register(/* plugin object */);

I copied the file and modified the source code to return the plugin rather than registering it globally. Then when creating the chart, I passed in the plugin. See more about passing a plugin directly to a chart here: https://www.chartjs.org/docs/2.9.4/developers/plugins.html.

Attached the updated code to just return and export the plugin object rather than globally registering it:

labels_plugin.txt

@alvarouribe
Copy link

alvarouribe commented Mar 20, 2023

I fixed my issue.

TypeError: Cannot read property 'x' of null

ChartDataLabels plugin was registered globally in another chart that I had. instead I register locally and that fixed my issue. Docs

basically I just had to remove this line in all my chart definitions

// Register the plugin to all charts:
Chart.register(ChartDataLabels);

instead I just need it the ChartDataLabels in my plugins object:

  plugins: [ChartDataLabels], ...```

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

5 participants