0👍
success
is a callback function, meaning that it executes only after the ajax call is complete. This means that the variable barChartData
is getting initialised before toll
has any values. In fact, toll
has not been initialised, since you’ve simply written var toll;
.
- Initialize
toll
, and set it to null value. - Get access to your chart.
-
Add the toll data to your datasets and update the chart inside the success function.
var toll = null; var chart = ... //Hopefully you have access to the instance of your chart? If not, please show the rest of your code. $.ajax({ async : false, type : "GET", url : "barChartData.php", success : function(data) { toll = data; barChartData.datasets.push(toll); chart.update(); } }); var barChartData = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September','October','Novemeber','December'], datasets: [] };
- Chartjs-How can I remove two labels from my legend with an alternative to item.datasetIndex !== 1 && item.datasetIndex !== 4; ? (Charts.js)
- Chartjs-How to add multiple data in chart js dynamically from JSON
Source:stackexchange.com