简体   繁体   English

将data.group用于Google可视化仪表板

[英]Using data.group for Google Visualization Dashboards

I have a table with two keys, like so: 我有一个带有两个键的表,如下所示:

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

I wish to be able to filter this table by key2, so I created a google.visualization.Dashboard like so: 我希望能够通过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": {...}
    });
]);

Now here's the rub: I wish to add a pie chart to this table which aggregates data by key1 . 现在麻烦了:我希望向该表添加一个饼图,该饼图通过key1聚合数据 So without any filtering, the pie chart would show something like: 因此,没有任何过滤,饼图将显示如下内容:

abc = 21
xyz = 12

But if I filtered down to "456" then it should show: 但是,如果我过滤到“ 456”,则应显示:

abc = 7
xyz = 4

Now if I weren't using a dashboard, I can accomplish this aggregation like so: 现在,如果我不使用仪表板,则可以像这样完成此聚合:

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);

However I don't know how to perform this aggregation within a dashboard 但是我不知道如何在仪表板中执行此聚合

save a reference to the table chart, 保存对表格的引用,
use its 'ready' event to draw the pie chart 使用其'ready'事件绘制饼图

anytime the filter changes, the 'ready' event will fire 只要过滤条件发生变化,就会触发'ready'事件

you can pull the filtered data table from the table chart, 您可以从表格图表中提取经过过滤的数据表格,
to use for your aggregation 用于您的汇总

it will be the same data table used to draw the dashboard, 它将与用于绘制仪表板的数据表相同,
with any filters applied 应用任何过滤器

just be sure to assign the 'ready' event, 只需确保分配'ready'事件,
before drawing the dashboard 绘制仪表盘之前

something like the following snippet... 类似于以下代码段...

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);

note: the dashboard also has a 'ready' event, 注意:仪表板还具有'ready'事件,
but it will fire for each bound control and chart, 但会为每个绑定的控件和图表触发,
so twice in this scenario 所以在这种情况下两次

and you also need somewhere to pull the filtered data table from, 而且您还需要在某处从中提取过滤的数据表,
hence saving a reference to the table chart... 因此可以保存对表格的引用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM