Javascript examples for Chart.js:Chart Data
Add data to chart dynamically
<html> <head> <title>ChartJS Line Chart</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script> <style id="compiled-css" type="text/css"> canvas { background-color : #eee; } </style> <script type="text/javascript"> window.onload=function(){// w w w . java2s . c om // Init chart const values = [] const ctx = document.getElementById('chartContainer').getContext('2d'); const chart = new Chart(ctx, { type: 'line', data: { labels: ['start'], datasets: [ { label: 'mm of rain', data: [1], borderWidth: 1 } ] } }); // Push 1 item every 100ms (10 Hz), simulates // your data coming through at a faster // rate than you can render setInterval(() => { values.push(Math.random()) }, 100) // Pull last 4 items every 1 second (1 Hz) setInterval(() => { // splice last 4 items, add them to the chart's data values.splice(values.length - 4, 4) .forEach((value, index) => { chart.data.labels.push(index) chart.data.datasets[0].data.push(value) }) // finally, command the chart to update once! chart.update() }, 1000) } </script> </head> <body> <canvas id="chartContainer" width="600" height="400"></canvas> </body> </html>