我尝试根据此处的示例创建饼图
在我的控制器(mycontroller/json)中,我有以下代码:
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: "<?= base_url()?>index.php/bunker/json",
dataType:"json",
async: false
}).responseText;
// Create and populate the data table.
var popularCategory = google.visualization.DataTable(jsonData);
new google.visualization.PieChart(document.getElementById('category')).draw(popularCategory,
{title:"Popularity by Category"});
最后我有一个名为“类别”的 div。但是,我总是收到一条消息说数据未定义。
使用 firebug 进行快速调试,我没有收到任何错误,$whatever 变量也被视为正确的 JSON 格式。我在这里犯了什么错误?
干杯,