如何使用 canvas 制作动画条形图?

How to make animated bar graph with canvas?

请在此处查看我正在尝试执行的操作的示例:http://www.chartjs.org/。我正在尝试复制 header,让条形图一直在移动。我试图让它工作,但它似乎不起作用。我可以看到他们有

<div class="aspect-ratio">
        <canvas width='1200' height='480' id="hero-bar"></canvas>
        <header>
        </header>
</div>

我认为这是使它起作用的 js:

<script type="text/javascript">
(function(){
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);
        };
    }
});
 }); 
    </script>

关于如何使这项工作有任何想法吗?

这是 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>