emn178 / chartjs-plugin-labels

Plugin for Chart.js to display percentage, value or label in Pie or Doughnut.
MIT License
612 stars 219 forks source link

How to make plugin not be global? #121

Open babakir opened 4 years ago

babakir commented 4 years ago

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 commented 4 years ago

Have the same issue, thanks for workaround

BrotherZach commented 3 years ago

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 commented 2 years ago

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 commented 1 year ago

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