summation columns created in pivoting

This commit is contained in:
unknown 2019-09-10 21:12:52 -04:00
parent d9c05ba321
commit 3dad9c1013
2 changed files with 26 additions and 24 deletions

View File

@ -12,8 +12,6 @@ rows = [
["b","3"], ["b","3"],
]; ];
table = new Table("Root", rows); table = new Table("Root", rows, [1]);
table.PivotTree([0, 1]); table.PivotTree([0, 1]);
table.ItrLeaves( inLeaf => console.log(inLeaf) ); table.ItrLeaves( inLeaf => console.log(inLeaf) );
console.log(table, leaves);

View File

@ -8,9 +8,9 @@ export class Sum
FromChildren: number; FromChildren: number;
FromOutside: number; FromOutside: number;
constructor() constructor(inIndex: number)
{ {
this.ColumnIndex = 0; this.ColumnIndex = inIndex;
} }
} }
@ -18,52 +18,49 @@ export class Table
{ {
Name: string; Name: string;
Rows: Array<Array<string>>; Rows: Array<Array<string>>;
Columns: Array<Sum>;
Parent?: Table; Parent?: Table;
Children: Array<Table>; Children: Array<Table>;
constructor(inName: string, inRows: Array<Array<string>>) constructor(inName: string, inRows: Array<Array<string>>, inColumns: Array<number>)
{ {
this.Name = inName; this.Name = inName;
this.Rows = inRows; this.Rows = inRows;
this.Parent = null; this.Parent = null;
this.Children = []; this.Children = [];
this.Columns = inColumns.map( (inIndex: number) => new Sum(inIndex) );
} }
Pivot(inColumn: number) Pivot(inColumnPivot: number)
{ {
var cell: string; var cell: string;
var child: Table; var child: Table;
var context: Table;
context = this;
this.Rows.forEach((inRow: Array<string>)=> this.Rows.forEach((inRow: Array<string>)=>
{ {
cell = inRow[inColumn]; cell = inRow[inColumnPivot];
child = this.Children.find( (inChild: Table) => inChild.Name == cell ); child = this.Children.find( (inChild: Table) => inChild.Name == cell );
if(!child) if(!child)
{ {
child = new Table(cell, []); child = new Table(cell, [], this.Columns.map( (inColumn: Sum) => inColumn.ColumnIndex ));
child.Parent = context; child.Parent = this;
context.Children.push(child); this.Children.push(child);
} }
child.Rows.push(inRow); child.Rows.push(inRow);
}); });
this.Rows = []; this.Rows = [];
} }
PivotTree(inColumns: Array<number>) PivotTree(inColumnsPivot: Array<number>)
{ {
var columns: Array<number>; var pivotsRemaining: Array<number>;
if(inColumns.length == 0) if(inColumnsPivot.length == 0)
{ {
return; return;
} }
columns = [...inColumns]; pivotsRemaining = [...inColumnsPivot];
this.Pivot(columns.shift()); this.Pivot(pivotsRemaining.shift());
this.Children.forEach( (inChild: Table) => this.Children.forEach( (inChild: Table) => inChild.PivotTree(pivotsRemaining) );
{
inChild.PivotTree(columns);
});
} }
ItrParents(inFunction: Function) ItrParents(inFunction: Function)
@ -93,4 +90,11 @@ export class Table
this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) ); this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) );
} }
} }
} }
export const Iterators = {
SumRows: (inTable: Table) =>
{
}
};