I am working on creating dynamic values for a pie chart which is a graph from chart.js I have created a variable which contains multiple object values which needs to be passed in the array for pie graph. If I pass the variable nothing shows up but if I pass the same static values to the variable the pie chart starts working. I searched for various methods but could not make it work with my code. My code looks like this:
/*PIE Chart*/
var elements = [];
var counter;
jQuery.ajax({
type: 'POST',
url: 'ajax-request.php',
data: {},
success: function(response){
//here data is means the out put from the php file it is not
$('#StudentID').val()
var jsonData = JSON.parse(response);
console.log(elements);
var pieData;
for (var i = 0; i < jsonData.length; i++) {
counter = jsonData[i];
var sale = roundFloat(counter.sales,2);
elements+= '{value:'+sale+',color: "#FDB45C",highlight: "#FFC870",label:"'+counter.sku+'"},';
}
var pieData = [elements];
var ctx = document.getElementById("pie-chartjs").getContext("2d");
window.myPie = new Chart(ctx).Pie(pieData);
}
});
elementsis a string hence pieData is an array of one element i.e the string value of elements. I guess thePiefunction is supposed to take an array of objects.color: "#FFC870" highlight: "#FFC871" label: "Some Name" value: 50Currently I am getting the output 0 as key and the complete string as its value which is incorrect.0: "{value:217419.2,color: "#be4bc7",highlight: "#FFC870",label:"Some name"},"Any Help??