[Chartjs]-Click event of stacked line chart not working

0👍

If I’m understanding what you are wanting correctly then this should work for you.

document.getElementById("myChart").onclick = function(evt) {
    var active = myChart.getElementAtEvent(event)[0]._index;
    var value = myChart.getElementAtEvent(event)[0]._chart.config.data.labels[active];
    alert(value);
};

Active simply finds the index of where you clicked and then it’s used at the end of value to find where in your labels that item correlates to.

If this is incorrect in what you want please let me know.

Edit 1

Based on wanting to click anywhere I took it as anywhere within the alley from where the point is to the next. You can try the following:

document.getElementById("myChart").onclick = function(evt, array) { 
    var xLabel = myChart.scales['x-axis-0'].getValueForPixel(evt.x);    
    alert(myChart.config.data.labels[xLabel - 1]);
};

Leave a comment