Chart JS Re-Animate chart with click event - Javascript Chart.js

Javascript examples for Chart.js:Event

Description

Chart JS Re-Animate chart with click event

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <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.2/Chart.min.js"></script> 
      <style id="compiled-css" type="text/css">
.container {width:700px; margin:20px auto}
      </style> 
      <script type="text/javascript">
    window.onload=function(){//from  w ww  . j  a  v  a  2 s  . com
var ctx = document.getElementById("myChart");
var config = {
  type: 'bar',
  data: {
    labels: ["2014", "2015", "2016"],
    datasets: [{
      label: 'Group 1',
      data: [50, -59, 64],
      datalabels:{
        anchor: "end",
        align: "end",
        color:"black",
        font:{ size: 14, },
      },
      backgroundColor: [ '#5bbbbb', '#5bbbbb4', '#5bbbbb' ],
      borderWidth: 1
    },{
      label: 'Group 2',
      data: [69, -85, 92],
      datalabels:{
        anchor: "end",
        align: "end",
        color:"black",
        font:{ size: 14, },
      },
      backgroundColor: ['#ddd','#ddd','#ddd'],
      borderWidth: 1
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
        beginAtZero:true,
        max:100
        }
      }]
    }
  }
};
var chart = new Chart(ctx, config);
(function(){ 
document.querySelector('#button1').addEventListener('click',function(){
    chart.destroy();
    chart = new Chart(ctx, config);
});
})();
    }

      </script> 
   </head> 
   <body> 
      <div class="container"> 
         <button id="button1">Animate Again </button>
         <br> 
         <canvas id="myChart"></canvas> 
      </div>  
   </body>
</html>

Related Tutorials