onlinecode

Handling onClick Events on ChartJS Bar Charts

Handling onClick Events on ChartJS Bar Charts

Handling onClick Events on ChartJS Bar Charts

In this post, we will give you information about Handling onClick Events on ChartJS Bar Charts. Here we will give you detail about Handling onClick Events on ChartJS Bar Charts And how to use it also give you a demo for it if it is necessary.

Detecting what bar on a bar chart the user clicked is easy, but the API for doing so has changed several times between major ChartJS releases.
For ChartJS 4.x, you need to attach an onclick handler to the chart’s canvas and use the getElementsForEventAtNode() method as follows.
This method returns an object which contains an index property that tells you which index in your labels was clicked.

const canvas = document.getElementById('chart');
const ctx = canvas.getContext('2d');
const chart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['A', 'B', 'C'],
    datasets: [{
      label: 'Easy as',
      data: [1, 2, 3],
    }],
  },
  options: {
    responsive: true
  }
});

// Make sure to attach 'onclick' to the canvas, **not** the chart instance
canvas.onclick = (evt) => {
  const res = chart.getElementsAtEventForMode(
    evt,
    'nearest',
    { intersect: true },
    true
  );
  // If didn't click on a bar, 'res' will be an empty array
  if (res.length === 0) {
    return;
  }
  // Alerts "You clicked on A" if you click the "A" chart
  alert('You clicked on ' + chart.data.labels[res[0].index]);
};

Below is a live example.
Click on “A” to see a You clicked on A alert.

In ChartJS 2 and 3 for Handling onClick Events on ChartJS Bar Charts

The only difference with ChartJS 2.x and 3.x is you need to use the getElementAtEvent() method, not getElementsAtEventForMode().
The getElementAtEvent() method is slightly more concise.

// The following only works with ChartJS 2.x or 3.x.
canvas.onclick = (evt) => {
  const res = chart.getElementAtEvent(evt);
  if (res.length === 0) {
    return;
  }
  alert('You clicked on ' + chart.data.labels[res[0]._index]);
};

 

Chart.js is a free, open-source JavaScript library for creating interactive charts. It is used by millions of developers worldwide to create beautiful and informative charts for their websites and applications.

Chart.js is easy to use and has a wide range of features, including:

Chart.js is a powerful tool that can be used to create a wide variety of charts. It is a great choice for developers who want to create beautiful and informative charts for their websites and applications.

Here are some of the benefits of using Chart.js:

If you are looking for a powerful and easy-to-use JavaScript library for creating interactive charts, then Chart.js is a great option.

Here are some examples of charts that can be created with Chart.js:

Chart.js is a powerful tool that can be used to create a wide variety of charts. It is a great choice for developers who want to create beautiful and informative charts for their websites and applications(Handling onClick Events on ChartJS Bar Charts).

Hope this code and post will helped you for implement Handling onClick Events on ChartJS Bar Charts – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version