Chartjs-How to remove only one specific dataset label chartJS?

1πŸ‘

βœ…

In the options, add the following:

options: {
   legend: {
      labels: {
         filter: function(label) {
            if (label.text == 'My first dataset') return true;
          }
       }
    }
 }

See the following snippet:

var config = {
            type: 'line',
            data: {
                labels: ["January", "February", "March", "April", "May", "June", "July"],
                datasets: [{
                    label: "My first dataset",
                    data: [65, 0, 80, 81, 56, 85, 40],
                    fill: false
                }, {
                    label: "My second dataset",
                    data: [60, 20, 50, 41, 36, 25, 80],
                    fill: false
                }]
            },
            options: {
                legend: {
                    labels: {
                        filter: function(label) {
                            if (label.text == 'My first dataset') return true;
                        }
                    }
                }
            }
        };

        var ctx = document.getElementById("myChart").getContext("2d");
        new Chart(ctx, config);
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.js"></script>

Leave a comment