Create aggregation function to report most popular in Google DataTable -
i have google datatable looks this:
var data = new google.visualization.datatable(); data.addcolumn('string', 'city'); data.addcolumn('string', 'model');
data.addcolumn('number', 'sold'); data.addrows([ ["melbourne","ford",10], ["perth","ford",2], ["melbourne","ford",7], ["melbourne","holden",25], ["perth","holden",25], ["melbourne","holden",12], ["melbourne","ford",11] ]);
what group city , report model highest cumulative sold value city.
the result returned :
city model melbourne holden perth ford
from reading of google visualisation api possible write custom aggregation function beyond limited experience.
any assistance appreciated.
this require double grouping: first time total sold per model per city, , second time popular model.
var g1 = google.visualization.data.group(data, [0, 1], [{ type: 'number', label: 'total sold', column: 2, aggregation: google.visualization.data.sum }]); // create view merge columns 1 , 2 grouping below var view = new google.visualization.dataview(g1); view.setcolumns([0, { type: 'string', label: 'model qty sold', calc: function (dt, row) { var o = {model: dt.getvalue(row, 1), sold: dt.getvalue(row, 2)}; return json.stringify(o); } }]); var g2 = google.visualization.data.group(data, [0], [{ type: 'string' label: 'most popular' aggregation: function (values) { var max = null, ret = null; (var = 0; < values; i++) { var o = json.parse(values[i]); if (max == null || o.sold > max) { max = o.sold; ret = o.model; } } return ret; } }]);
g2
should contain model highest sales grouped city. if there tie between 2 models, return first model in values
array (likely values
ordered model name, not guaranteed).
Comments
Post a Comment