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. You may create your own functor – a function that gets two parameters:
and returns the result value.
Let's take the example about sales of 4 companies described in the "Grouping" article. Here the "sum" functor was used to get the total sales of a company.
Let's now create a functor that will display the average sales. In this case grouping can be implemented 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;
}
Back to top