Google可视化数据分组

时间:2013-04-26 13:24:07

标签: google-visualization aggregation dashboard

我有一个示例Google可视化仪表板,其中包含两个控件和两个柱形图,其中使用数据表聚合绘制第二个柱形图,

'dataTable' : google.visualization.data.group(data, [0],
 [{'column': 2, 'aggregation': google.visualization.data.sum, 'type': 'number'}])

并使用中间表格图重新绘制,

google.visualization.events.addListener(divPicker, 'statechange',
 function(event) {
 // group the data of the filtered table and set the result in the pie chart.
 columnChart1.setDataTable( google.visualization.data.group(
 // get the filtered results
 table.getDataTable(),
 [0],
 [{'column': 2, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
 ));

所有代码都运行正常。但问题是当我选择控件时,聚合创建的图表没有正确更改。只有当我选择两次相同的控制选项时,它才会改变。

1 个答案:

答案 0 :(得分:2)

经过这么多小时的破解后,我终于解决了这个问题。

对于修复,我使用了两个事件监听器,其中一个是就绪事件,另一个是 statechange 事件,

google.visualization.events.addListener(subdivPicker, 'ready',
function(event) {
// group the data of the filtered table and set the result in the pie chart.
columnChart1.setDataTable( google.visualization.data.group(
// get the filtered results
 table.getDataTable(),
 [0],
[{'column': 2, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
));    
// redraw the pie chart to reflect changes
columnChart1.draw();
});

google.visualization.events.addListener(subdivPicker, 'statechange',
 function(event) {
 // group the data of the filtered table and set the result in the pie chart.
 columnChart1.setDataTable( google.visualization.data.group(
 // get the filtered results
 table.getDataTable(),
 [0],
 [{'column': 2, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
 ));
 // redraw the pie chart to reflect changes
 columnChart1.draw();
 });

this fiddle中找到更新的代码。