Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
How to add on click event to chart js

Hello I have a question concerning js charts. I have already created one in django application and i want to generate a javascript alert by clicking a certain point in the chart. How can the alert get the value of the point that i choose? For example with the value 92 like what is shown in the figure below: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This can be done with an onClick even handler as follows:

onClick: (event, elements, chart) => {
  if (elements[0]) {            
     const i = elements[0].index;
     alert(chart.data.labels[i] + ': ' + chart.data.datasets[0].data[i]);
  }
}

Please take a look at below runnable code and see how it works.

new Chart('myChart', {
  type: 'line',
  data: {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: '# of Votes',
      data: [65, 59, 80, 81, 56, 55, 40],
      borderColor: '#a00'
    }]
  },
  options: {
    onClick: (event, elements, chart) => {
      if (elements[0]) {            
         const i = elements[0].index;
         alert(chart.data.labels[i] + ': ' + chart.data.datasets[0].data[i]);
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<canvas id="myChart" width="400" height="95"></canvas>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!