Reputation: 1192
Is there any way to calculate sum of a column in table of google visualization API. For example in MS Excel there is feature for auto sum to calculate sum of a column. thanks.
Upvotes: 2
Views: 3148
Reputation: 7128
The only built-in aggregation functions are for grouping data
However, it is easy to write a function that will return the sum of a column in javascript.
For instance:
var data = google.visualization.arrayToDataTable([
['Year', 'Austria', 'Belgium', 'Czech Republic', 'Finland', 'France', 'Germany'],
['2003', 1336060, 3817614, 974066, 1104797, 6651824, 15727003],
['2004', 1538156, 3968305, 928875, 1151983, 5940129, 17356071],
['2005', 1576579, 4063225, 1063414, 1156441, 5714009, 16716049],
['2006', 1600652, 4604684, 940478, 1167979, 6190532, 18542843],
['2007', 1968113, 4013653, 1037079, 1207029, 6420270, 19564053],
['2008', 1901067, 6792087, 1037327, 1284795, 6240921, 19830493]
]);
function getSum(data, column) {
var total = 0;
for (i = 0; i < data.getNumberOfRows(); i++)
total = total + data.getValue(i, column);
return total;
}
alert(getSum(data, 1));
This will give you a nice alert box saying 9920627
1336060+1538156+1576579+1600652+1968113+1901067=9920627
Fiddle with the column number or use your own data table to make it all flexible-like.
Upvotes: 6