Dynamic pie chart with DataTable

162 views
Skip to first unread message

research800

unread,
Apr 19, 2012, 1:57:56 PM4/19/12
to Google Visualization API
I'm using arrayToDataTable to convert set of rows & columns to Table
format.
I need to build a pie chart using two columns of the table but pie
chart should be a after performing an aggregation function on the rows
of the "table".

Ex: Table1
var data = google.visualization.arrayToDataTable([
['YEAR','Country','State','Population'],
['2011','USA','CA',2.5],
['2011','USA','NY',3.4],
['2011','USA','PA',1.2],
['2011','CANADA','ONTARIO',0.4],
['2011','CANADA','QUEBEC',0.2],
]);

I want pie chart to be drawn for Country & Population using this data
(Table2):
Country, Population
USA, 7.1
CANADA, 0.6

Could someone explain how to create Table2 from Table1 dynamically for
the pie chart?

asgallant

unread,
Apr 20, 2012, 8:59:27 AM4/20/12
to google-visua...@googlegroups.com
You need to use the Group data method (https://developers.google.com/chart/interactive/docs/reference#google_visualization_data_group): 

// create a DataTable with data from the 4th column grouped by the second column 
​var table2 google.visualization.data.Group(table1[1][{
    column3,
    aggregationgoogle.visualization.data.sum,
    type'number'
}]); 
Reply all
Reply to author
Forward
0 new messages