[Chartjs]-Live charts using chart.js

2👍

There is a live chart example on the Chart.js homepage at http://www.chartjs.org/ Here is the relevant code, but you can copy the full working example from that page.

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);
        };
    }
});

Leave a comment