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

chart.js

chart.js Problem Overview


Using chart.js v2, is it possible to mark a dataset in a line chart as being disabled on initial load?

enter image description here

Didn't find an option for it in the documentation.

chart.js Solutions


Solution 1 - chart.js

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

Solution 2 - chart.js

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();

Solution 3 - chart.js

If you are using angular-chartjs, then you can add the properties of the dataset in the chart-dataset-override property:

For example:

HTML:

<div class="container" ng-app="app" ng-controller="ChartCtrl">
  <canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series" chart-dataset-override="datasetOverride">
  </canvas>
</div>

Javascript:

Chart.defaults.global.legend.display = true;

angular.module("app", ["chart.js"])
  .controller("ChartCtrl", function($scope) {

    $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
    $scope.series = ['Series A', 'Series B'];

    $scope.data = [
      [65, 59, 80, 81, 56, 55, 40],
      [28, 48, 40, 19, 86, 27, 90]
    ];

    $scope.datasetOverride = [{}, {
      hidden: true,
    }];
  });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul SkarsethView Question on Stackoverflow
Solution 1 - chart.jsRoryView Answer on Stackoverflow
Solution 2 - chart.jsThyrazView Answer on Stackoverflow
Solution 3 - chart.jstoto_ticoView Answer on Stackoverflow