Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

Get sum of column in google chart table

Any method that can get sum, avg, count of data at a column in google chart table? I want to show metadata of that table out of the table.

Upvotes: 3

Views: 7036

Answers (2)

jmac
jmac

Reputation: 7128

Alternatively, if you have a DataTable you manually input, you can do the same as follows:

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

Upvotes: 3

redDragonzz
redDragonzz

Reputation: 1571

Perhaps you are 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 correspond to a Google Chart Data Source URL.

From: https://developers.google.com/chart/interactive/docs/querylanguage

Upvotes: 2

Related Questions