Amchart and Ajax getting data in incorrect format
-
14-06-2021 - |
Question
I want to use AmChart and getting data for charts from php.
here is my php file:
$colors = Array("#FF0F00","#FF6600","#FF9E01","#FCD202","#F8FF01","#B0DE09","#04D215","#0D8ECF","#0D52D1","#2A0CD0","#8A0CCF","#CD0D74","#754DEB","#DDDDDD","#999999","#333333","#000000");
$datas = $bdd->query("SELECT count(*) as count, rest.name as name
FROM command com, zone z, dinningroom dr, restaurant rest
WHERE com.zone = z.number
AND z.dinningroom = dr.number
AND dr.restaurant = rest.number
GROUP BY rest.number;");
$i=0;
while($data = $datas->fetch())
{
$dataList[$i]=array("name"=>$data['name'],"count"=>$data['count'],"color"=>$colors[$i]);
$i++;
}
echo json_encode($dataList);
?>
$datas has good data. SQL request is good and return what i want.
Here is my javascript with my Ajax request:
$.ajax({
url: 'statComPerMenu.php',
failure: function (data) {
alert('fail: ' + data);
},
success: function(data) {
var statComPerMenuChart;
// SERIAL CHART
statComPerMenuChart = new AmCharts.AmSerialChart();
statComPerMenuChart.dataProvider = data;
statComPerMenuChart.categoryField = "name";
// the following two lines makes chart 3D
statComPerMenuChart.depth3D = 20;
statComPerMenuChart.angle = 30;
// AXES
// category
var categoryAxis = statComPerMenuChart.categoryAxis;
categoryAxis.labelRotation = 90;
categoryAxis.dashLength = 5;
categoryAxis.gridPosition = "start";
// value
var valueAxis = new AmCharts.ValueAxis;
valueAxis.dashLength = 5;
statComPerMenuChart.addValueAxis(valueAxis);
// GRAPH
var graph = new AmCharts.AmGraph();
graph.valueField = "count";
graph.colorField = "color";
graph.balloonText = "[[name]]: [[count]]";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 1;
statComPerMenuChart.addGraph(graph);
// WRITE
statComPerMenuChart.write("chartdiv");
} //Success
});
};
No chart are displayed.
But if I change the var 'data' by this: data = [{name: "toto", count: 10, color: "#FF0F00"},{name: "toto", count: 10, color: "#FF0F00"}]; in success from Ajax query, this works correctly! I get the chart...
So the problem stay is the format i get from my php file... i can't find the right format to return.
EDIT: if i do:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = data;
[...]
}
this is printed on page: [{"name":"Brussels","count":"4","color":"#FF0F00"},{"name":"Chimay","count":"2","color":"#FF6600"}] and shows an empty chart.
if copy paste printed table as data:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = [{"name":"Brussels","count":"4","color":"#FF0F00"},{"name":"Chimay","count":"2","color":"#FF6600"}];
[...]
}
the chart is well displayed.
Solution
Solved by doing this:
$.getJSON("statComPerMenu.php", // le fichier qui recevra la requête
null, // les paramètres
function(data){ // la fonction qui traitera l'objet reçu
[...]
chart.dataProvider = data;
[...]
});
Check this for more about $.getJSON (french): http://www.hooba.ca/blog/2008/echange-dobjets-json-entre-php-et-javascript-facile-grace-a-jquery/