Home forums Using CanvasJS How can I use PHP MySQL Dynamic data Reply To: How can I use PHP MySQL Dynamic data

#7402

Anjali
Participant

Hi,

In your JSON data y-value contains string data (in quotes) while CanvasJS requires numbers. So you basically need to parse those strings before assigning.

Here, is how you can do the same.

$(document).ready(function () {
var dataPoints = [];
$.getJSON("jsonparser.php", function (result) {
for (var i = 0; i < result.length; i++) {
	dataPoints.push({x: result[i].x, y: parseFloat(result[i].y)})	
};
var chart = new CanvasJS.Chart("chartContainer", {
data: [
{ type:'line',
dataPoints: dataPoints
}
]
});
chart.render();
});	
}); 

__
Anjali