You can pivot the data as in my eample, and then use a DataView to create your "Others" column:
var othersView = new google.visualization.DataView(pivotedData);
othersView.setColumns([0, 1, 3, {
type: 'number',
label: 'Others',
calc: function (dt, row) {
return dt.getValue(row, 2) + dt.getValue(row, 4);
}
}]);
If I understand your intentions correctly, you'll want to dynamically calculate which columns are the top 2. You'll have to write some logic to determine that, and then you can use something like this:
var top2; // array containing the column indices of the top 2 columns
var othersViewColumns = [0].concat(top2);
othersViewColumns.push({
type: 'number',
label: 'Others',
calc: function (dt, row) {
var total = 0; for (var i = 1; i < dt.getNumberOfColumns(); i++) { total += (top2.indexOf(i) == -1) ? dt.getValue(row, i) : 0; } return total;
}
});
var othersView = new google.visualization.DataView(pivotedData);
othersView.setColumns(othersViewColumns);