[Chartjs]-Chart.js turn off all y axis and toggle them on / off

1πŸ‘

βœ…

At a first glance, your code looks fine to me and I expected it to work just fine. After some debugging however, I found out that chart.getDatasetMeta(i).hidden is null when the dataset is hidden and false, when it is visible. Therefore, the beforeLayout callback function should be changed as follows to make it work.

chartPlugins = [{
  beforeLayout: chart =>
    chart.data.datasets.forEach((ds, i) => {
      let hidden = chart.getDatasetMeta(i).hidden !== false;
      chart.config.options.scales.yAxes[i].display = !hidden;
    })
}];

Please take a look at your amended StackBlitz.

Leave a comment