correct summation / rendering

This commit is contained in:
SethTrowbridge 2021-05-12 19:08:52 -04:00
parent 005ff073e4
commit 3fbf0673d2

View File

@ -172,7 +172,7 @@ var Pivot =
Pivot.Leaves = inRows.map(r => N.Create({Row:r})); Pivot.Leaves = inRows.map(r => N.Create({Row:r}));
Pivot.Init = ()=>{}; Pivot.Init = ()=>{};
}, },
Pivot(inParent, inColumnIndicies, inSumIndicies, inDepth) Pivot(inParent, inPivotIndicies, inSumIndicies, inDepth)
{ {
//arguments: //arguments:
// - a Node with leaf Nodes temporarily stored in its Meta.Leaves // - a Node with leaf Nodes temporarily stored in its Meta.Leaves
@ -185,14 +185,21 @@ var Pivot =
inParent.Meta.Leaves.forEach((inLeaf)=> inParent.Meta.Leaves.forEach((inLeaf)=>
{ {
let row = inLeaf.Meta.Row; // shorthand for the raw "CSV" row in the leaf Node's Meta let row = inLeaf.Meta.Row; // shorthand for the raw "CSV" row in the leaf Node's Meta
let value = row[inColumnIndicies[depth]]; // get the pivot column let value = row[inPivotIndicies[depth]]; // get the pivot column
let match = uniques[value]; // check in the uniques list if this pivot column exists let match = uniques[value]; // check in the uniques list if this pivot column exists
if(!match) if(!match)
{ {
// if not, store a value under that key that will be the meta object for a new child // if not, store a value under that key that will be the meta object for a new child
let clone = row.map(r=>null);
inSumIndicies.forEach((inSumIndex, inIndex, inArray)=>
{
clone[inSumIndex] = row[inSumIndex]
});
match = uniques[value] = { match = uniques[value] = {
Label:value, Label:value,
Row:inSumIndicies.map((inColumnIndex, inIndex, inArray) => row[inColumnIndex]), // create a Meta.Row also on the new child nodes to store summed totals Row:clone,
Leaves:[] Leaves:[]
}; };
// grow a child off of the parent using the meta object // grow a child off of the parent using the meta object
@ -201,7 +208,7 @@ var Pivot =
else else
{ {
// if a match does exist, sum the appropriate columns // if a match does exist, sum the appropriate columns
inSumIndicies.forEach((inColumnIndex, inIndex, inArray) => match.Row[inIndex] += row[inColumnIndex]); inSumIndicies.forEach((inSumIndex) => match.Row[inSumIndex] += row[inSumIndex]);
} }
// move the leaves into the child // move the leaves into the child
match.Leaves.push(inLeaf); match.Leaves.push(inLeaf);
@ -209,7 +216,7 @@ var Pivot =
delete inParent.Meta.Leaves; delete inParent.Meta.Leaves;
var iterator; var iterator;
if(depth >= inColumnIndicies.length-1) if(depth >= inPivotIndicies.length-1)
{ {
iterator = inChild => iterator = inChild =>
{ {
@ -219,14 +226,14 @@ var Pivot =
} }
else else
{ {
iterator = child => Pivot.Pivot(child, inColumnIndicies, inSumIndicies, depth+1); iterator = child => Pivot.Pivot(child, inPivotIndicies, inSumIndicies, depth+1);
} }
N.Step(inParent, "Hierarchy").forEach(iterator); N.Step(inParent, "Hierarchy").forEach(iterator);
return inParent; return inParent;
}, },
Create(inColumnIndicies, inSumIndicies) Create(inPivotIndicies, inSumIndicies)
{ {
return Pivot.Pivot(N.Create({Leaves:Pivot.Leaves}), inColumnIndicies, inSumIndicies); return Pivot.Pivot(N.Create({Leaves:Pivot.Leaves}), inPivotIndicies, inSumIndicies);
}, },
Modify(inNode) Modify(inNode)
{ {
@ -320,13 +327,13 @@ let ElNode = ({node, depth}) =>
margin:0; margin:0;
border-top:1px solid lightgrey; border-top:1px solid lightgrey;
.Label & > .Label
{ {
display:inline-block; display:inline-block;
width:200px; width:200px;
background:${color}; background:${color};
} }
.Label::before & > .Label::before
{ {
content:" "; content:" ";
display:inline-block; display:inline-block;
@ -345,8 +352,8 @@ let ElNode = ({node, depth}) =>
} }
`; `;
return h("div", {className:css(nodeBase)}, [ return h("div", {className:"Node"}, [
h("div", {className:"Upper"}, [ h("div", {className:cx(css(nodeBase), "Upper")}, [
h("div", {className:"Label"}, (node.Meta.Label || "a node") ), h("div", {className:"Label"}, (node.Meta.Label || "a node") ),
h("div", {className:"Table"}, (node.Meta.Row || []).map( cell => h("div", {className:"Cell"}, cell)) ) h("div", {className:"Table"}, (node.Meta.Row || []).map( cell => h("div", {className:"Cell"}, cell)) )
]), ]),