Chartjs-Chart.js: How to group legends of dataset? So one toggle for multiple datasets?

2👍

You can write a custom legendOnClick handler together with the filter for labels:

var labels = [];
for (let index = 0; index < 12; index++) {
  labels.push(index);
}


window.onload = function() {
  var canvas = document.getElementById('elm-chart'),
    ctx = canvas.getContext('2d');

  var myLineChart = new Chart(ctx, {
    type: 'line',
    data: {
      labels: labels,
      datasets: [{
          label: '-15',
          data: [{
              x: 0,
              y: 10,
            },
            {
              x: 1,
              y: 20,
            },
          ],
          borderColor: 'red',
        },
        {
          label: '15',
          data: [{
              x: 1,
              y: 20,
            },
            {
              x: 2,
              y: 30,
            },
          ],
          borderColor: 'blue',
        },
        {
          label: '25',
          data: [{
              x: 2,
              y: 30,
            },
            {
              x: 3,
              y: 35,
            },
          ],
          borderColor: 'yellow',
        },
        {
          label: '-15',
          data: [{
              x: 6,
              y: -10,
            },
            {
              x: 7,
              y: -20,
            },
          ],
          borderColor: 'red',
        },
        {
          label: '15',
          data: [{
              x: 7,
              y: -20,
            },
            {
              x: 8,
              y: -30,
            },
          ],
          borderColor: 'blue',
        },
        {
          label: '25',
          data: [{
              x: 8,
              y: -30,
            },
            {
              x: 9,
              y: -35,
            },
          ],
          borderColor: 'yellow',
        },
      ],

    },
    options: {
      responsive: true,
      plugins: {
        legend: {
          onClick: (evt, legendItem, legend) => {
            let newVal = !legendItem.hidden;
            legend.chart.data.datasets.forEach(dataset => {
              if (dataset.label === legendItem.text) {
                dataset.hidden = newVal
              }
            });
            legend.chart.update();
          },
          labels: {
            filter: (legendItem, chartData) => {
              let entries = chartData.datasets.map(e => e.label);
              return entries.indexOf(legendItem.text) === legendItem.datasetIndex;
            }
          }
        },
      },
      scales: {
        x: {
          type: 'linear',
          suggestedMin: labels.reduce((pv, cv) => Math.min(pv, cv)),
          suggestedMax: labels.reduce((pv, cv) => Math.max(pv, cv)),
          ticks: {
            stepSize: 30,
          },
        },
      },
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.1/chart.min.js"></script>
<canvas id="elm-chart" width="640" height="480"></canvas>

Leave a comment