使用CodeIgniter基于JSON数据创建Google饼图
我试图根据示例创建一个饼图 在我的控制器(mycontroller/json)中,我有以下代码:使用CodeIgniter基于JSON数据创建Google饼图,codeigniter,google-visualization,json,Codeigniter,Google Visualization,Json,我试图根据示例创建一个饼图 在我的控制器(mycontroller/json)中,我有以下代码: public function json(){ $whatever = '{ "cols": [ {"id":"","label":"Topping","pattern":"","type":"string"}, {"id":"","label":"Slices","pattern":"","type":"number"} ], "rows": [ {"c":[{"v"
public function json(){
$whatever = '{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}';
echo $whatever;
}
在我看来,我有以下代码(显示部分代码)
var jsonData=$.ajax({
url:“index.php/bunker/json”,
数据类型:“json”,
异步:false
}).responseText;
//创建并填充数据表。
var popularCategory=google.visualization.DataTable(jsonData);
新的google.visualization.PieChart(document.getElementById('category')).draw(popularCategory,
{标题:“按类别划分的受欢迎程度”});
最后我有一个id为“category”的div。但是,我总是收到一条消息,说数据没有定义
使用firebug进行快速调试,我没有收到任何错误,$whatever变量也被视为正确的JSON格式。我在这里犯了什么错误
干杯,您的json变量的数据格式不正确。请检查此处的饼图示例:
尝试php json编码函数:
echo json_encode($whatever);
另外,确保开始使用console.log(jsonData),因为浏览器控制台将输出类似于php中print_r的'jsonData'变量。我从来没有第一次得到正确的json格式,所以console.log(jsonData)特性绝对是杀手锏 而不是:
var popularCategory = google.visualization.DataTable(jsonData);
你必须具备:
var popularCategory = new google.visualization.DataTable(jsonData);
是的(除了使用console.log之外),我的json代码看起来很正常(或者至少看起来与google中的代码一模一样)。我要找的是json数据源。回到我最初的JSON代码问题,上面的JSON代码是从google本身直接复制粘贴的。你是从哪里复制的?你能给我链接吗。
var popularCategory = new google.visualization.DataTable(jsonData);