Chartjs-How to access the hooks of the Chartjs plugins?

0👍

You can’t access the plugin hooks from a specific plugin within the config of that plugin, you will need to create your own custom plugin and in the plugin itself you can access all the hooks:

const customPlugin = {
  id: 'customPlugin',
  afterDatasetsDraw: (chart, args, opts) => {
    console.log('afterDatasetsDraw')
  },
  beforeDatasetsDraw: (chart, args, opts) => {
    console.log('beforeDatasetsDraw');
  }
  // You can add all the other hooks here
}

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange'
      }
    ]
  },
  options: {},
  plugins: [customPlugin]
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.js"></script>

<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>

Leave a comment