Javascript examples for Chart.js:Bar Chart
Chartjs bar chart with dynamic data
<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> function getRandomIntInclusive(min, max) { min = Math.ceil(min);//from w ww . j a va 2s . c o m 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; // logic to get new data 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>