Chartjs-Chart.js dynamic updates with data from database

2👍

Welcome to SO and chart.js!

Based on your code, It looks like you are not quite updating your chart data correctly. Also, you will need to use the .update() function instead of .render(). Here is an updated version of your code that should work.

Note, I’m not sure what format result is in, but you might have to iterate over it and build a new data array first (just like you did when you first created the chart) before using this example.

var updateChart = function() {
  $.getJSON("data.php", function (result) {
     //barGraph.data.labels = newLabelArray;
     barGraph.data.datasets[0].data = result;
     barGraph.update();
   }); 
}

One thing to keep in mind is that chances are if you are updating your data, then you usually need to update your labels as well. I have commented out a line above that demonstrates how to do it. You will also probably have to take your result object and build your labels like you do on your initial chart setup.

Based on your latest code and comments,it looks like you are having trouble structuring you code to get this to work. Here is an example that you should be able to build from to solve your problem.

Leave a comment