将data.group用于Google Visualization Dashboards

时间:2017-08-25 13:31:15

标签: javascript charts google-visualization dashboard graphing

我有一张包含两个键的表,如下所示:

+------+------+-------+
| key1 | key2 | value |
+------+------+-------+
| abc  | 123  |   5   |
| abc  | 456  |   7   |
| abc  | 789  |   9   |
| xyz  | 123  |   2   |
| xyz  | 456  |   4   |
| xyz  | 789  |   6   |
+------+------+-------+

我希望能够通过key2过滤此表,因此我创建了google.visualization.Dashboard,如下所示:

var dashboard = new google.visualization.Dashboard();
dashboard.bind([
    new google.visualization.ControlWrapper({
        "controlType": "CategoryFilter",
        "containerId": "...",
        "options": {
            "filterColumnIndex": 1
        }
    });
], [
    new google.visualization.ChartWrapper({
        "chartType": "Table",
        "containerId": "...",
        "options": {...}
    });
]);

现在有了解决方法:我希望在此表中添加一个饼图,它通过key1 聚合数据。因此,没有任何过滤,饼图将显示如下内容:

abc = 21
xyz = 12

但如果我过滤到" 456"然后它应该显示:

abc = 7
xyz = 4

现在,如果我没有使用仪表板,我可以完成这样的聚合:

var graph = new google.visualization.PieChart();
var aggregatedData = google.visualization.data.group(
    data,
    [0],
    [{
        "column": 2,
        "aggregation": google.visualization.data.sum,
        "type": "number"
    }]
);
graph.draw(aggregatedData);

但我不知道如何在仪表板中执行此聚合

1 个答案:

答案 0 :(得分:2)

保存对表格图表的引用,
使用其'ready'事件绘制饼图

只要过滤器发生变化,'ready'事件就会触发

您可以从表格中提取已过滤的数据表,
用于聚合

它将与用于绘制仪表板的数据表相同,
应用任何过滤器

确保分配'ready'事件,
在绘制仪表板之前

类似以下代码段......

var chartTable = new google.visualization.ChartWrapper({
    "chartType": "Table",
    "containerId": "...",
    "options": {...}
});

var dashboard = new google.visualization.Dashboard();
dashboard.bind([
    new google.visualization.ControlWrapper({
        "controlType": "CategoryFilter",
        "containerId": "...",
        "options": {
            "filterColumnIndex": 1
        }
    });
], [
    chartTable
]);

google.visualization.events.addListener(chartTable, 'ready', function () {
    var graph = new google.visualization.PieChart(needContainer);
    var aggregatedData = google.visualization.data.group(
        chartTable.getDataTable(),
        [0],
        [{
            "column": 2,
            "aggregation": google.visualization.data.sum,
            "type": "number"
        }]
    );
    graph.draw(aggregatedData);
});

dashboard.draw(data);

注意:仪表板还有一个'ready'事件,
但它会为每个绑定的控件和图表触发,
在这种情况下两次

你还需要一个从中提取过滤数据表的地方,
因此保存了对表格图表的引用......