DHTMLX Docs & Samples Explorer

Grouping functors

Properties of grouped objects are defined by an array. The first element of this array is a template with a property from original data, the second one – the functor which needs being applied to all values of this property in a group. There are “sum”, “max” and “min” functors. But you may create the new one – the function that gets two parameters:

  • the value template
  • the array with all data object in a group

and returns the result value.

Lets take the example about sales of 4 companies that is described in the “Grouping” article. Here “sum” functor was used to get the total sales of a company. But if you need to represent the average sales, grouping could be done as follows:

chart.group({
     by:"#company#",
     map:{
        sales:["#sales#",getAverage]
     }
});
 
function getAverage(prop,data){
    var count = data.length;
    var summ = 0; 
    for(var i = 0; i < count; i++){
        summ += parseFloat(data[i].sales);
    }
    return summ/count;
}