score:-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/


Related Query