Reputation: 49
I have a significant amount of data loading dynamically into a DataTable. I'm looking to do several grouping filters to cut the data down. Here is a small example...
cols: 0 = marketId, 1 = storeId, 2 = question1, 3 = question2, ..., n = questionN
row = 1 survey
market1 | 1234 | yes | no | ... | no
market1 | 2345 | no | no | ... | yes
market1 | 3456 | yes | yes | ... | no
market2 | 4567 | no | no | ... | yes
market2 | 5678 | no | yes | ... | yes
market3 | 6789 | yes | no | ... | yes
I'm looking to use grouping (and I'm assuming some creative joining too) to chop this down a bit. I need to:
1. count the store ids for each unique market id and return a total number of stores per marketId in its own column
2. for each marketId, count the number of yes and no responses per question and post the results in their own column.
cols: 0 = marketId, 1 = num of stores in market, 2 = question1(num of yes), 3 = question1(num of no), 4 = question2(num of yes), 5 = question2(num of no), ..., n-1 = questionN(num of yes), n = questionN(num of no)
row = survey summary
market1 | 3 | 2 | 1 | 1 | 2 | ... | 1 | 2
market2 | 2 | 0 | 2 | 1 | 1 | ... | 2 | 0
market3 | 1 | 1 | 0 | 0 | 1 | ... | 1 | 0
I've looked through the Google Charts Visualization Documentation and have had a little luck in some basic grouping, but I've never tried something this complex before. I'm wondering if this amount of filtering possible, and if so - a point in the right direction would be much appreciated.
Upvotes: 0
Views: 110
Reputation: 26340
Filtering and grouping are two different things: filtering restricts your data, grouping aggregates your data. What you want seems to be grouping rather than filtering, so here's some basic code that should do what you want:
var groupColumns = [{
// count of store ids
type: 'number',
column: 1,
label: 'Count of ' + data.getColumnLabel(1),
aggregation: google.visualization.data.count
}];
for (var i = 2, length = data.getNumberOfColumns(); i < length; i++) {
groupColumns.push({
// count of yes's in column i
type: 'number',
column: i,
label: 'Count of yes\'s in ' + data.getColumnLabel(1),
aggregation: function (vals) {
var cnt = 0;
for (var j = 0; j < vals.length; j++) {
if (vals[j] == 'yes') {
cnt++;
}
}
return cnt;
}
});
groupColumns.push({
// count of no's in column i
type: 'number',
column: i,
label: 'Count of no\'s in ' + data.getColumnLabel(1),
aggregation: function (vals) {
var cnt = 0;
for (var j = 0; j < vals.length; j++) {
if (vals[j] == 'no') {
cnt++;
}
}
return cnt;
}
});
}
var groupedData = google.visualization.data.group(data, [0], groupColumns);
Upvotes: 1