I am making a pie chart with Chart.js 3 and want to call a function when someone clicks on a slice of the pie. Of course, I'd like to identify which data point was clicked and use that in the function. I can't seem to find a good explanation in the docs of how to do it in Chart.js V3. I have searched here and see many solutions for V1 and V2, but not V3. Any help would be greatly appreciated.
chart = new Chart(thecomplexeschart, {
type: 'pie',
data: {
labels: Object.keys(dataset),
datasets: [{
label: label,
data: Object.values(dataset),
hoverOffset: 4,
backgroundColor: makeRandomColorsString(Object.keys(dataset).length),
}]
},
options: {
onClick: (e) => {
const canvasPosition = Chart.helpers.getRelativePosition(e, chart);
},
interaction: {
mode: 'index'
},
responsive: false,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
title: {
font: {
size: 20,
weight: 'bolder'
},
display: true,
text: label,
padding: {
top: 10,
bottom: 30
}
}
}
}
}
);