Javascript examples for Chart.js:Chart Data
Chart.js dynamic updates with data from database
<html lang="en"> <head> <title>Chart.js - Dynamically Update Chart Via Ajax Requests</title> </head> <body translate="no"> <div style="width:50%;"> <canvas id="mycanvas"></canvas> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> <script> // used for example purposes function getRandomIntInclusive(min, max) { min = Math.ceil(min);//from w w w. j av a 2 s .c om max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } var ctx_live = document.getElementById("mycanvas"); var myChart = new Chart(ctx_live, { type: 'bar', data: { labels: [], datasets: [{ data: [], borderWidth: 1, borderColor:'#00c0ef', label: 'liveCount', }] }, options: { responsive: true, title: { display: true, text: "Chart.js - Dynamically Update Chart Via Ajax Requests", }, legend: { display: false }, scales: { yAxes: [{ ticks: { beginAtZero: true, } }] } } }); var postId = 1; var getData = function() { $.ajax({ url: 'https://jsonplaceholder.typicode.com/posts/' + postId + '/comments', success: function(data) { myChart.data.labels.push("Post " + postId++); myChart.data.datasets[0].data.push(getRandomIntInclusive(1, 25)); myChart.update(); } }); }; setInterval(getData, 3000); </script> </body> </html>