question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onClick for inside the chart, not the whole canvas

See original GitHub issue

Expected Behavior

I expect that if i use chart.options.onClick, that the onclick functon works on the chart, and not the whole canvas

Current Behavior

At this moment the onClick responds on the whole canvas. Alsof if you click on the legend.

Possible Solution

Maybe a seperate function to check if there has been click in the chart box, instead the whole canvas. Because the chart onClick function also responds if there is a onClick function on the Legend.

Steps to Reproduce (for bugs)

Create a chart and

chartTemplate.options.onClick = function () {
  console.log('Click on chart');
};

chartTemplate.options.legend.onClick = function () {
  console.log('Chart click on legend?');
};

Context

I want different to use different functions for:

  • Chart (box) onClick
  • Legend onClick (options.legend.onClick)
  • Chart Element onClick (getElementAtEvent)

So if someone clicks on the Legend, something happens. If someone clicks on an element of the chart, specific things happen for the data behind that point/element If someone clicks on the chart, the chart resizes to a bigger size for a better view / get smaller again.

A this moment, using config.legend.onClick and config.onClick both, alsways fires config.onClick when you click on the legend.

Environment

  • Chart.js version: 2.7.2
  • Browser name and version: Chrome

If there is any way to get the onClick only on the box of the chart (axes and everything inside), please let me know! Couldn’t figure it out myself

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
gkemp94commented, Feb 4, 2019

This might only help a little, but I needed something similar and have this piece of code which works for me.

        onClick: function (event) {
          const { left, right, top, bottom} = this.chart.chartArea;
          if (
            event.offsetX > left && 
            event.offsetX < right &&
            event.offsetY > top && 
            event.offsetY < bottom
           ) {
            // dosomething()
          }
        }

You’ll need to extend that to include the axis areas if you need them. You can access them through this.chart.boxes I believe. Hope that helps.

0reactions
Amiable-Syedcommented, Apr 9, 2021

Added my answer for this issue, I need a click on area filled part of each chart and also wanted legend title, https://stackoverflow.com/questions/66570715/chart-js-line-chart-with-fill-on-click-with-full-information-of-its-legend-text

Read more comments on GitHub >

github_iconTop Results From Across the Web

Click events on Pie Charts in Chart.js - Stack Overflow
canvas.onclick = function(evt){ var activePoints = myLineChart. ... It triggers on the entire chart, but if you click on a pie the model...
Read more >
How to Toggle Single Chart Onclick To Full Screen ... - YouTube
How to Toggle Single Chart Onclick To Full Screen Mobile Responsive in Chart JSIn this video we will explore how to toggle single...
Read more >
How to Show Onclick Text In Center of Doughnut Chart in ...
The canvas tag, javascript, arrays and Chart JS all need to be combined to draw an eye catching bar chart or line chart....
Read more >
click - Sets Click Event Handler for Data Series - CanvasJS.com
Sets the click event handler for dataSeries which is triggered when user clicks on a dataSeries. Upon event, a parameter that contains event...
Read more >
Canvas API - MDN Web Docs
The Canvas API provides a means for drawing graphics via JavaScript ... The Canvas API is extremely powerful, but not always simple to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found