我正在尝试使用 API 绘制 Google 柱形图。数据来自 web 服务作为我解析的 JSON 字符串,然后进行操作以添加数据,然后显示图表,就像这样。
var jsond = JSON.parse(response.d);
var data = null;
data = new google.visualization.DataTable();
for (var i = 0; i < jsond["cols"].length; i++) {
data.addColumn((jsond["cols"][i].type == 'number' ? 'number' : 'string'), null, jsond["cols"][i].id);
}
data.addRows(jsond["rows"].length);
for (var i = 0; i < jsond["rows"].length; i++) {
data.setCell(i, 0, "'" + jsond["rows"][i].finmonth.toString() + "'");
data.setCell(i, 1, parseFloat(jsond["rows"][i].Target));
data.setCell(i, 2, parseFloat(jsond["rows"][i].Proposal));
data.setCell(i, 3, parseFloat(jsond["rows"][i].ContractTotal));
data.setCell(i, 4, parseFloat(jsond["rows"][i].Budget));
data.setCell(i, 5, parseFloat(jsond["rows"][i].flag));
}
var options = {
title: 'The road thus far',
vAxis: { title: "Totals" },
hAxis: { title: "Financial Month" }
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
现在的问题是它接受了数据并且没有返回错误。我在数据表中加载数据后检查了数据,data.toJSON()
它与谷歌规定的数据方式相匹配。请参阅下面的数据,请记住,我只在下面的示例中添加了 1 个数据行,有 12 个标准行。
{"cols":[{"id":"finmonth","label":"","pattern":"","type":"string"},
{"id":"Target","label":"","pattern":"","type":"number"},
{"id":"Proposal","label":"","pattern":"","type":"number"},
{"id":"ContractTotal","label":"","pattern":"","type":"number"},
{"id":"Budget","label":"","pattern":"","type":"number"},
{"id":"flag","label":"","pattern":"","type":"number"}],
"rows":[{"c":[{"v":"'2013/1'","f":null},{"v":0,"f":null},{"v":37,"f":null},{"v":4696,"f":null},{"v":4733,"f":null},{"v":0,"f":null}]}],
"p":null}
那我错过了什么?因为如果我在 javascript 数组中对测试数据进行硬编码,图表会显示...:/