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

183
Views
Detect if the click is in bar or outside of bar in ChartJS

I am working with chartJS bar graph. I want to detect the location of click. How can I detect if graph was clicked in bar or outside the bar region in chartJS?

const waterFChart = new Chart(canvasRef.current, {
    plugins: [ChartDataLabels],
    type: "bar",
    data: {
        labels: ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"],
        datasets: [
            {
                label: 'Failures',
                type: "bar",
                data: [10, 20, 30, 41, 51],
                backgroundColor:"green",
                hoverBackgroundColor: darkBar,
            }
                  ]
           }

          options:{
             onClick: function (e) {
                  if(clicked on bar){
                    {alert("Clicked on bar")
                   }
                  else {
                    alert("Clicked outside of the bar");
                  }
                },
          );
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You get an array containing all the active elements as second argument so if this array has a length of 0 no bar has been clicked and if it has a length greater then 0 a bar has been clicked:

options: {
  onClick: function (evt, activeEls) {
    if (activeEls.length === 0) { 
      alert("clicked outside of bar");
    } else {
      alert("clicked on bar");
    }
  }
}
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!