[Chartjs]-Chart JS โ€“ Remember hidden label state after page refresh

-1๐Ÿ‘

Iโ€™m using Chart.js v2.9.3

I guess you already know how to deal with cookies, here is the code that overwrites the default function that gives you an index that you can store in a cookie for further use.

onClick(e, legendItem) {
    // Get the index
    const index = legendItem.datasetIndex;
    //index alert
    alert("dataset index " + index + " was clicked")

    // this table object
    const ci = this.chart;

    // show/hide dataset
    var meta = ci.getDatasetMeta(index);
    meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;

    // We hid a dataset ... rerender the chart
    ci.update();
},

Copy paste this code inside options > legend: {Here}

Finally, here is a jsFiddle live example.
https://jsfiddle.net/pt0s95r7/1/

Leave a comment