Chart.js to strike label text - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Chart.js to strike label text

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>Chart.js Hide Dataset After Chart Initialization</title> 
   </head> 
   <body translate="no"> 
      <div id="canvas-holder" style="width: 50%;"> 
         <h1>
            Hiding Dataset in /*from w  w w. j a v a 2  s  . c  om*/
            <span id="timer">5 seconds</span>
         </h1>
         <h1> 
            <canvas id="canvas"></canvas> 
         </h1>
      </div> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> 
      <script>

var chartColors = {
   red: 'rgb(255, 99, 132)',
   orange: 'rgb(255, 159, 64)',
   yellow: 'rgb(255, 205, 86)',
   green: 'rgb(75, 192, 192)',
   blue: 'rgb(54, 162, 235)',
   purple: 'rgb(153, 102, 255)',
   grey: 'rgb(231,233,237)'
};

var config = {
  type: 'line',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First Dataset",
      backgroundColor: chartColors.red,
      borderColor: chartColors.red,
      data: [5, 10, 25, 15, 10, 20, 30],
      fill: false,
    }, {
      label: "My Second Dataset",
      fill: false,
      backgroundColor: chartColors.blue,
      borderColor: chartColors.blue,
      data: [5, 0, 12, 5, 25, 35, 15],
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'Chart.js Hide Dataset Matching "My Seconds Dataset" After 5 Seconds'
    },
    tooltips: {
      mode: 'index',
      intersect: false,
    },
    hover: {
      mode: 'nearest',
      intersect: true
    },
    scales: {
      xAxes: [{
        display: true,
        scaleLabel: {
          display: true,
          labelString: 'Month'
        }
      }],
    }
  }
};

var hiddenLabel = 'My Second Dataset';

var myLine = new Chart($('#canvas'), config);
var timerDuration = 5;
var timer = setInterval(function() {
  timerDuration--;
  $('#timer').text(timerDuration + ' seconds');
}, 1000);

setTimeout(function() {
  var indexToHide = -1;

  myLine.config.data.datasets.forEach(function(e, i) {
    if (e.label === hiddenLabel) {
      indexToHide = i;
    }
  });

  var meta = myLine.getDatasetMeta(indexToHide);

  meta.hidden = true;
  myLine.update();

  $('#timer').text('(already hidden)');
  clearInterval(timer);
}, timerDuration * 1000);
      </script>  
   </body>
</html>

Related Tutorials