Oglala Lakota College Archives
You can define certain data manipulation functions and create new derived data in the displays.
Convert Data
The convertData attribute of a display takes the form of:
{{display_linechart   
convertData="func1(args); func2(args);"
}}
Where convertData can be any number of ";" delimited functions. The available functions are:

Replace:
convertData="replace(fields=field_ids, pattern=,with=);"
TBD:
convertData="roundDate(round=hour|day|week|month|year);"
TBD:
filterDate(one of month=0);
TBD:
groupTime(field=field to group time on);
Merge rows:
mergeRows(keyFields=f1_comma_f2, operator=count|sum|average, valueFields=);
Add percent increase:
addPercentIncrease(replaceValues=false);
Calculate # days to double:
doublingRate(fields=f1\\,f2, keyFields=f3);
Add a fixed value:
addFixed(id=field_name, value=3700,type=double);
TBD:
accum(fields=);
TBD:
mean(fields=);
Uniquify rows:
unique(groupFields=f1\\,f2,valueField=);
Count uniques:
count(field=,sort=true);
Unfurl:
unfurl(headerField=field to get header from,uniqueField=e.g. date,valueFields=);
Rotate data:
rotateData(includeFields=true,includeDate=true,flipColumns=true);
Prune where fields are all NaN:
prune(fields=);
Scale and offset:
accum(scale=1,offset1=0,offset2=0,unit=,fields=);

where we scale the value as:
(value + offset1) * scale + offset2
Specifying a function
You can also specify your own function with the derived convertData. For example, lets say you have data with values a and b and you want to add a new field to the data "a+b". To do this simply specify a convertData attribute:
{{display_linechart   fields="a_and_b"
convertData="derived(field=a_and_b, function=return a+b)"}}
The derived function takes a field name and a function that is applied to each record in turn.

A more complicated example is below. Here we are using a separately defined Javascript function to take a dataset that contains a "discharge" field which is cubic feet/second of water flow and calculate both a volume for each record time step as well as a running total. The display has a convertData attribute. This has a "fields" attribute which is a comma separated list of field names (note the escaped comma). If just one field you can also use a "field" attribute. This also has an optional "units" attribute. The function needs to be of the form: "return function_name(args)".
{{display_linechart
convertData="derived(fields=volume\,total_volume, units=acre feet\,acre feet, function=return calculateVolume(args));" 
entry=76c9e5a1-2875-4a08-9b45-d106e9b1f341   fields="volume,total_volume"}}
To define the javascript you can include javascript inline with the "+javascript/-javascript" wiki tag. This gets called for every record. The args object contains:
{
values:{},
recordIndex:rowIdx,
record:record
};
Where:
  • values - is a map holding the record values, e.g. values[1], values[2]
  • state - an object that exists
  • recordIndex
  • record - the data record
The args object also is the same object call by call so you can store other values in it. Here is the calculateVolume function:
+javascript
function calculateVolume(args) {
    if(!args.lastDate) {
    //if first record then initialize date and total_volume
        args.lastDate = args.record.getDate();
        args.total_volume = 0;
        return [0,0];
    }
    //calculate the volume for this record
    let seconds = (args.record.getDate().getTime()-args.lastDate.getTime())/1000;
    args.lastDate = args.record.getDate();
    let discharge = args.values['discharge'];
    if(isNaN(discharge)) return NaN;
    let volume = discharge*seconds/43560;
    args.total_volume+=volume;
    return [volume,args.total_volume];
}

-javascript