Skip to content

Instantly share code, notes, and snippets.

@geoff-parsons
Created June 1, 2013 05:36
Show Gist options
  • Save geoff-parsons/5689383 to your computer and use it in GitHub Desktop.
Save geoff-parsons/5689383 to your computer and use it in GitHub Desktop.
Example of live updating Chart.js charts.
<!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>
@ckylefc
Copy link

ckylefc commented Jan 29, 2014

Hi this is a cool chart.
Is it possible if you can tell me how to do this with the bar graph?

@JJ
Copy link

JJ commented Jan 30, 2014

Do you need to do line #19? Woudln't it be enough with generating a new chart object? Or if you don't do that it will be drawn on top of it?

@developius
Copy link

I get my data from a MySQL db, via PHP. Is there any way to keep the chart live-updated with the latest results from PHP?
@JJ it looks as if you do, because otherwise the height of the chart changes.

@iampbernardo
Copy link

@xavbabe You would do an AJAX request and then on AJAX success you update the values.

@polunzh
Copy link

polunzh commented Oct 30, 2014

good

@Kayoti
Copy link

Kayoti commented Apr 21, 2015

@DamianCasale
Copy link

I have been looking for a solution for live updates of graphs. I stumbled across this and noticed that to do the update, you are creating a new graph and replacing the previous canvas.
This looks OK on your demo, but if you re-enable animations this redraws the animations rather than smoothly updating.

We can alter the segment data and utilise the .update() method (keeping the same canvas and chart object making the updates smooth and without replacing elements, giving an easy way to pull updates via ajax)

--- updated example ---

<!DOCTYPE html>
<html>
  <head>
    <title>Chart.js Live Update 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="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
    <script type="text/javascript" charset="utf-8">
      $(document).ready(function() {

            var max_value = 60

            chartOptions = {
              segmentShowStroke: false,
              percentageInnerCutout: 75,
              animation: false
            };

            chartData = [{
                value: 0,
                color: '#4FD134'
            },{
                value: max_value,
                color: '#DDDDDD'
            }];

            var ctx = $('#chart').get(0).getContext("2d");
            var theChart = new Chart(ctx).Doughnut(chartData,chartOptions);

            theInterval = setInterval(function(){ 
                if (theChart.segments[0].value == max_value) {
                    clearInterval(theInterval);
                } else {
                    theChart.segments[0].value = theChart.segments[0].value+1
                    theChart.segments[1].value = theChart.segments[1].value-1
                    theChart.update()
                }
            }, 1000);
      });
    </script>
  </head>
  <body>
    <canvas id="chart" width="300" height="300"></canvas>
  </body>
</html>

@Siddhant08
Copy link

Hi! I am new to js and other web development tools. Can you please tell me at what part of your code you are accessing your database server to plot the real-time graph?
Thanks in advance. :)

@RicardoVaranda
Copy link

@Siddhant08 He isn't, he is using:

setTimeout (function() { chartUpdate(value - 1); }, 1000);

at the end of chartUpdate to call the function again with a different value, this is also known as recursive programming.

https://en.wikipedia.org/wiki/Recursion_(computer_science)

@tomherold
Copy link

Replacing the canvas to update the chart is brilliant! I tried with Chart.update() but never got it to work.

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