Get the sum of a column in a google chart table
2 answers
Alternatively, if you have a DataTable that you manually enter, you can do the same as:
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['x', 'Cats'],
['A', 1],
['B', 2],
['C', 4],
['D', 8],
['E', 7],
['F', 7],
['G', 8],
['H', 4],
['I', 2],
['J', 3.5],
['K', 3],
['L', 3.5],
['M', 1],
['N', 1]
]);
// Create an Array to hold column data
var tmpColumn = new Array();
// Add each data value to the array with push()
for(var i = 0; i < data.getNumberOfColumns(); ++i) {
tmpColumn.push(data.getValue(i, 1));
}
// Use built-in Google Functions on the array
alert(google.visualization.data.avg(tmpColumn));
alert(google.visualization.data.sum(tmpColumn));
alert(google.visualization.data.count(tmpColumn));
}
+3
source to share
You may be looking for something like this:
var query = new google.visualization.Query(DATA_SOURCE_URL);
query.setQuery('select dept, sum(salary) group by dept');
query.send(handleQueryResponse);
DATA_SOURCE_URL
can match the url of the Google Chart data source.
From: https://developers.google.com/chart/interactive/docs/querylanguage
+2
source to share