Chartjs-How to unregister chartjs-plugin-labels globally?

0👍

chartjs-plugin-labels is registered with id: 'labels' as you can see inside chartjs-plugin-labels.js on GitHub.

Therefore, I thought, it should be possible to unregister it using the following command:

Chart.plugins.unregister('labels');

Since this doesn’t work, you can remove the plugin from the registered plugin array as follows:

const plugins = Chart.plugins.getAll();
const labelsPlugin = plugins.find(p => p.id == 'labels');
plugins.splice(plugins.indexOf(labelsPlugin), 1);

Please take a look at the runnable code snippet below:

const plugins = Chart.plugins.getAll();
const labelsPlugin = plugins.find(p => p.id == 'labels');
plugins.splice(plugins.indexOf(labelsPlugin), 1);

new Chart('chart', {
  type: 'pie',
  data: {
    labels: ['A', 'B'],
    datasets: [{
      data: [20, 40],
      backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)"],
      borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)"],
    }],
  },
  options: {
    responsive: true
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js"></script>
<canvas id="chart" height="80"></canvas>

Leave a comment