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

爷,独闯天下 提交于 2019-12-29 06:34:27

问题


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

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


回答1:


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




回答2:


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,
    }];
  });


来源:https://stackoverflow.com/questions/36846177/chart-js-how-to-set-a-line-chart-dataset-as-disabled-on-load

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!