-
-
Save jackishere/6c8265a1d66581d1c9d95f69c0adc079 to your computer and use it in GitHub Desktop.
Example of live updating Chart.js charts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Chart.js Redraw Example</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> | |
<script type="text/javascript" charset="utf-8" src="chart.min.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
window.chartOptions = { | |
segmentShowStroke: false, | |
percentageInnerCutout: 75, | |
animation: false | |
}; | |
var chartUpdate = function(value) { | |
console.log("Updating Chart: ", value); | |
// Replace the chart canvas element | |
$('#chart').replaceWith('<canvas id="chart" width="300" height="300"></canvas>'); | |
// Draw the chart | |
var ctx = $('#chart').get(0).getContext("2d"); | |
new Chart(ctx).Doughnut([ | |
{ value: value, | |
color: '#4FD134' }, | |
{ value: 100-value, | |
color: '#DDDDDD' }], | |
window.chartOptions); | |
// Schedule next chart update tick | |
setTimeout (function() { chartUpdate(value - 1); }, 1000); | |
} | |
$(document).ready(function() { | |
setTimeout (function() { chartUpdate(99); }, 1000); | |
}) | |
</script> | |
</head> | |
<body> | |
<canvas id="chart" width="300" height="300"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment