@@ -28,52 +28,53 @@ export class PivotRowDimensionsStrategy implements IPivotDimensionStrategy {
2828 }
2929
3030 public process (
31- collection : any ,
32- rows : IPivotDimension [ ] ,
33- values ?: IPivotValue [ ] ,
34- pivotKeys : IPivotKeys =
35- { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
36- ) : any [ ] {
37- let hierarchies ;
38- let data ;
39- const prevRowDims = [ ] ;
40- let prevDim ;
41- let prevDimTopRecords = [ ] ;
42- const currRows = cloneArray ( rows , true ) ;
43- PivotUtil . assignLevels ( currRows ) ;
44- for ( const row of currRows ) {
45- if ( ! data ) {
46- // build hierarchies - groups and subgroups
47- hierarchies = PivotUtil . getFieldsHierarchy ( collection , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
48- // generate flat data from the hierarchies
49- data = PivotUtil . processHierarchy ( hierarchies , collection [ 0 ] ?? [ ] , pivotKeys , 0 , true ) ;
50- prevRowDims . push ( row ) ;
51- prevDim = row ;
52- prevDimTopRecords = data ;
53- } else {
54- const newData = [ ...data ] ;
55- const curDimTopRecords = [ ] ;
56- for ( let i = 0 ; i < newData . length ; i ++ ) {
57- const currData = newData [ i ] [ prevDim . memberName + '_' + pivotKeys . records ] ;
58- const leafData = PivotUtil . getDirectLeafs ( currData , pivotKeys ) ;
59- const hierarchyFields = PivotUtil
60- . getFieldsHierarchy ( leafData , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
61- const siblingData = PivotUtil
62- . processHierarchy ( hierarchyFields , newData [ i ] ?? [ ] , pivotKeys , 0 ) ;
63- PivotUtil . processSiblingProperties ( newData [ i ] , siblingData , pivotKeys ) ;
31+ collection : any ,
32+ rows : IPivotDimension [ ] ,
33+ values ?: IPivotValue [ ] ,
34+ pivotKeys : IPivotKeys =
35+ { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
36+ ) : any [ ] {
37+ let hierarchies ;
38+ let data ;
39+ const prevRowDims = [ ] ;
40+ let prevDim ;
41+ let prevDimTopRecords = [ ] ;
42+ const currRows = cloneArray ( rows , true ) ;
43+ PivotUtil . assignLevels ( currRows ) ;
44+ for ( const row of currRows ) {
45+ if ( ! data ) {
46+ // build hierarchies - groups and subgroups
47+ hierarchies = PivotUtil . getFieldsHierarchy ( collection , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
48+ // generate flat data from the hierarchies
49+ data = PivotUtil . processHierarchy ( hierarchies , collection [ 0 ] ?? [ ] , pivotKeys , 0 , true ) ;
50+ prevRowDims . push ( row ) ;
51+ prevDim = row ;
52+ prevDimTopRecords = data ;
53+ } else {
54+ const newData = [ ...data ] ;
55+ const curDimTopRecords = [ ] ;
56+ for ( let i = 0 ; i < newData . length ; i ++ ) {
57+ const currData = newData [ i ] [ prevDim . memberName + '_' + pivotKeys . records ] ;
58+ const leafData = PivotUtil . getDirectLeafs ( currData , pivotKeys ) ;
59+ const hierarchyFields = PivotUtil
60+ . getFieldsHierarchy ( leafData , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
61+ const siblingData = PivotUtil
62+ . processHierarchy ( hierarchyFields , newData [ i ] ?? [ ] , pivotKeys , 0 ) ;
63+ PivotUtil . processSiblingProperties ( newData [ i ] , siblingData , pivotKeys ) ;
6464
6565 PivotUtil . processSubGroups ( row , prevRowDims . slice ( 0 ) , siblingData , pivotKeys ) ;
66- if ( ( prevDimTopRecords [ i ] . length != undefined && prevDimTopRecords [ i ] . length < siblingData . length ) || prevDimTopRecords . length <= siblingData . length ) {
66+ if ( ( prevDimTopRecords [ i ] . length != undefined && prevDimTopRecords [ i ] . length < siblingData . length ) || prevDimTopRecords . length < siblingData . length ) {
67+ // Add the sibling data as child records because the previous dimension contains more dense version of the previous dimension records.
6768 newData [ i ] [ row . memberName + '_' + pivotKeys . records ] = siblingData ;
6869 } else {
70+ // Replace the current record with the sibling records because the current dimension is a denser version or produces the same amount of records.
6971 newData . splice ( i , 1 , ...siblingData ) ;
70- if ( prevDimTopRecords [ i ] . length == undefined ) {
71- prevDimTopRecords . splice ( siblingData . length , prevDimTopRecords . length - siblingData . length , ...prevDimTopRecords ) ;
72- } else {
73- prevDimTopRecords [ i ] . splice ( siblingData . length , prevDimTopRecords . length - siblingData . length , ...prevDimTopRecords [ i ] ) ;
74- }
72+ // Shift the prevDimTopRecords item to the right because of the previous row transforms the newData and increases the elements in newData
73+ prevDimTopRecords . splice ( siblingData . length , prevDimTopRecords . length - siblingData . length , ...prevDimTopRecords ) ;
74+ // Increase the index the amount of sibling record that replaces the current one. Subtract 1 because there is already i++ in the for cycle.
7575 i += siblingData . length - 1 ;
7676 }
77+ // Add the current top sibling elements for the dimension
7778 curDimTopRecords . push ( cloneArray ( siblingData , true ) ) ;
7879 }
7980 data = newData ;
0 commit comments