I am using chart.js version 2.8.0. I want to add a click event on any data point to drill down to more details of that datapoint. I added following under options of a line chart:
options: {
events: ['click', 'mousemove', 'touchstart', 'touchmove'],
onClick: handleClick
}
Then I have a js function:
function handleClick(evt, legendItem, index, con) {
debugger;
var activeElement = getTrafficTypeChartId().getElementAtEvent(evt);
var ar = $('#datePicker').val().split('/');
reloadChart(ar.reverse().join(''), activeElement[0]._chart.data.labels[activeElement[0]._index].substring(0, 2));
}
The click event is registered for the entire chart. So what is happening is that when I click legend of the chart, The legend is not sticking out and corresponding effect is not coming on the chart. So the legend has become non-useful.
I tried to add function on the legend: the property itself but that does not get executed, only the handler under options:
get executed:
https://www.chartjs.org/docs/2.7.2/configuration/legend.html?h=onclick
How do I make the legend click work as usual and still have custom functionality on datapoint click?