|
1 | 1 | import { Pipe, PipeTransform } from '@angular/core'; |
2 | | -import { IPivotKeys } from 'igniteui-angular'; |
3 | 2 | import { cloneArray, cloneValue } from '../../core/utils'; |
4 | 3 | import { DataUtil } from '../../data-operations/data-util'; |
5 | 4 | import { FilteringExpressionsTree, IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree'; |
6 | 5 | 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'; |
8 | 8 |
|
9 | 9 | /** |
10 | 10 | * @hidden |
@@ -131,126 +131,3 @@ export class IgxPivotGridFilterPipe implements PipeTransform { |
131 | 131 | return result; |
132 | 132 | } |
133 | 133 | } |
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 | | -} |
0 commit comments