Skip to content

Instantly share code, notes, and snippets.

@rossdylan
Created October 2, 2013 21:29
Show Gist options
  • Select an option

  • Save rossdylan/6800797 to your computer and use it in GitHub Desktop.

Select an option

Save rossdylan/6800797 to your computer and use it in GitHub Desktop.
scrolling barchart with chart.js
<!doctype html>
<html>
<head>
<script src="Chart.js"></script>
</head>
<body>
<canvas id="canvas" height="450" width="1000"></canvas>
<script>
var data = {
labels: ["","","","","","","","","","","","","","","","","","","",""],
datasets: [
{
data: [10,15,5,10,12,6,19,10,5,7]
}
]
}
options = {
animation: false,
barValueSpacing: 1
}
var chart = new Chart(document.getElementById("canvas").getContext("2d"));
chart.Bar(data, options);
function getRandomInt(min,max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
setInterval(function () {
data.datasets[0].data.unshift(getRandomInt(0,20));
if(data.datasets[0].data.length >= 21) {
data.datasets[0].data.pop();
}
chart.Bar(data, options);
}, 1000);
</script>
</body>
</html>
@ivanbylinkin

Copy link
Copy Markdown

not sure if anyone is still using this or if OP is still updating this, but using the above method will create a funky chart overlay every time you hover over any bar in the chart. If you modify it just a little bit to destroy the chart before creating a new one it will function exactly the same but you won't have any problems with hovering. If you take a little longer on it you can even use the update() method for a faster/cleaner implementation. Nevertheless the adding the destroy function would be an improvement:

var chart = new Chart(document.getElementById("canvas").getContext("2d")).Bar(data, options);
....
setInterval(function () {
    data.datasets[0].data.unshift(getRandomInt(0,20));
    if(data.datasets[0].data.length >= 21) {
        data.datasets[0].data.pop();
    }
    chart.destroy();
    chart = new Chart(document.getElementById("canvas").getContext("2d")).Bar(data, options);
}, 1000);

@adamellsworth

Copy link
Copy Markdown

@ivanbylinkin thanks!

@adithyamaheshb

Copy link
Copy Markdown

What version of chart.js are you guys using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment