Chartjs-How to make animated bar graph with canvas?

1👍

Here’s a working version of the randomly changing Hero from ChartJS.org

var data = [],
    barsCount = 50,
    labels = new Array(barsCount),
    updateDelayMax = 500,
    $id = function(id){
      return document.getElementById(id);
    },
    random = function(max){ return Math.round(Math.random()*100)},
    helpers = Chart.helpers;


Chart.defaults.global.responsive = true;


for (var i = barsCount - 1; i >= 0; i--) {
  data.push(Math.round(Math.random() * 100));
};
new Chart($id('hero-bar').getContext('2d')).Bar({
  labels : labels,
  datasets : [{
    fillColor : '#2B303B',
    data : data
  }]
},{
  showScale : false,
  barShowStroke : false,
  barValueSpacing: 1,
  showTooltips : false,
  onAnimationComplete : function(){
    // Get scope of the hero chart during updates
    var heroChart = this,
        timeout;
    // Stop this running every time the update is fired
    this.options.onAnimationComplete = randomUpdate;

    this.options.animationEasing = 'easeOutQuint';

    randomUpdate();

    function randomUpdate(){
      heroChart.stop();
      clearTimeout(timeout);
      // Get a random bar
      timeout = setTimeout(function(){
        var randomNumberOfBars = Math.floor(Math.random() * barsCount),
            i;
        for (i = randomNumberOfBars - 1; i >= 0; i--) {
          heroChart.datasets[0].bars[Math.floor(Math.random() * barsCount)].value = Math.round(Math.random() * 100);
        };
        heroChart.update();
      },Math.random() * updateDelayMax);
    };
  }
});
body{ background-color: ivory; }
#canvas{border:1px solid red;}
.aspect-ratio{background-color:#232830}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<div class="aspect-ratio">
  <canvas width='1200' height='480' id="hero-bar"></canvas>
  <header></header>
</div>

Leave a comment