我想使用 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 请求很好,返回我想要的。
这是我的 javascript 与我的 Ajax 请求:
$.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': data = [{name: "toto", count: 10, color: "#FF0F00"},{name: "toto", count: 10, color: "#FF0F00 "}]; 从 Ajax 查询成功,这工作正常!我得到图表...
所以问题是我从我的php文件中得到的格式......我找不到正确的格式来返回。
编辑:如果我这样做:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = data;
[...]
}
这打印在页面上:[{"name":"Brussels","count":"4","color":"#FF0F00"},{"name":"Chimay","count":"2" ,"color":"#FF6600"}] 并显示一个空图表。
如果将打印的表格复制粘贴为数据:
success: function(data) {
$("#omg").html(data);
[...]
chart.dataProvider = [{"name":"Brussels","count":"4","color":"#FF0F00"},{"name":"Chimay","count":"2","color":"#FF6600"}];
[...]
}
图表显示得很好。