[Chartjs]-Return array from ajax

0๐Ÿ‘

You can not return from ajax as it is asynchronous in nature, Use callback function or do the initialization of barChartData in ajax success handler.

Try this:

$.ajax({
  method: "GET",
  cache: false,
  url: "charts/applieschart.php",
  success: function(myData) {
    var chartjsLabel = [];
    var chartjsData = [];
    for (var i = 0; i < myData.length; i++) {
      chartjsData.push(myData[i].count);
      chartjsLabel.push(myData[i].date);
    }
    var barChartData = {
      labels: chartjsLabel,
      datasets: [{
        fillColor: "rgba(220,280,220,0.5)",
        strokeColor: "rgba(220,220,220,1)",
        data: chartjsData
      }]
    };
  }
});

Leave a comment