Skip to main content

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[]
ParameterTypeRequiredDefaultsDescription
dataArrayObject[]trueData array to aggregate
aggregateBy NamestringtrueName of the aggregated field in the new set of data
aggregateBy FieldstringtrueField to use in the aggregation
operations NamestringfalseOperation result field name
operations FieldstringfalseField to use in the operation
OperationType"sum" | "count" | "max" | "percent" | "custom"falseOperation to perform
CustomFunctionFunctionfalseOperation to perform when operationType is custom
sort fieldsstring[]falseFields to use in the order by
sort order"ascendent" | "descendent"falseOrder type
noZerosstring[]falseTo 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.

Example

img-box-shadow