Skip to content

Commit 890207d

Browse files
MKirovaMKirova
authored andcommitted
feat(igxPivot): Add aggregatorName property as aggregator alternative.
1 parent 884dfbb commit 890207d

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid-aggregate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class IgxPivotAggregate {
2828
*
2929
* @memberof IgxPivotAggregate
3030
*/
31-
public static aggregators() {
31+
public static aggregators(): Array<IPivotAggregator> {
3232
return [{
3333
key: 'COUNT',
3434
label: this.resourceStrings.igx_grid_pivot_aggregate_count,

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.interface.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ export interface IPivotAggregator {
5757
key: string;
5858
/** Aggregation label to show in the UI. */
5959
label: string;
60+
/**
61+
* Aggregation name that will be used from a list of predefined aggregations.
62+
* If not set will use the specified aggregator function.
63+
*/
64+
aggregatorName?: PivotAggregationType;
6065
/**
6166
* Aggregator function can be a custom implementation of `PivotAggregation`, or
6267
* use predefined ones from `IgxPivotAggregate` and its variants.
6368
*/
64-
aggregator: (members: any[], data?: any[]) => any;
69+
aggregator?: (members: any[], data?: any[]) => any;
6570
}
6671

6772
/**
@@ -176,6 +181,8 @@ export enum PivotDimensionType {
176181
Filter
177182
}
178183

184+
export type PivotAggregationType = 'SUM' | 'AVG' | 'MIN' | 'MAX' | 'COUNT' | 'LATEST' | 'EARLIEST' ;
185+
179186
/** Interface describing the pivot dimension data.
180187
* Contains additional information needed to render dimension headers.
181188
*/

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-util.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,30 @@ export class PivotUtil {
233233
public static aggregate(records, values: IPivotValue[]) {
234234
const result = {};
235235
for (const pivotValue of values) {
236-
result[pivotValue.member] = pivotValue.aggregate.aggregator(records.map(r => r[pivotValue.member]), records);
236+
const aggregator = PivotUtil.getAggregatorForType(pivotValue.aggregate, pivotValue.dataType);
237+
if (!aggregator) {
238+
throw "No valid aggregator found for: " + pivotValue.member + ". Please set either a valid aggregatorName or aggregator.";
239+
}
240+
result[pivotValue.member] = aggregator(records.map(r => r[pivotValue.member]), records);
237241
}
238242

239243
return result;
240244
}
241245

246+
public static getAggregatorForType(aggregate: IPivotAggregator, dataType: GridColumnDataType) {
247+
let aggregator = aggregate.aggregator;
248+
if (aggregate.aggregatorName) {
249+
let aggregators = IgxPivotNumericAggregate.aggregators();
250+
if (dataType === 'date' || dataType === 'dateTime') {
251+
aggregators = aggregators.concat(IgxPivotDateAggregate.aggregators())
252+
} else if (dataType === 'time') {
253+
aggregators = aggregators.concat(IgxPivotTimeAggregate.aggregators());
254+
}
255+
aggregator = aggregators.find(x => x.key === aggregate.aggregatorName)?.aggregator;
256+
}
257+
return aggregator;
258+
}
259+
242260
public static processHierarchy(hierarchies, pivotKeys, level = 0, rootData = false): IPivotGridRecord[] {
243261
const flatData: IPivotGridRecord[] = [];
244262
hierarchies.forEach((h, key) => {

src/app/pivot-grid/pivot-grid.sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class PivotGridSampleComponent {
372372
member: 'UnitsSold',
373373
aggregate: {
374374
key: 'SUM',
375-
aggregator: IgxPivotNumericAggregate.sum,
375+
aggregatorName: 'SUM',
376376
label: 'Sum'
377377
},
378378
enabled: true,

0 commit comments

Comments
 (0)