I wanted to allow the user to select from 100+ datasets, but instead of adding / removing them from my chart, I decided to set showLine: false for any dataset I want to hide. Unfortunately, the default legend will show all 100+. Therefore, in my solution, I generate the legend manually by filtering out any dataset that showLine: false has showLine: false .
Your settings will have this:
legend: { labels: { generateLabels: (a) => { return a.data.labels } }
And you will create your own labels using a helper function:
function updateAllLabels() { const myNewLabels = []; myChart.data.datasets.forEach((element) => { if (element.showLine) { myNewLabels.push(generateLabel(element)); } }); myChart.data.labels = myNewLabels; }
And you will generate a label with another function:
function generateLabel(data) { return { fillStyle: data.borderColor, lineWidth: 1, strokeStyle: data.borderColor, text: data.countyName,
Now just remember to call the function when updating the chart:
updateAllLabels(); myChart.update();
Have a nice schedule!
Boris Yakubchik May 23 '19 at 5:54 a.m. 2019-05-23 05:54
source share