Home > OS >  Using data.group for Google Visualization Tables
Using data.group for Google Visualization Tables

Time:12-01

I have a dataset with 1 key and two values, like below:

Column A Column B Column C
Anh 6 8
Zavier 2 3
Trey 5 5
Zavier 5 9
Anh 1 2

I would like the to create a table that groups A by the sums of B & C like this:

Column A Column B Column C
Anh 7 10
Zavier 7 12
Trey 5 5

The following code groups A by the sum of B:

var nationalSummary = new google.visualization.ChartWrapper({
          'chartType': 'Table',
          'containerId': 'nationalSummary_div',
          dataTable: data,
      'options': {
        'width': '100%',
        'rowCount': 'enable',
        'allowHtml': true,
        'alternatingRowStyle': true,
        'cssClassNames': {
          tableCell: 'large-font',
          headerCell: 'nsHeaderCell',
          }
          },
            'view': {
                'columns': [0,1]}
        });

  var container = document.getElementById('buildingDetail_div');
  google.visualization.events.addListener(table, 'ready', function () {

  var nationalSummaryNew = table.getDataTable();
  var nationalSummaryArray = google.visualization.data.group(nationalSummaryNew,[{
      column: 0,
      type: 'string',
      label: 'Column A'        
      }], [{
      column: 1,
      type: 'number',
      label: 'Column B',
      aggregation: google.visualization.data.sum
      }]);

When I try to modify the code to include Col C, I modify the end of "nationalSummary" from:

            'view': {
                'columns': [0,1]}

to

            'view': {
                'columns': [0,1,2]}

and "nationalSummaryArray" to:

  var nationalSummaryArray = google.visualization.data.group(nationalSummaryNew,[{
      column: 0,
      type: 'string',
      label: 'Column A'        
      }], [{
      column: 1,
      type: 'number',
      label: 'Column B',
      aggregation: google.visualization.data.sum
      }], [{
      column: 2,
      type: 'number',
      label: 'Column C',
      aggregation: google.visualization.data.sum
      }]);

I get the following error:

Invalid column index 2. Should be an integer in the range [0-1].

How do I add Column C? Thank you in advance.

CodePudding user response:

Try adding behind options (as a sixth line):

animation: {
        "startup": false,}

I've read here there's a bug in that, just to give it a try!

CodePudding user response:

You can achieve this by using the following QUERY function:

=QUERY(A1:C5,"SELECT A, SUM(B), SUM(C) GROUP BY A")
  • GROUP BY Col - Aggregate rows for the given Col as "key"
  • SUM(Col) - Sum the aggregated column values defined by Group by

Documentation reference:

  • Related