我有如下数据:
store 1 Store 2
store_id walk-ins walk-ins
morning 20 25
noon 35 40
night 50 55
有20个商店需要绘制每行数值的堆叠图。
Google Charts文档告诉我数据数组应该像这样:
var data = google.visualization.arrayToDataTable([
['Stores', 'Store 1', 'Store 2', 'Store 3', 'Store 4', ... ],
['morning', 10, 24, 20, 32, 18, 5, ...],
['noon', 16, 22, 23, 30, 16, 9, ...],
['night', 28, 19, 29, 30, 12, 13, ...],
]);
我通过MySQL脚本/服务器PHP脚本获取数据。JSON应该是什么样子?来自MySQL查询的json_encode($data)
返回结果如下:
[{"store_name":"Store 1","Time":"Morning","count":"17"}, ...]
但是图表无法加载并显示“Table has no columns
”消息。
我按以下方式加载JSON:
var url = '/url/updatedata.php?var=querytype';
jQuery.getJSON( url, function(Json) {
// Create and populate the data table.
var data = new google.visualization.DataTable(Json);
....
一个堆叠式柱状图的JSON结构是什么?
谢谢!
map
函数将你的JSON数组映射到数据表中,例如Json.map(function(item) { return ['morning', 10, 16, 28]; })
。 - vortexwolfgoogle.visualization.DataTable
构造函数:https://developers.google.com/chart/interactive/docs/reference - vortexwolf