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.

Get point index from chart.getPointsAtEvent(e)

See original GitHub issue

Hi, I am looking to make my chart a little more interactive and I would like to get a point’s index (of its dataset) with the following code:

canvas.onclick = function(e) {
  const points = chart.getPointsAtEvent(e);
  // something like `point.getIndex()` would be great so that I know where this point is in the original dataset
};

Anybody have a good solution for this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
imdocommented, Apr 7, 2018

I made a workaround to get data from the clicked point; As we know, the hover event fires before the event click, so I suggest the following solution

// declare a global variable
var selectedPoint;
// add the code below to your chartjs options
options: {
    .....,
    tooltips: {
        mode: 'single',
        callbacks: {
            afterBody: (data) => {
                if(data && data[0])
                selectedPoint = data[0];
                return [''];
            }
        }
}
// here you can use the global variable
canvas.onclick = function(e) {
   if(selectedPoint){
         // retrieve the data directly from selected point
         console.log("xLabel : "+selectedPoint.xLabel +" ,yLabel : "+selectedPoint.yLabel);
         // you can also retrieve the data && datasets
        console.log("datasets",myChart.config.data.datasets[selectedPoint.datasetIndex]);
console.log("data",myChart.config.data.datasets[selectedPoint.datasetIndex].data[selectedPoint.index]);
   }
};
2reactions
sohampshettycommented, Mar 2, 2017

@tyrex1975 It’s not working for me, an error Uncaught TypeError: Cannot read property 'currentDevicePixelRatio' of undefined not leaving me alone.

var activePoints = lineChart.getElementsAtEvent(evt);
var mouse_position = Chart.helpers.getRelativePosition(evt); 

activePoints = $.grep(activePoints, function(activePoint, index) {
	var leftX = activePoint.x - 5,
		rightX = activePoint.x + 5,
		topY = activePoint.y + 5,
		bottomY = activePoint.y - 5;

	return mouse_position.x >= leftX && mouse_position.x <=rightX && 
                        mouse_position.y >= bottomY && mouse_position.y <= topY;
});

console.log(activePoints[0]);
console.log('activePoints[0]----');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js: Get point index from chart.getPointsAtEvent(e)
js) a little more interactive and I would like to get a point's index (of its dataset) with the following code: canvas.onclick =...
Read more >
[Solved]-Chart.js: Get point index from chart.getPointsAtEvent(e)
returns points from all datasets. So the same code would work irrespective of how many datasets you have or which of the datasets...
Read more >
Chart.js: Get point index from chart.getPointsAtEvent(e)
I am looking to make my chart (made with Chart.js) a little more interactive and I would like to get a point's index...
Read more >
How to Extract the Data Index of a Dataset when ... - YouTube
How to Extract the Data Index of a Dataset when Hover on Tooltip in Chart JSExtracting the data index and the dataset index...
Read more >
How to Display Clicked Data Points in the Table in Chart JS
Sometimes you might want to extract the data points and insert them in a table. We will make a small interactive item where...
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