javascript - How to group SAPUI5 OData before bind to control at client side? -
i have odata list example in json notation:
var data = [ {"category" : "a", "value" : 1, "group" : "x"}, {"category" : "b", "value" : 2, "group" : "y"}, {"category" : "c", "value" : 3, "group" : "x"}, {"category" : "a", "value" : 4, "group" : "y"}, {"category" : "a", "value" : 5, "group" : "x"} ];
first of filter against group == x;
values left are:
var data = [ {"category" : "a", "value" : 1, "group" : "x"}, {"category" : "c", "value" : 3, "group" : "x"}, {"category" : "a", "value" : 5, "group" : "x"} ];
now group (at client side) category , sum values, result should be:
var data = [ {"category" : "a", "value" : 6, }, {"category" : "c", "value" : 3, }, ];
after bind model sapui5 control.
but grouping part seems not possible.
does know generic solution problem?
a potential use case:
var odataset = new sap.viz.ui5.data.flatteneddataset({ dimensions : [ {axis : 1, value : "{category}", name : "category" } ], measures : [ {value : "{value}", name : "value" } ], data : { path : "/data" } }); var ograph = new sap.viz.ui5.donut({ dataset : odataset, // sap.viz.ui5.data.dataset });
the following code works me. query northwind order_details service couple of products via generic binding, map reduce the returned bindings , aggregate quantity of each order total quanity sold, show results in column chart.
note sap.viz introduces array.prototype.map , array.prototype.reduce functions
var suri = 'http://services.odata.org/northwind/northwind.svc/'; var odatamodel = new sap.ui.model.odata.odatamodel(suri, true); odatamodel.setsizelimit(10000); var ojsonmodel = new sap.ui.model.json.jsonmodel({}, 'jmodel'); // handle list of contexts var handler = function(oevent) { var mapcallback = function(ocontext) { var obj = {}; obj.productid = ocontext.getobject().productid, obj.quantity = ocontext.getobject().quantity return obj; }; var reducecallback = function(aprev, ocurr) { var anext = aprev; var bfound = false; anext.foreach(function(item) { if (item.productid === ocurr.productid) { bfound = true; item.quantity += ocurr.quantity; } }) if (bfound === false) { anext.push(ocurr); } return anext; }; //release handler obindings.detachchange(handler); var atotals = oevent.osource.getcontexts().map(mapcallback).reduce(reducecallback, []); ojsonmodel.setdata({ 'order_totals': atotals }); }; // filter orders 3 products var ofilter1 = new sap.ui.model.filter("productid", sap.ui.model.filteroperator.eq, '1'); var ofilter2 = new sap.ui.model.filter("productid", sap.ui.model.filteroperator.eq, '68'); var ofilter3 = new sap.ui.model.filter("productid", sap.ui.model.filteroperator.eq, '11'); var afilter = [ofilter1, ofilter2, ofilter3]; // sort productid var osorter = new sap.ui.model.sorter("productid", false, true); // reduce returned payload nominating need fields var oselect = { select: 'productid,quantity' } var obindings = odatamodel.bindlist("/order_details", null, osorter, afilter, oselect); // call odata service , handle results obindings.attachchange(handler); obindings.getcontexts(); var odataset = new sap.viz.ui5.data.flatteneddataset({ dimensions: [{ axis: 1, name: 'productid', value: "{productid}" }], measures: [{ name: 'quantity sold', value: '{quantity}' }], data: { path: "/order_totals" } }); var ocolumnchart = new sap.viz.ui5.column({ width: "80%", height: "400px", plotarea: { 'colorpalette': d3.scale.category20().range() }, title: { visible: true, text: 'qutantity sold product' }, dataset: odataset }); ocolumnchart.setmodel(ojsonmodel);
Comments
Post a Comment