score:1

Accepted answer

you can use the filter on the labels of the legend to filter out the datasets you dont want to show in the legend, see example:

var options = {
  type: 'bar',
  data: {
    labels: ["red", "blue", "yellow", "green", "purple", "orange"],
    datasets: [{
        label: '# of votes',
        data: [12, 19, 3, 5, 2, 3],
        borderwidth: 1,
        backgroundcolor: 'red'
      },
      {
        label: '# of points',
        data: [7, 11, 5, 8, 3, 7],
        borderwidth: 1,
        backgroundcolor: 'blue'
      },
      {
        label: 'dont show this',
        data: [17, 1, 15, 18, 13, 17],
        borderwidth: 1,
        backgroundcolor: 'orange'
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        labels: {
          filter: (legenditem, chartdata) => (legenditem.text !== 'dont show this')
        }
      }
    }
  }
}

var ctx = document.getelementbyid('chartjscontainer').getcontext('2d');
new chart(ctx, options);
<body>
  <canvas id="chartjscontainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.3.2/chart.js"></script>
</body>

documentation: https://www.chartjs.org/docs/master/configuration/legend.html#legend-label-configuration


Related Query

More Query from same tag