@@ -28,47 +28,58 @@ 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- const currRows = cloneArray ( rows , true ) ;
42- PivotUtil . assignLevels ( currRows ) ;
43- for ( const row of currRows ) {
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- prevRowDims . push ( row ) ;
50- prevDim = row ;
51- } else {
52- const newData = [ ...data ] ;
53- for ( let i = 0 ; i < newData . length ; i ++ ) {
54- const currData = newData [ i ] [ prevDim . memberName + '_' + pivotKeys . records ] ;
55- const leafData = PivotUtil . getDirectLeafs ( currData , pivotKeys ) ;
56- const hierarchyFields = PivotUtil
57- . getFieldsHierarchy ( leafData , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
58- const siblingData = PivotUtil
59- . processHierarchy ( hierarchyFields , newData [ i ] ?? [ ] , pivotKeys , 0 ) ;
60- 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 ) ;
6164
6265 PivotUtil . processSubGroups ( row , prevRowDims . slice ( 0 ) , siblingData , pivotKeys ) ;
63- if ( PivotUtil . getDimensionDepth ( prevDim ) > PivotUtil . getDimensionDepth ( row ) && siblingData . length > 1 ) {
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.
6468 newData [ i ] [ row . memberName + '_' + pivotKeys . records ] = siblingData ;
6569 } 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.
6671 newData . splice ( i , 1 , ...siblingData ) ;
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.
6775 i += siblingData . length - 1 ;
6876 }
77+ // Add the current top sibling elements for the dimension
78+ curDimTopRecords . push ( cloneArray ( siblingData , true ) ) ;
6979 }
7080 data = newData ;
7181 prevDim = row ;
82+ prevDimTopRecords = curDimTopRecords ;
7283 prevRowDims . push ( row ) ;
7384 }
7485 }
0 commit comments