Skip to content

Commit a7e09ea

Browse files
committed
chore(pivot): Fix build and move utils to another file
1 parent b648258 commit a7e09ea

File tree

3 files changed

+129
-126
lines changed

3 files changed

+129
-126
lines changed
Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { IPivotKeys } from 'igniteui-angular';
32
import { cloneArray, cloneValue } from '../../core/utils';
43
import { DataUtil } from '../../data-operations/data-util';
54
import { FilteringExpressionsTree, IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree';
65
import { IFilteringStrategy } from '../../data-operations/filtering-strategy';
7-
import { IPivotDimension, IPivotValue } from './pivot-grid.interface';
6+
import { IPivotDimension, IPivotKeys, IPivotValue } from './pivot-grid.interface';
7+
import { PivotUtil } from './pivot-util';
88

99
/**
1010
* @hidden
@@ -131,126 +131,3 @@ export class IgxPivotGridFilterPipe implements PipeTransform {
131131
return result;
132132
}
133133
}
134-
135-
export class PivotUtil {
136-
public static getFieldsHierarchy(data: any[], columns: IPivotDimension[], pivotKeys: IPivotKeys): Map<string, any> {
137-
const hierarchy = new Map<string, any>();
138-
for (const rec of data) {
139-
const vals = this.extractValuesFromDimension(columns, rec);
140-
for (const val of vals) { // this should go in depth also vals.children
141-
if (hierarchy.get(val.value) != null && val.children) {
142-
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
143-
} else {
144-
hierarchy.set(val.value, cloneValue(val));
145-
hierarchy.get(val.value).children = new Map<string, any>();
146-
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
147-
}
148-
}
149-
}
150-
return hierarchy;
151-
}
152-
153-
public static extractValueFromDimension(dim: IPivotDimension, recData: any) {
154-
return typeof dim.member === 'string' ? recData[dim.member] : dim.member.call(this, recData);
155-
}
156-
157-
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any){
158-
const vals = [];
159-
let i = 0;
160-
for (const col of dims) {
161-
const value = this.extractValueFromDimension(col, recData);
162-
vals.push({ value });
163-
if (col.childLevels != null && col.childLevels.length > 0) {
164-
const childValues = this.extractValuesFromDimension(col.childLevels, recData);
165-
vals[i].children = childValues;
166-
}
167-
i++;
168-
}
169-
return vals;
170-
}
171-
172-
public static applyAggregations(hierarchies, values, pivotKeys) {
173-
hierarchies.forEach((hierarchy) => {
174-
const children = hierarchy[pivotKeys.children];
175-
if (children) {
176-
this.applyAggregations(children, values, pivotKeys);
177-
const childrenAggregations = this.collectAggregations(children, pivotKeys);
178-
hierarchy[pivotKeys.aggregations] = this.aggregate(childrenAggregations, values);
179-
} else if (hierarchy[pivotKeys.records]) {
180-
hierarchy[pivotKeys.aggregations] = this.aggregate(hierarchy[pivotKeys.records], values);
181-
}
182-
});
183-
}
184-
185-
public static aggregate(records, values: IPivotValue[]) {
186-
const result = {};
187-
for (const pivotValue of values) {
188-
result[pivotValue.member] = pivotValue.aggregate(records.map(r => r[pivotValue.member]));
189-
}
190-
191-
return result;
192-
}
193-
194-
public static flattenHierarchy(hierarchies, rec, pivotKeys, level = 0) {
195-
let flatData = [];
196-
const field = this.generateFieldValue(rec);
197-
hierarchies.forEach((h, key) => {
198-
let obj = {};
199-
obj[field] = key;
200-
obj[pivotKeys.records] = h[pivotKeys.records];
201-
obj = {...obj, ...h[pivotKeys.aggregations]};
202-
obj[pivotKeys.level] = level;
203-
flatData.push(obj);
204-
if (h[pivotKeys.children]) {
205-
obj[pivotKeys.records] = this.flattenHierarchy(h[pivotKeys.children], rec, pivotKeys, level + 1);
206-
flatData = [...flatData, ...obj[pivotKeys.records]];
207-
}
208-
});
209-
210-
return flatData;
211-
}
212-
213-
public static flattenColumnHierarchy(hierarchies, values, pivotKeys) {
214-
let flatData = [];
215-
hierarchies.forEach((h, key) => {
216-
const obj = {};
217-
for (const value of values) {
218-
obj[key] = h[pivotKeys.aggregations][value.member];
219-
obj[pivotKeys.records] = h[pivotKeys.records];
220-
flatData.push(obj);
221-
if (h[pivotKeys.children]) {
222-
flatData = [...flatData, ...this.flattenColumnHierarchy(h[pivotKeys.children], values, pivotKeys)];
223-
}
224-
}
225-
});
226-
227-
return flatData;
228-
}
229-
230-
private static generateFieldValue(rec) {
231-
let i = 0;
232-
while (Object.keys(rec).indexOf('field' + ++i) !== -1) {}
233-
return 'field' + i;
234-
}
235-
236-
private static collectAggregations(children, pivotKeys) {
237-
const result = [];
238-
children.forEach(value => result.push(value[pivotKeys.aggregations]));
239-
240-
return result;
241-
}
242-
243-
private static applyHierarchyChildren(hierarchy, val, rec, recordsKey) {
244-
for (const child of val.children) {
245-
if (!hierarchy.get(val.value).children.get(child.value)) {
246-
hierarchy.get(val.value).children.set(child.value, child);
247-
}
248-
249-
if (hierarchy.get(val.value).children.get(child.value)[recordsKey]) {
250-
hierarchy.get(val.value).children.get(child.value)[recordsKey].push(rec);
251-
} else {
252-
hierarchy.get(val.value).children.get(child.value)[recordsKey] = [rec];
253-
}
254-
}
255-
}
256-
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class IgxPivotRowComponent extends IgxRowDirective<IgxPivotGridComponent>
3434

3535
public rowDimension: IgxColumnComponent[] = [];
3636
public level = 0;
37-
protected hasChild = false;
37+
public hasChild = false;
3838

3939
constructor(
4040
public gridAPI: GridBaseAPIService<IgxPivotGridComponent>,
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
import { cloneValue } from '../../core/utils';
3+
import { IPivotDimension, IPivotKeys, IPivotValue } from './pivot-grid.interface';
4+
5+
export class PivotUtil {
6+
public static getFieldsHierarchy(data: any[], columns: IPivotDimension[], pivotKeys: IPivotKeys): Map<string, any> {
7+
const hierarchy = new Map<string, any>();
8+
for (const rec of data) {
9+
const vals = this.extractValuesFromDimension(columns, rec);
10+
for (const val of vals) { // this should go in depth also vals.children
11+
if (hierarchy.get(val.value) != null && val.children) {
12+
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
13+
} else {
14+
hierarchy.set(val.value, cloneValue(val));
15+
hierarchy.get(val.value).children = new Map<string, any>();
16+
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
17+
}
18+
}
19+
}
20+
return hierarchy;
21+
}
22+
23+
public static extractValueFromDimension(dim: IPivotDimension, recData: any) {
24+
return typeof dim.member === 'string' ? recData[dim.member] : dim.member.call(recData);
25+
}
26+
27+
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any){
28+
const vals = [];
29+
let i = 0;
30+
for (const col of dims) {
31+
const value = this.extractValueFromDimension(col, recData);
32+
vals.push({ value });
33+
if (col.childLevels != null && col.childLevels.length > 0) {
34+
const childValues = this.extractValuesFromDimension(col.childLevels, recData);
35+
vals[i].children = childValues;
36+
}
37+
i++;
38+
}
39+
return vals;
40+
}
41+
42+
public static applyAggregations(hierarchies, values, pivotKeys) {
43+
hierarchies.forEach((hierarchy) => {
44+
const children = hierarchy[pivotKeys.children];
45+
if (children) {
46+
this.applyAggregations(children, values, pivotKeys);
47+
const childrenAggregations = this.collectAggregations(children, pivotKeys);
48+
hierarchy[pivotKeys.aggregations] = this.aggregate(childrenAggregations, values);
49+
} else if (hierarchy[pivotKeys.records]) {
50+
hierarchy[pivotKeys.aggregations] = this.aggregate(hierarchy[pivotKeys.records], values);
51+
}
52+
});
53+
}
54+
55+
public static aggregate(records, values: IPivotValue[]) {
56+
const result = {};
57+
for (const pivotValue of values) {
58+
result[pivotValue.member] = pivotValue.aggregate(records.map(r => r[pivotValue.member]));
59+
}
60+
61+
return result;
62+
}
63+
64+
public static flattenHierarchy(hierarchies, rec, pivotKeys, level = 0) {
65+
let flatData = [];
66+
const field = this.generateFieldValue(rec);
67+
hierarchies.forEach((h, key) => {
68+
let obj = {};
69+
obj[field] = key;
70+
obj[pivotKeys.records] = h[pivotKeys.records];
71+
obj = {...obj, ...h[pivotKeys.aggregations]};
72+
obj[pivotKeys.level] = level;
73+
flatData.push(obj);
74+
if (h[pivotKeys.children]) {
75+
obj[pivotKeys.records] = this.flattenHierarchy(h[pivotKeys.children], rec, pivotKeys, level + 1);
76+
flatData = [...flatData, ...obj[pivotKeys.records]];
77+
}
78+
});
79+
80+
return flatData;
81+
}
82+
83+
public static flattenColumnHierarchy(hierarchies, values, pivotKeys) {
84+
let flatData = [];
85+
hierarchies.forEach((h, key) => {
86+
const obj = {};
87+
for (const value of values) {
88+
obj[key] = h[pivotKeys.aggregations][value.member];
89+
obj[pivotKeys.records] = h[pivotKeys.records];
90+
flatData.push(obj);
91+
if (h[pivotKeys.children]) {
92+
flatData = [...flatData, ...this.flattenColumnHierarchy(h[pivotKeys.children], values, pivotKeys)];
93+
}
94+
}
95+
});
96+
97+
return flatData;
98+
}
99+
100+
private static generateFieldValue(rec) {
101+
let i = 0;
102+
while (Object.keys(rec).indexOf('field' + ++i) !== -1) {}
103+
return 'field' + i;
104+
}
105+
106+
private static collectAggregations(children, pivotKeys) {
107+
const result = [];
108+
children.forEach(value => result.push(value[pivotKeys.aggregations]));
109+
110+
return result;
111+
}
112+
113+
private static applyHierarchyChildren(hierarchy, val, rec, recordsKey) {
114+
for (const child of val.children) {
115+
if (!hierarchy.get(val.value).children.get(child.value)) {
116+
hierarchy.get(val.value).children.set(child.value, child);
117+
}
118+
119+
if (hierarchy.get(val.value).children.get(child.value)[recordsKey]) {
120+
hierarchy.get(val.value).children.get(child.value)[recordsKey].push(rec);
121+
} else {
122+
hierarchy.get(val.value).children.get(child.value)[recordsKey] = [rec];
123+
}
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)