我想使用AmChart并从php获取图表的数据。
这是我的php文件:
$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有很好的数据。 SQL请求很好并返回我想要的内容。
这是我的Ajax请求的javascript:
$.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
});
};
不显示图表。
但是如果我通过这个改变var'数据': data = [{name:“toto”,count:10,color:“#FF0F00”},{name:“toto”,count:10,color:“#FF0F00”}]; 从Ajax查询成功,这可以正常工作!我得到了图表......
所以问题是我从我的php文件中获取的格式...我找不到合适的格式返回。
编辑: 如果我这样做:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = data;
[...]
}
这是打印在页面上: [{ “名称”: “布鲁塞尔”, “计数”: “4”, “色彩”: “#FF0F00”},{ “名称”: “智美”, “计数”: “2”, “颜色”:” #FF6600" }] 并显示一个空图表。
如果复制粘贴打印表作为数据:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = [{"name":"Brussels","count":"4","color":"#FF0F00"},{"name":"Chimay","count":"2","color":"#FF6600"}];
[...]
}
图表显示得很好。
答案 0 :(得分:0)
解决这个问题:
$.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;
[...]
});
查看此内容,了解有关$ .getJSON(法语)的更多信息:http://www.hooba.ca/blog/2008/echange-dobjets-json-entre-php-et-javascript-facile-grace-a-jquery/