// 2016-08-08 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>// The following snippet of code dynamically updates one of the records of the data used for BI computations - see !!! test !!! part// This scenario can be applied when it is necessary to manually adjust BI computations, for example in some 2-step algorithm.// Let's assume that we have data with the following fields:// id, TYPE, VALUE, WHATEVER// and that the measure would be computed using and equation, let's say `$sum(floor(VALUE))`// but another part of the algorithm re-evaluates the computations using `ceil(VALUE)` instead of `floor(VALUE)` for top 10 records.// // We cannot express such a computation in the Expression because it is using context information that cannot be accessed from the expression.// But we can manually adjust the computations (and later "attach" this adjustment to the "filtered" chart event if necessary)//// To do that, let's configure a custom field in the wizard - let's call it `adjust` and assign it a value `0`. Then instead of// `$sum(floor(VALUE))// use// `$sum(floor(VALUE) + adjust)//// Notice that in the standard computation the values won't change - `adjust` value is always `0`.// Here comes the following snippet of code - on demand we can update `adjust` property for certain record (for record with ID=55 in this case)// and set it to `1`//// Then after applying the update and redrawing the "charts", the computation uses floor(VALUE) + 1 for the record with ID=55. And this is what we wanted// to achieve!//gsp.ready(function(gsp) { // uncomment to use it in the browser javascript consolegsp.bi.stage.requireLibraries(function(gvc){gsp.bi.stage.findStage(null,function(stage){varcharts=gvc.dc.chartRegistry.list(stage.id());varfilterDict={};varcf=stage.facts();// crossfilter instance// temporaly suppress current filtersfunctionsuppressFilters(){charts.forEach(function(chart){filterDict[chart.id()]=chart.filters();chart.filterAll();});}// resume previously suppressed filtersfunctionresumeFilters(){charts.forEach(function(chart){if(filterDict[chart.id()]){filterDict[chart.id()].map(chart.filter);deletefilterDict[chart.id()];}});}// function for filtering unique records by IDfunctionfilterById(record){returnrecord.id;}// dimension on unique IDsvaridDim=cf.dimension(filterById);// function for dynamically modifying record's value, for example after some computationsfunctionupdate_record(id,changes){suppressFilters();idDim.filter(id);varrecord=idDim.top(Infinity)[0];cf.remove();for(varkeyNameinchanges){record[keyName]=changes[keyName];}cf.add([record]);idDim.filterAll();resumeFilters();}// !!! begin test !!!update_record("55",{adjust:1});charts.map(function(ch){ch.redraw()})// !!! end test !!!});});//}); // uncomment to use it in the browser javascript console
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.