必须将哪种Java对象传递给Google Charts趋势线?

时间:2015-03-12 12:56:04

标签: java charts google-visualization

通过传递Gson作为参数,使用List<List<Trendline>>创建JSON。

对象Trendline如下:

private double state;
private double planogram;
private double mix;
private double share;
private double execution;
private String annotation;
// constructors, getters and setters

在JavaScript部分中,创建列后,每行都会添加:

var i = 0;
for (var key in trendline) {
    var row = trendline[key];
    for (var prop in row) {
        if (row.hasOwnProperty(prop)) {
            if (i === 0) {
                stateName = row[prop];
            }
            if (i === 1) {
                planogram = row[prop];
            }
            if (i === 2) {
                mix = row[prop];
            }
            if (i === 3) {
                share = row[prop];
            }
            if (i === 4) {
                execution = row[prop];
            }
            if (i === 5) {
                annotation = row[prop];
            }
        }
        i = i+1;
    }
    row = new Array(stateName, planogram, mix, share, execution, annotation);
    columnData.addRow(row);
}

即使在Java方法中将这些变量设置为手动值之后,也会显示图表,但未加载任何数据。

1 个答案:

答案 0 :(得分:0)

使用jsonschema2pojo作为构建POJO的助手。我粘贴的JSON代码来自Google Charts documentation

{
  "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}]}
      ]
}

它创建了4个类,我对其进行了一些修改(请参阅此答案的结尾处):GCDataTable(在“类名”中输入),ColRow和{{ 1}}。


构建JSON字符串的代码如下(使用Gson):

C

然后,我不是遍历每一行,而是将生成的JSON传递给Gson gson = new GsonBuilder().serializeNulls().create(); GCDataTable dataTable = new GCDataTable(); List<Col> cols = new ArrayList<>(); Col col1 = new Col(); col1.setId(""); col1.setLabel("Label 1"); col1.setPattern(""); col1.setType("number"); cols.add(col1); Col col2 = new Col(); col2.setId(""); col2.setLabel("Label 2"); col2.setPattern(""); col2.setType("number"); cols.add(col2); Col col3 = new Col(); col3.setId(""); col3.setLabel("Label 3"); col3.setPattern(""); col3.setType("number"); cols.add(col3); Col col4 = new Col(); col4.setId(""); col4.setLabel("Label 4"); col4.setPattern(""); col4.setType("number"); cols.add(col4); Col col5 = new Col(); col5.setId(""); col5.setLabel("Label 5"); col5.setPattern(""); col5.setType("number"); cols.add(col5); dataTable.setCols(cols); List<Row> rows = new ArrayList<>(); listOne.stream().map((_item) -> new ArrayList<>()).forEach((cList) -> { Row row = new Row(); listTwo.stream().map((obj) -> { C c = new C(); c.setV(obj.getAttribute()); return c; }).map((c) -> { c.setF(null); return c; }).map((c) -> { cList.add(c); return c; }).map((_item) -> { row.setC(cList); return _item; }).forEach((_item) -> { rows.add(row); }); }); dataTable.setRows(rows); trendlineJson = gson.toJson(dataTable); 的构造函数,如下所示:

google.visualization.DataTable

Java 7 for-each:

var dataTable = #{managedBean.trendlineJson}
var columnData = new google.visualization.DataTable(dataTable);