chartjs-plugin-labels icon indicating copy to clipboard operation
chartjs-plugin-labels copied to clipboard

How to make plugin not be global?

Open babakir opened this issue 5 years ago • 4 comments

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

babakir avatar Feb 22 '20 09:02 babakir

Have the same issue, thanks for workaround

maranmaran avatar Feb 23 '20 20:02 maranmaran

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?

BrotherZach avatar May 07 '21 13:05 BrotherZach

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

jernchr11 avatar Jul 11 '21 02:07 jernchr11

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], ...```

alvarouribe avatar Mar 20 '23 23:03 alvarouribe