Chart.js - How to set a line chart dataset as disabled on load

Yes, there is a "hidden" flag in ChartJS. eg.

data:
{
        datasets: [
        {
            data: [1,2,3],
            label: 'My First Dataset',
            hidden: true,
        },
        ],
}

See this issue on GitHub: https://github.com/chartjs/Chart.js/issues/689


The accepted solution has the downside, that hiding/unhiding signals might sometimes fail after initializing the chart like that.

It might be a better idea to change it in the current metadata of the dataSet, which holds the actual data that is used by the chart:

chart.data.datasets.forEach((dataSet, i) => {
  var meta = chart.getDatasetMeta(i);

  meta.hidden = (<your-condition-here>);
});

this.chart.update();

Tags:

Chart.Js