Skip to content

Commit 0c10753

Browse files
committed
feat(pivot-aggr): adding default aggregates
1 parent 0b84199 commit 0c10753

File tree

8 files changed

+188
-45
lines changed

8 files changed

+188
-45
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { IgxDateSummaryOperand, IgxNumberSummaryOperand, IgxTimeSummaryOperand } from '../summaries/grid-summary';
2+
3+
4+
export class IgxPivotAggregate {
5+
/**
6+
* Counts all the records in the data source.
7+
* If filtering is applied, counts only the filtered records.
8+
* ```typescript
9+
* IgxSummaryOperand.count(dataSource);
10+
* ```
11+
*
12+
* @memberof IgxPivotAggregate
13+
*/
14+
public static count(members: number[]): number {
15+
return members.length;
16+
}
17+
}
18+
19+
export class IgxPivotNumericAggregate extends IgxPivotAggregate {
20+
/**
21+
* Returns the minimum numeric value in the provided data records.
22+
* If filtering is applied, returns the minimum value in the filtered data records.
23+
* ```typescript
24+
* IgxPivotNumericAggregate.min(members, data);
25+
* ```
26+
*
27+
* @memberof IgxPivotNumericAggregate
28+
*/
29+
public static min(members: number[]): number {
30+
return IgxNumberSummaryOperand.min(members);
31+
}
32+
33+
/**
34+
* Returns the maximum numeric value in the provided data records.
35+
* If filtering is applied, returns the maximum value in the filtered data records.
36+
* ```typescript
37+
* IgxPivotNumericAggregate.max(data);
38+
* ```
39+
*
40+
* @memberof IgxPivotNumericAggregate
41+
*/
42+
public static max(members: number[]): number {
43+
return IgxNumberSummaryOperand.max(members);
44+
}
45+
46+
/**
47+
* Returns the sum of the numeric values in the provided data records.
48+
* If filtering is applied, returns the sum of the numeric values in the data records.
49+
* ```typescript
50+
* IgxPivotNumericAggregate.sum(data);
51+
* ```
52+
*
53+
* @memberof IgxPivotNumericAggregate
54+
*/
55+
public static sum(members: number[]): number {
56+
return IgxNumberSummaryOperand.sum(members);
57+
}
58+
59+
/**
60+
* Returns the average numeric value in the data provided data records.
61+
* If filtering is applied, returns the average numeric value in the filtered data records.
62+
* ```typescript
63+
* IgxPivotNumericAggregate.average(data);
64+
* ```
65+
*
66+
* @memberof IgxPivotNumericAggregate
67+
*/
68+
public static average(members: number[]): number {
69+
return IgxNumberSummaryOperand.average(members);
70+
}
71+
}
72+
73+
export class IgxPivotDateAggregate extends IgxPivotAggregate {
74+
/**
75+
* Returns the latest date value in the data records.
76+
* If filtering is applied, returns the latest date value in the filtered data records.
77+
* ```typescript
78+
* IgxPivotDateAggregate.latest(data);
79+
* ```
80+
*
81+
* @memberof IgxPivotDateAggregate
82+
*/
83+
public static latest(members: number[]) {
84+
return IgxDateSummaryOperand.latest(members);
85+
}
86+
87+
/**
88+
* Returns the earliest date value in the data records.
89+
* If filtering is applied, returns the latest date value in the filtered data records.
90+
* ```typescript
91+
* IgxPivotDateAggregate.earliest(data);
92+
* ```
93+
*
94+
* @memberof IgxPivotDateAggregate
95+
*/
96+
public static earliest(members: number[]) {
97+
return IgxDateSummaryOperand.earliest(members);
98+
}
99+
}
100+
101+
export class IgxPivotTimeAggregate extends IgxPivotAggregate {
102+
/**
103+
* Returns the latest time value in the data records. Compare only the time part of the date.
104+
* If filtering is applied, returns the latest time value in the filtered data records.
105+
* ```typescript
106+
* IgxPivotTimeAggregate.latestTime(data);
107+
* ```
108+
*
109+
* @memberof IgxPivotTimeAggregate
110+
*/
111+
public static latestTime(members: number[]) {
112+
return IgxTimeSummaryOperand.latestTime(members);
113+
}
114+
115+
/**
116+
* Returns the earliest time value in the data records. Compare only the time part of the date.
117+
* If filtering is applied, returns the earliest time value in the filtered data records.
118+
* ```typescript
119+
* IgxPivotTimeAggregate.earliestTime(data);
120+
* ```
121+
*
122+
* @memberof IgxPivotTimeAggregate
123+
*/
124+
public static earliestTime(members: number[]) {
125+
return IgxTimeSummaryOperand.earliestTime(members);
126+
}
127+
}

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
100100
* @hidden
101101
*/
102102
public ngAfterContentInit() {
103-
// ignore any user defined columns and auto-generate based on pivot config.
104-
this.columnList.reset([]);
105-
Promise.resolve().then(() => {
106-
this.setupColumns();
107-
});
103+
// ignore any user defined columns and auto-generate based on pivot config.
104+
this.columnList.reset([]);
105+
Promise.resolve().then(() => {
106+
this.setupColumns();
107+
});
108108
}
109109

110110
/** @hidden */
@@ -205,18 +205,18 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
205205
return MINIMUM_COLUMN_WIDTH * rowDimCount;
206206
}
207207

208-
public toggleColumn(col: IgxColumnComponent) {
209-
const state = this.columnGroupStates.get(col.field);
210-
const newState = !state;
211-
this.columnGroupStates.set(col.field, newState);
212-
this.toggleGroup(col, newState);
213-
this.reflow();
214-
}
208+
public toggleColumn(col: IgxColumnComponent) {
209+
const state = this.columnGroupStates.get(col.field);
210+
const newState = !state;
211+
this.columnGroupStates.set(col.field, newState);
212+
this.toggleGroup(col, newState);
213+
this.reflow();
214+
}
215215

216-
protected toggleGroup(col: IgxColumnComponent, newState: boolean) {
216+
protected toggleGroup(col: IgxColumnComponent, newState: boolean) {
217217
if (this.hasMultipleValues) {
218-
const fieldColumns = col.children.filter(x => !x.columnGroup);
219-
const groupColumns = col.children.filter(x => x.columnGroup);
218+
const fieldColumns = col.children.filter(x => !x.columnGroup);
219+
const groupColumns = col.children.filter(x => x.columnGroup);
220220
groupColumns.forEach(groupColumn => {
221221
groupColumn.hidden = newState;
222222
this.resolveToggle(groupColumn);
@@ -226,23 +226,23 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
226226
});
227227
} else {
228228
const parentCols = col.parent ? col.parent.children : this.columns.filter(x => x.level === 0);
229-
const fieldColumn = parentCols.filter(x => x.header === col.header && !x.columnGroup)[0];
230-
const groupColumn = parentCols.filter(x => x.header === col.header && x.columnGroup)[0];
229+
const fieldColumn = parentCols.filter(x => x.header === col.header && !x.columnGroup)[0];
230+
const groupColumn = parentCols.filter(x => x.header === col.header && x.columnGroup)[0];
231231
groupColumn.hidden = newState;
232232
this.resolveToggle(groupColumn);
233233
fieldColumn.hidden = !newState;
234234
if (newState) {
235-
fieldColumn.headerTemplate = this.headerTemplate;
235+
fieldColumn.headerTemplate = this.headerTemplate;
236236
} else {
237-
fieldColumn.headerTemplate = undefined;
237+
fieldColumn.headerTemplate = undefined;
238238
}
239239
}
240-
}
240+
}
241241

242-
protected resolveToggle(groupColumn: IgxColumnComponent) {
243-
const hasChildGroup = groupColumn.children.filter(x => x.columnGroup).length > 0;
244-
if (!groupColumn.hidden && hasChildGroup) {
245-
const fieldChildren = groupColumn.children.filter(x => !x.columnGroup);
242+
protected resolveToggle(groupColumn: IgxColumnComponent) {
243+
const hasChildGroup = groupColumn.children.filter(x => x.columnGroup).length > 0;
244+
if (!groupColumn.hidden && hasChildGroup) {
245+
const fieldChildren = groupColumn.children.filter(x => !x.columnGroup);
246246
const groupChildren = groupColumn.children.filter(x => x.columnGroup);
247247
groupChildren.forEach(group => {
248248
this.resolveToggle(group);
@@ -273,8 +273,8 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
273273
data,
274274
this.pivotConfiguration.columns,
275275
PivotDimensionType.Column,
276-
{aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'}
277-
);
276+
{ aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
277+
);
278278
const columns = this.generateColumnHierarchy(fieldsMap, data);
279279
this._autoGeneratedCols = columns;
280280

@@ -291,15 +291,15 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
291291
fields.forEach((value, key) => {
292292
if (value.children == null || value.children.length === 0 || value.children.size === 0) {
293293
const ref = this.hasMultipleValues ?
294-
factoryColumnGroup.create(this.viewRef.injector) :
295-
factoryColumn.create(this.viewRef.injector);
294+
factoryColumnGroup.create(this.viewRef.injector) :
295+
factoryColumn.create(this.viewRef.injector);
296296
ref.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
297297
ref.instance.field = key;
298298
ref.instance.parent = parent;
299299
ref.changeDetectorRef.detectChanges();
300300
columns.push(ref.instance);
301301
if (this.hasMultipleValues) {
302-
const measureChildren = this.getMeasureChildren(factoryColumn, data , ref.instance, false);
302+
const measureChildren = this.getMeasureChildren(factoryColumn, data, ref.instance, false);
303303
ref.instance.children.reset(measureChildren);
304304
columns = columns.concat(measureChildren);
305305
}
@@ -312,7 +312,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
312312
if (value.expandable) {
313313
ref.instance.headerTemplate = this.headerTemplate;
314314
}
315-
if(!this.hasMultipleValues) {
315+
if (!this.hasMultipleValues) {
316316
const refSibling = factoryColumn.create(this.viewRef.injector);
317317
refSibling.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
318318
refSibling.instance.field = key;
@@ -326,7 +326,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
326326
ref.changeDetectorRef.detectChanges();
327327
columns.push(ref.instance);
328328
if (this.hasMultipleValues) {
329-
const measureChildren = this.getMeasureChildren(factoryColumn, data , ref.instance, true);
329+
const measureChildren = this.getMeasureChildren(factoryColumn, data, ref.instance, true);
330330
const nestedChildren = filteredChildren.concat(measureChildren);
331331
const allChildren = children.concat(measureChildren);
332332
ref.instance.children.reset(nestedChildren);
@@ -345,7 +345,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
345345
const cols = [];
346346
this.pivotConfiguration.values.forEach(val => {
347347
const ref = colFactory.create(this.viewRef.injector);
348-
ref.instance.header = val.member;
348+
ref.instance.header = val.displayName || val.member;
349349
ref.instance.field = parent.field + '-' + val.member;
350350
ref.instance.parent = parent;
351351
ref.instance.hidden = hidden;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { FilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree';
22

3+
4+
export type PivotAggregation = (members: any[], data: any[]) => any;
5+
36
export interface IPivotConfiguration {
47
rows: IPivotDimension[] | null;
58
columns: IPivotDimension[] | null;
@@ -21,8 +24,10 @@ export interface IPivotDimension {
2124

2225
export interface IPivotValue {
2326
member: string;
27+
// display name if present shows instead of member for the column header of this value
28+
displayName?: string;
2429
// aggregate function - can use one of the predefined like IgxNumberSummaryOperand.sum etc.
25-
aggregate: (data: any[]) => any;
30+
aggregate: PivotAggregation;
2631
// Enables/Disables a particular value from pivot aggregation.
2732
enabled: boolean;
2833
// Allow conditionally styling of the IgxPivotGrid cells

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class PivotUtil {
101101
public static aggregate(records, values: IPivotValue[]) {
102102
const result = {};
103103
for (const pivotValue of values) {
104-
result[pivotValue.member] = pivotValue.aggregate(records.map(r => r[pivotValue.member]));
104+
result[pivotValue.member] = pivotValue.aggregate(records.map(r => r[pivotValue.member]), records);
105105
}
106106

107107
return result;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './pivot-grid.component';
22
export * from './pivot-grid.interface';
3+
export * from './pivot-grid-aggregate';
34
export * from './pivot-grid.module';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<div class="sample-column">
2-
<igx-pivot-grid #grid1 [data]="origData" [pivotConfiguration]="pivotConfigHierarchy">
2+
<igx-pivot-grid #grid1 [width]="'1500px'" [height]="'100%'" [data]="origData" [pivotConfiguration]="pivotConfigHierarchy">
33
</igx-pivot-grid>
44
</div>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
:host ::ng-deep {
22
.upFont {
3-
color: green;
3+
color: lightgreen;
44
}
55

66
.downFont {
7-
color:red;
7+
color: lightcoral;
88
}
99

1010
.upFont1 {
11-
color: blue;
11+
color: skyblue;
1212
}
1313

1414
.downFont1 {
15-
color:yellow;
15+
color: yellow;
1616
}
1717

1818
.sample-column {

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { Component, ViewChild } from '@angular/core';
2-
import { IgxNumberSummaryOperand, IgxPivotGridComponent, IPivotConfiguration } from 'igniteui-angular';
2+
import {
3+
IgxPivotNumericAggregate,
4+
IgxPivotGridComponent,
5+
IPivotConfiguration,
6+
PivotAggregation
7+
} from 'igniteui-angular';
38
import { HIERARCHICAL_SAMPLE_DATA } from '../shared/sample-data';
49

10+
export class IgxTotalSaleAggregate {
11+
public static totalSale: PivotAggregation = (members, data: any) =>
12+
data.reduce((accumulator, value) => accumulator + value.UnitPrice * value.UnitsSold, 0) ;
13+
}
14+
515
@Component({
616
providers: [],
717
selector: 'app-tree-grid-sample',
818
styleUrls: ['pivot-grid.sample.scss'],
919
templateUrl: 'pivot-grid.sample.html'
1020
})
11-
1221
export class PivotGridSampleComponent {
1322
@ViewChild('grid1', { static: true }) public grid1: IgxPivotGridComponent;
1423

@@ -36,7 +45,7 @@ export class PivotGridSampleComponent {
3645
values: [
3746
{
3847
member: 'UnitsSold',
39-
aggregate: IgxNumberSummaryOperand.sum,
48+
aggregate: IgxPivotNumericAggregate.sum,
4049
enabled: true
4150
}
4251
],
@@ -83,7 +92,7 @@ export class PivotGridSampleComponent {
8392
values: [
8493
{
8594
member: 'UnitsSold',
86-
aggregate: IgxNumberSummaryOperand.sum,
95+
aggregate: IgxPivotNumericAggregate.sum,
8796
enabled: true,
8897
styles: {
8998
upFont: (rowData: any, columnKey: any): boolean => rowData[columnKey] > 300,
@@ -93,8 +102,9 @@ export class PivotGridSampleComponent {
93102
formatter: (value) => value ? value + '$' : undefined
94103
},
95104
{
96-
member: 'UnitPrice',
97-
aggregate: IgxNumberSummaryOperand.sum,
105+
member: 'AmountOfSale',
106+
displayName: 'Amount of Sale',
107+
aggregate: IgxTotalSaleAggregate.totalSale,
98108
enabled: true,
99109
dataType: 'currency',
100110
styles: {

0 commit comments

Comments
 (0)