Update Chart in Javascript using Chart.js - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Update Chart in Javascript using Chart.js

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://rawgit.com/nnnick/Chart.js/v1.0.2/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){// ww w  .j a  va  2  s  . com
function generateData() {
    yArr = [Math.random() * 100];
}
var data = {
    labels: ["J", "F", "M", "A", "M", "J", "J"],
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,0,0.5)",
            strokeColor: "rgba(220,220,0,0.8)",
            highlightFill: "rgba(220,220,0,0.75)",
            highlightStroke: "rgba(220,220,0,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).Line(data);
var simDur = 10;
var yArr;
var t = 0;
var interval = setInterval(function () {
    if (t >= simDur) {
        clearInterval(interval);
        return;
    }
    generateData(); //calculate the new data yArr;
    myLineChart.addData(yArr, t); //yArr is an array of Y values, t=time
    myLineChart.update();  //update the chart with the new added data
    t++;
}, 1000)
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="200"></canvas>  
   </body>
</html>

Related Tutorials