我一直在尝试动态填写我的谷歌图表,比如这个例子: https ://developers.google.com/chart/interactive/docs/php_example
我将此依赖项用于 DataTable 类和其他类:
<dependency>
<groupId>com.google.visualization</groupId>
<artifactId>visualization-datasource</artifactId>
<version>1.1.1</version>
</dependency>
这是 DataTable 类的 API: https ://developers.google.com/chart/interactive/docs/dev/dsl_javadocs/com/google/visualization/datasource/datatable/DataTable
我正在使用包含此代码(示例)的 Java HttpServlet 来填充 DataTable:
DataTable dataTable = new DataTable();
dataTable.addColumn(new ColumnDescription("uur", ValueType.TEXT, "Uur"));
dataTable.addColumn(new ColumnDescription("aantal", ValueType.NUMBER, "Aantal spellen"));
try {
dataTable.addRowFromValues("1", 5, true);
dataTable.addRowFromValues("2", 9, true);
dataTable.addRowFromValues("3", 17, true);
} catch (TypeMismatchException ex) {
Logger.getLogger(VerkrijgChartDataServlet.class.getName()).log(Level.SEVERE, null, ex);
}
我还包括它以使其返回 JSON:
response.setContentType("application/json");
然后使用 Gson 将其转换为 JSON 字符串:
new Gson().toJson(dataTable);
这将返回格式的字符串:
{
"columnIndexById" : {"aantal":1, "uur":0},
"columns" : [
{"id":"uur","label":"Uur","pattern":"","type":"TEXT"},
{"id":"aantal","label":"Aantal spellen","pattern":"","type":"NUMBER"}
],
"rows" : [
{"cells":[{"value":{"value":"1"}},{"value":{"value":5.0}}]},
{"cells":[{"value":{"value":"2"}},{"value":{"value":9.0}}]},
{"cells":[{"value":{"value":"3"}},{"value":{"value":17.0}}]},
],
"warnings" : []
}
但随后谷歌图表显示:“表格没有列。” Google 的 JSON 示例如下所示:
{
"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}]}
]
}
有谁知道我做错了什么?我需要做其他事情吗?提前谢谢。