用PHP绘制饼图 [英] Flot Pie Chart with PHP
问题描述
我尝试使用Flot Chart和PHP/MySQL动态添加加载饼图.
I tried to add load a Pie Chart dynamically using Flot Chart and PHP/MySQL.
这是我的JavaScript代码
This is my javascript code
<script id="source" language="javascript" type="text/javascript">
function fetchData() {
$.ajax({
url: "test.php",
method: "GET",
dataType: "json",
success: function(series) {
var data = [ series ];
$.plot($("#graph1"), data, {
pie: {
show: true,
showLabel: true
},
legend: {
show: false
}
});
}
});
setTimeout(fetchData, 1000);
}
$(function () {
$("#btn").click(function(){
fetchData();
});
});
这是我的PHP代码
<?php
include("db.php");
$return_arr = array();
$sql = mysql_query("SELECT item, COUNT(target) FROM counter WHERE type='video' and date BETWEEN '2011-02-21' and '2011-02-26' GROUP BY target ORDER BY id ASC");
while($obj = mysql_fetch_object($sql)){
$return_arr[] = $obj;
}
echo json_encode($return_arr);
?>
这是我的按钮
<input type="submit" value="click" id="btn"/>
这是单击按钮时得到的数组
This is the array that I get when I click the button
[{"item":"Final 2010","COUNT(target)":"2"},{"item":"Semi Final 2009","COUNT(target)":"3"}]
当我单击按钮时,它给我这个错误:
When I click the button, it gives me this error:
An invalid or illegal string was specified" code: "12
[Break On This Error] false
,甚至饼图也无法加载.谁能告诉我我在哪里弄错了.
and Even the pie chart is not loading. Can anybody tell me where did I made the wrong.
当我检入Firebug时,数组显示如下
When I check in firebug, the array is showing like this
[{"item":"Final 2010","COUNT(target)":"2"},{"item":"Semi Final 2009","COUNT(target)":"3"}]
但是当我打印它时,它显示为[Object object] [Object object],我认为这可能是问题所在,任何人都知道如何修复
But when I print it, it shows as [Object object][Object object], I think that would be the problem, anyone know how to fix it
谢谢
推荐答案
我认为您需要评估该数据(系列)
I think you need to eval that data (series)
var data = eval("(" + series + ")");
我认为应该这样做
编辑
在jsfiddle上添加了一个简单的复制: http://jsfiddle.net/TVAWL/1/
Added a simple replication on jsfiddle : http://jsfiddle.net/TVAWL/1/
这篇关于用PHP绘制饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!