How to remove only one specific dataset label chartJS?

I have two datasets in datasets array and they have labels like My first dataset and My second dataset.I only want to show My first dataset label.I researched on the internet and only could find how to remove all or not.

enter image description here

Here jsFiddle : http://jsfiddle.net/yj6mqu4r/

Upvotes: 0

Views: 498

Answers (1)

Philip F.
Philip F.

Reputation: 1237

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/[email protected]/dist/Chart.js"></script>

Upvotes: 1

Related Questions