1👍
✅
Chart.js has an example for a html legend on their sample page: https://www.chartjs.org/docs/master/samples/legend/html.html
li.onclick = () => {
const {type} = chart.config;
if (type === 'pie' || type === 'doughnut') {
// Pie and doughnut charts only have a single dataset and visibility is per item
chart.toggleDataVisibility(item.index);
} else {
chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
}
chart.update();
};
The chart
and item
here are argument in the plugin callback so if you are implementing it yourself without use of the plugin you will need to reference your own chart object and manage which index you clicked and then call the setDatasetVisibility
or toggleDataVisibility
for that index
Source:stackexchange.com