score:14

Accepted answer

you can add a callback function to legends onclick:

var options = {
        animation: false,
        scales: {
            yaxes: [{
                position: "left",
                ticks: {
                    beginatzero: true
                },
                scalelabel: {
                    display: true,
                    labelstring: 'total sql queries'
                },
                id: "y-axis-0"
            }, {
                position: "right",
                ticks: {
                    beginatzero: true
                },
                scalelabel: {
                    display: true,
                    labelstring: 'slow sql queries'
                },
                id: "y-axis-1"
            }]
        },
        legend: {
        onclick: function(event, legenditem) {
            //get the index of the clicked legend
            var index = legenditem.datasetindex;
            //toggle chosen dataset's visibility
            sqlperformancechart.data.datasets[index].hidden = 
                !sqlperformancechart.data.datasets[index].hidden;
            //toggle the related labels' visibility
            sqlperformancechart.options.scales.yaxes[index].display =                 
                !sqlperformancechart.options.scales.yaxes[index].display;
            sqlperformancechart.update();
        }
        }
    }

score:1

this solution applies if you are using angular-chartjs, and if you want to apply this behaviour to all displayed charts.

if you want to skip to the code, check this fiddlejs.

you can also check this other fiddlejs to check the default angular-chartjs behaviour.


step by step:

i use the first chart example in angular-chart.js, so this will be the final result after clicking:

enter image description here

    <div ng-app="app" ng-controller="maincontroller as mainctrl">  
        <canvas id="line" class="chart chart-line" chart-data="data"
        chart-labels="labels" chart-series="series" chart-options="options"
        chart-dataset-override="datasetoverride" chart-click="onclick">
      </canvas>
    </div>
  1. replace the handler of the global chart:
chart.defaults.global.legend.onclick = function (e, legenditem) {
  var idx = legenditem.datasetindex;

  // important charts will be created in the second and third step
  var chart = charts[e.srcelement.id];
  chart.options.scales.yaxes[idx].display = !chart.options.scales.yaxes[idx].display;

  var meta = chart.getdatasetmeta(idx);
  // see controller.isdatasetvisible comment
  meta.hidden = meta.hidden === null ? !chart.data.datasets[idx].hidden : null;

  chart.update();

};

  1. create a global variable charts so we can get access each of the charts with the canvas id:
var charts = {};
  1. fill up the charts variables using the chart-create event:
angular.module("app", ["chart.js"]).controller("maincontroller", function ($scope) {
  $scope.$on('chart-create', function (event, chart) {
    charts[chart.chart.canvas.id] = chart;
  });

  $scope.labels = ["january", "february", "march", "april", "may", "june", "july"];
  $scope.series = ['series a', 'series b'];
  $scope.data = [...

i wish there would be a better way of getting a chart from the canvas id, but as far as i know this is the suggested way by the developers.

score:1

this solution applies if you are using ng2-charts with chart.js and angular 7^ and if you want to apply this behavior to all displayed charts.

    import chart from chart.js
    chart.defaults.global.legend.onclick = function (e: mouseevent, chartlegendlabelitem: chartlegendlabelitem) {
  const idx: number = chartlegendlabelitem.datasetindex;
  const chart = this.chart;
  chart.options.scales.yaxes[idx].display = !chart.options.scales.yaxes[idx].display;

  const meta = chart.getdatasetmeta(idx);
  meta.hidden = meta.hidden === null ? !chart.data.datasets[idx].hidden : null;

  chart.update();

};

or for local configuration

    legend: <chartlegendoptions>{
         onclick: function (e: mouseevent, chartlegendlabelitem:chartlegendlabelitem) {
      const idx: number = chartlegendlabelitem.datasetindex;
      const chart = this.chart;
      chart.options.scales.yaxes[idx].display = 
     !chart.options.scales.yaxes[idx].display;

      const meta = chart.getdatasetmeta(idx);
      meta.hidden = meta.hidden === null ? 
             !chart.data.datasets[idx].hidden : null;

      chart.update();

    }
}

Related Query

More Query from same tag