|
| 1 | + |
| 2 | +import { IPivotDimension, IPivotKeys, IPivotValue, PivotDimensionType } from '../grids/pivot-grid/pivot-grid.interface'; |
| 3 | +import { PivotUtil } from '../grids/pivot-grid/pivot-util'; |
| 4 | + |
| 5 | +export interface IPivotDimensionStrategy { |
| 6 | + process(collection: any, |
| 7 | + dimensions: IPivotDimension[], |
| 8 | + values: IPivotValue[], |
| 9 | + pivotKeys?: IPivotKeys): any[]; |
| 10 | +} |
| 11 | + |
| 12 | +export class NoopPivotDimensionsStrategy implements IPivotDimensionStrategy { |
| 13 | + private static _instance: NoopPivotDimensionsStrategy = null; |
| 14 | + |
| 15 | + public static instance() { |
| 16 | + return this._instance || (this._instance = new NoopPivotDimensionsStrategy()); |
| 17 | + } |
| 18 | + |
| 19 | + public process(collection: any[], _: IPivotDimension[], __: IPivotValue[]): any[] { |
| 20 | + return collection; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +export class PivotRowDimensionsStrategy implements IPivotDimensionStrategy { |
| 26 | + private static _instance: PivotRowDimensionsStrategy = null; |
| 27 | + |
| 28 | + public static instance() { |
| 29 | + return this._instance || (this._instance = new PivotRowDimensionsStrategy()); |
| 30 | + } |
| 31 | + |
| 32 | + public process( |
| 33 | + collection: any, |
| 34 | + rows: IPivotDimension[], |
| 35 | + values?: IPivotValue[], |
| 36 | + pivotKeys: IPivotKeys = |
| 37 | + { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'} |
| 38 | + ): any[] { |
| 39 | + let hierarchies; |
| 40 | + let data; |
| 41 | + const prevRowDims = []; |
| 42 | + let prevDim; |
| 43 | + for (const row of rows) { |
| 44 | + if (!data) { |
| 45 | + // build hierarchies - groups and subgroups |
| 46 | + hierarchies = PivotUtil.getFieldsHierarchy(collection, [row], PivotDimensionType.Row, pivotKeys); |
| 47 | + // generate flat data from the hierarchies |
| 48 | + data = PivotUtil.processHierarchy(hierarchies, collection[0] ?? [], pivotKeys, 0, true); |
| 49 | + row.fieldName = hierarchies.get(hierarchies.keys().next().value).dimension.fieldName; |
| 50 | + prevRowDims.push(row); |
| 51 | + prevDim = row; |
| 52 | + } else { |
| 53 | + const newData = [...data]; |
| 54 | + for (let i = 0; i < newData.length; i++) { |
| 55 | + const currData = newData[i][prevDim.fieldName + '_' + pivotKeys.records]; |
| 56 | + const hierarchyFields = PivotUtil |
| 57 | + .getFieldsHierarchy(currData, [row], PivotDimensionType.Row, pivotKeys); |
| 58 | + const siblingData = PivotUtil |
| 59 | + .processHierarchy(hierarchyFields, newData[i] ?? [], pivotKeys, 0); |
| 60 | + row.fieldName = hierarchyFields.get(hierarchyFields.keys().next().value).dimension.fieldName; |
| 61 | + PivotUtil.processSiblingProperties(newData[i], siblingData, pivotKeys); |
| 62 | + |
| 63 | + PivotUtil.processSubGroups(row, prevRowDims.slice(0), siblingData, pivotKeys); |
| 64 | + if (PivotUtil.getDimensionDepth(prevDim) > PivotUtil.getDimensionDepth(row)) { |
| 65 | + newData[i][row.fieldName + '_' + pivotKeys.records] = siblingData; |
| 66 | + } else { |
| 67 | + newData.splice(i , 1, ...siblingData); |
| 68 | + } |
| 69 | + i += siblingData.length - 1; |
| 70 | + } |
| 71 | + data = newData; |
| 72 | + prevDim = row; |
| 73 | + prevRowDims.push(row); |
| 74 | + } |
| 75 | + } |
| 76 | + return data; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +export class PivotColumnDimensionsStrategy implements IPivotDimensionStrategy { |
| 81 | + private static _instance: PivotRowDimensionsStrategy = null; |
| 82 | + |
| 83 | + public static instance() { |
| 84 | + return this._instance || (this._instance = new PivotColumnDimensionsStrategy()); |
| 85 | + } |
| 86 | + |
| 87 | + public process( |
| 88 | + collection: any[], |
| 89 | + columns: IPivotDimension[], |
| 90 | + values: IPivotValue[], |
| 91 | + pivotKeys: IPivotKeys = {aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'} |
| 92 | + ): any[] { |
| 93 | + const result = []; |
| 94 | + collection.forEach(hierarchy => { |
| 95 | + // apply aggregations based on the created groups and generate column fields based on the hierarchies |
| 96 | + this.groupColumns(hierarchy, columns, values, pivotKeys); |
| 97 | + if (hierarchy[pivotKeys.children]) { |
| 98 | + let flatCols = {}; |
| 99 | + PivotUtil.flattenColumnHierarchy(hierarchy[pivotKeys.children], values, pivotKeys).forEach(o => { |
| 100 | + delete o[pivotKeys.records]; |
| 101 | + flatCols = {...flatCols, ...o}; |
| 102 | + }); |
| 103 | + delete hierarchy[pivotKeys.children]; /* or we can keep it |
| 104 | + and use when creating the columns in pivot grid instead of recreating it */ |
| 105 | + const keys = Object.keys(hierarchy); |
| 106 | + //remove all record keys from final data since we don't need them anymore. |
| 107 | + keys.forEach(k => { |
| 108 | + if (k.indexOf(pivotKeys.records) !== -1 || k === pivotKeys.level) { |
| 109 | + delete hierarchy[k]; |
| 110 | + } |
| 111 | + }); |
| 112 | + if (this.isLeaf(hierarchy, pivotKeys)) { |
| 113 | + delete hierarchy[pivotKeys.records]; /* remove the helper records of the actual records so that |
| 114 | + expand indicators can be rendered properly */ |
| 115 | + } |
| 116 | + for (const property in flatCols) { |
| 117 | + if (flatCols.hasOwnProperty(property)) { |
| 118 | + hierarchy[property] = flatCols[property]; |
| 119 | + } |
| 120 | + } |
| 121 | + result.push(hierarchy); |
| 122 | + } |
| 123 | + }); |
| 124 | + return result; |
| 125 | + } |
| 126 | + |
| 127 | + private groupColumns(hierarchy, columns, values, pivotKeys) { |
| 128 | + const children = hierarchy[pivotKeys.children]; |
| 129 | + if (children) { |
| 130 | + this.groupColumns(children, columns, values, pivotKeys); |
| 131 | + } else if (hierarchy[pivotKeys.records]) { |
| 132 | + const leafRecords = this.getLeafs(hierarchy[pivotKeys.records], pivotKeys); |
| 133 | + hierarchy[pivotKeys.children] = PivotUtil.getFieldsHierarchy(leafRecords, columns, PivotDimensionType.Column, pivotKeys); |
| 134 | + PivotUtil.applyAggregations(hierarchy[pivotKeys.children], values, pivotKeys); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private getLeafs(records, pivotKeys) { |
| 139 | + let leafs = []; |
| 140 | + for (const rec of records) { |
| 141 | + if (rec[pivotKeys.records]) { |
| 142 | + leafs = leafs.concat(this.getLeafs(rec[pivotKeys.records], pivotKeys)); |
| 143 | + } else { |
| 144 | + leafs.push(rec); |
| 145 | + } |
| 146 | + } |
| 147 | + return leafs; |
| 148 | + } |
| 149 | + |
| 150 | + private isLeaf(record, pivotKeys) { |
| 151 | + return !(record[pivotKeys.records] && record[pivotKeys.records].some(r => r[pivotKeys.records])); |
| 152 | + } |
| 153 | +} |
0 commit comments