Skip to content

Commit 5e48b93

Browse files
authored
Merge branch 'master' into mdragnev/refactor-density
2 parents 1c1af6a + 7343afa commit 5e48b93

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export class IgxPivotDateDimension implements IPivotDimension {
144144
constructor(inBaseDimension: IPivotDimension, inOptions: IPivotDateDimensionOptions = {}) {
145145
this._baseDimension = inBaseDimension;
146146
this._options = inOptions;
147+
this.enabled = inBaseDimension.enabled;
147148
if (this.baseDimension && this.options) {
148149
this.initialize(this.baseDimension, this.options);
149150
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import { IgxColumnResizingService } from '../resizing/resizing.service';
9999
import { DefaultDataCloneStrategy, IDataCloneStrategy } from '../../data-operations/data-clone-strategy';
100100
import { IgxTextHighlightService } from '../../directives/text-highlight/text-highlight.service';
101101
import { IgxPivotRowHeaderGroupComponent } from './pivot-row-header-group.component';
102+
import { IgxPivotDateDimension } from './pivot-grid-dimensions';
102103

103104
let NEXT_ID = 0;
104105
const MINIMUM_COLUMN_WIDTH = 200;
@@ -328,6 +329,19 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
328329
return this._pivotConfiguration || { rows: null, columns: null, values: null, filters: null };
329330
}
330331

332+
/**
333+
* Gets/Sets whether to auto-generate the pivot configuration based on the provided data.
334+
*
335+
* @remarks
336+
* The default value is false. When set to true, it will override all dimensions and values in the pivotConfiguration.
337+
* @example
338+
* ```html
339+
* <igx-pivot-grid [data]="Data" [autoGenerateConfig]="true"></igx-pivot-grid>
340+
* ```
341+
*/
342+
@Input({ transform: booleanAttribute })
343+
public autoGenerateConfig = false;
344+
331345
@Input()
332346
/**
333347
* Gets/Sets the pivot ui settings for the pivot grid - chips and their
@@ -1001,6 +1015,9 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10011015
// ignore any user defined columns and auto-generate based on pivot config.
10021016
this.updateColumns([]);
10031017
Promise.resolve().then(() => {
1018+
if (this.autoGenerateConfig) {
1019+
this.generateConfig();
1020+
}
10041021
this.setupColumns();
10051022
});
10061023
if (this.valueChipTemplateDirective) {
@@ -1093,6 +1110,9 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10931110
public set data(value: any[] | null) {
10941111
this._data = value || [];
10951112
if (!this._init) {
1113+
if (this.autoGenerateConfig) {
1114+
this.generateConfig();
1115+
}
10961116
this.setupColumns();
10971117
this.reflow();
10981118
}
@@ -2169,6 +2189,67 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
21692189
return columns;
21702190
}
21712191

2192+
2193+
protected generateConfig() {
2194+
if (!this.data) return;
2195+
2196+
const data = this.data;
2197+
const fields = this.generateDataFields(data);
2198+
const columnDimensions: IPivotDimension[] = [];
2199+
const rowDimensions: IPivotDimension[] = [];
2200+
const values: IPivotValue[] = [];
2201+
let isFirstDate = true;
2202+
fields.forEach((field) => {
2203+
const dataType = this.resolveDataTypes(data[0][field]);
2204+
switch (dataType) {
2205+
case "number":
2206+
{
2207+
const value: IPivotValue = {
2208+
member: field,
2209+
displayName: field,
2210+
dataType: dataType,
2211+
aggregate: {
2212+
key: 'sum',
2213+
label: 'Sum',
2214+
aggregatorName: "SUM"
2215+
},
2216+
enabled: true
2217+
};
2218+
values.push(value);
2219+
break;
2220+
}
2221+
case "date":
2222+
{
2223+
const dimension: IPivotDimension = new IgxPivotDateDimension(
2224+
{
2225+
memberName: field,
2226+
enabled: isFirstDate,
2227+
dataType: dataType
2228+
}
2229+
)
2230+
rowDimensions.push(dimension);
2231+
isFirstDate = false;
2232+
break;
2233+
}
2234+
default: {
2235+
const dimension: IPivotDimension = {
2236+
memberName: field,
2237+
enabled: false,
2238+
dataType: dataType
2239+
};
2240+
columnDimensions.push(dimension);
2241+
break;
2242+
}
2243+
}
2244+
});
2245+
const config: IPivotConfiguration = {
2246+
columns: columnDimensions,
2247+
rows: rowDimensions,
2248+
values: values
2249+
};
2250+
this.pivotConfiguration = config;
2251+
}
2252+
21722253
protected createColumnForDimension(value: any, data: any, parent: ColumnType, isGroup: boolean) {
21732254
const key = value.value;
21742255
const ref = isGroup ?

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,36 @@ describe('IgxPivotGrid #pivotGrid', () => {
19781978
expect(rowDimension.width).toBe('auto');
19791979
expect(pivotGrid.rowDimensionWidthToPixels(rowDimension)).toBe(158);
19801980
});
1981+
1982+
it ('should auto-generate pivot config when autoGenerateConfig is set to true.', () => {
1983+
const pivotGrid = fixture.componentInstance.pivotGrid;
1984+
pivotGrid.pivotConfiguration = undefined;
1985+
pivotGrid.data = [];
1986+
fixture.detectChanges();
1987+
1988+
expect(pivotGrid.pivotConfiguration).toEqual({ rows: null, columns: null, values: null, filters: null });
1989+
pivotGrid.autoGenerateConfig = true;
1990+
fixture.detectChanges();
1991+
expect(pivotGrid.pivotConfiguration).toEqual({ rows: null, columns: null, values: null, filters: null });
1992+
1993+
pivotGrid.data = [{
1994+
ProductCategory: 'Clothing', UnitPrice: 12.81, SellerName: 'Stanley',
1995+
Country: 'Bulgaria', Date: new Date('01/01/2021'), UnitsSold: 282
1996+
}];
1997+
fixture.detectChanges();
1998+
1999+
expect(pivotGrid.allDimensions.length).toEqual(4);
2000+
// only date is row dimension and is enabled by default
2001+
expect(pivotGrid.pivotConfiguration.rows.map(x => x.memberName)).toEqual(['AllPeriods']);
2002+
expect(pivotGrid.pivotConfiguration.rows.map(x => x.enabled)).toEqual([true]);
2003+
// all other are disabled column dimensions.
2004+
expect(pivotGrid.pivotConfiguration.columns.map(x => x.memberName)).toEqual(['ProductCategory', 'SellerName', 'Country']);
2005+
expect(pivotGrid.pivotConfiguration.columns.map(x => x.enabled)).toEqual([false, false, false]);
2006+
// values are all enabled.
2007+
expect(pivotGrid.values.length).toEqual(2);
2008+
expect(pivotGrid.values.map(x => x.member)).toEqual(['UnitPrice', 'UnitsSold']);
2009+
expect(pivotGrid.values.map(x => x.enabled)).toEqual([true, true]);
2010+
});
19812011
});
19822012
});
19832013

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
[superCompactMode]="size === 'superCompact'"
6969
[defaultExpandState]="true"
7070
[rowSelection]="'single'"
71-
[pivotConfiguration]="pivotConfigHierarchy"
72-
(dimensionsChange)="dimensionChange()"
71+
[autoGenerateConfig]="true"
7372
>
7473
<ng-template igxPivotValueChip let-value>
7574
{{ value.displayName }}

0 commit comments

Comments
 (0)