Data
The namespace Data provides to developers, methods to easily manipulate data.
//accessing to data methods
SW.Data.{methodName}
aggregate
Description
This method can be used to aggregate data and perform operations, like sum, count, max or custom, when aggregating data.
It can also remove rows where a field is equal to zero (noZeros) and order them (sort).
Method(s)
1 function aggregate(
dataArray: Object[],
aggregateBy: [{ Name: string; Field: string }],
params?: {
operations: [
{
Name: string;
Field: string;
OperationType: "sum" | "count" | "max" | "percent" | "custom";
CustomFunction?: Function;
}
];
sort: {
fields: string[];
order: "ascendent" | "descendent";
};
noZeros: string[];
}
): any[]
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
dataArray | Object[] | true | Data array to aggregate | |
aggregateBy Name | string | true | Name of the aggregated field in the new set of data | |
aggregateBy Field | string | true | Field to use in the aggregation | |
operations Name | string | false | Operation result field name | |
operations Field | string | false | Field to use in the operation | |
OperationType | "sum" | "count" | "max" | "percent" | "custom" | false | Operation to perform | |
CustomFunction | Function | false | Operation to perform when operationType is custom | |
sort fields | string[] | false | Fields to use in the order by | |
sort order | "ascendent" | "descendent" | false | Order type | |
noZeros | string[] | false | To remove rows where a given field is zero |
Basic Usage
SW.Data.aggregate(dataArray, [{ Name: 'AggregatedName', Field: 'UserName' }],
{
operations: [
{ Name: 'SumAge', Field: 'Age', OperationType: 'sum' },
{ Name: 'MaxAge', Field: 'Age', OperationType: 'max' },
{ Name: 'CountRows', OperationType: 'count' },
{ Name: 'AverageYearsInJob', Field: 'Age', Reference: 'NumberOfJobs' OperationType: 'percent' }
],
grandTotals: [
{ Name: "AverageYearsInJobTotal", Field: "Age", Reference: "NumberOfJobs", OperationType: "percent" }
],
sort: {
fields: ['AggregatedName', 'MaxAge'];
order: "ascendent";
},
noZeros: ['SumAge']
});
Response
It will return a new set of data aggregated by UserName with the fields:
- AggregatedName: choosen name for the aggregation field
- SumAge: sum of the age where the UserName is the same
- MaxAge: max age where the UserName is the same
- CountRows: count of the rows where the UserName is the same
- AverageYearsInJob: will apply the following calculation Field / Reference, in this case Age / NumberOfJobs
- GrandTotals:
- AverageYearsInJobTotal: it will sum all of the Age and NumberOfJobs and divide them
It will be sorted by the fields AggregatedName and MaxAge in an ascendent order and it will remove rows where the SumAge is equal to zero.