In chartjs to hide certain dataset in legends - Javascript Chart.js

Javascript examples for Chart.js:Legend

Description

In chartjs to hide certain dataset in legends

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>hide certain dataset legends</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from w w w.j ava 2s.  c  o m
const ctx = document.getElementById('chart');
const data = [1, 2, 3, 4, 5, 6, 7, 8];
const chart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: 'Legend 1',
      data: [12, 19, 3, 5, 2, 3],
    }, {
      label: 'Legend 2',
      data: [11, 12, 33, 4, 2, 3],
    }, {
      label: 'Legend 3',
      data: [12, 19, 3, 5, 2, 3],
    }]
  },
  options: {
    legend: {
      labels: {
        filter: function(item, chart) {
          // Logic to remove a particular legend item goes here
          return !item.text.includes('Legend 1');
        }
      }
    }
  }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="chart" width="400" height="400"></canvas>  
   </body>
</html>

Related Tutorials