Miretz
Miretz

Reputation: 154

google.visualisation.DataTable() merge rows

I have this data in a Google DataTable:

project1 | system1 | 5
project2 | system2 | 2
project1 | system1 | 5
project3 | system4 | 1

I need to merge rows that have the same projects and systems. So the result would be:

project1 | system1 | 10
project2 | system2 | 2
project3 | system4 | 1

the number must be a sum of the values in the respective columns.

Is there a build in method in the DataTable to achieve this or can somebody give me a hint how to do this without the need of typical iterating through the table and comparing on each row.

Upvotes: 2

Views: 1902

Answers (1)

HansMaulwurf
HansMaulwurf

Reputation: 348

populate your DataTable and use the group-Function for this: https://developers.google.com/chart/interactive/docs/reference#google_visualization_data_group

Your example should fit into something like this:

var result = google.visualization.data.group(
  // input data
  your_data_table_obj,
  // key columns (columns to group by)
  [0, 1],
  // third column (index 2) will be summed
  [{'column': 2, 'aggregation': google.visualization.data.sum, 'type': 'number'}]
);

One last thing: you are talking about Grouping not Merging. :-)

Upvotes: 5

Related Questions