198 lines
6.3 KiB
JavaScript
198 lines
6.3 KiB
JavaScript
var Pivot =
|
|
{
|
|
Leaves:{},
|
|
Root:N.Create({Label:"All Pivots"}),
|
|
Schema:N.Create({Label:"Column Details"}),
|
|
Proto:N.Create({Label:"User Form"}),
|
|
Init(inColumnTypes, inColumnNames, inRows)
|
|
{
|
|
for(let i=0; i<inColumnNames.length; i++)
|
|
{
|
|
let columnNode = N.Create({Label:inColumnNames[i], Index:i});
|
|
N.Connect(Pivot.Schema, columnNode, inColumnTypes[i]);
|
|
N.Connect(Pivot.Schema, columnNode, "all");
|
|
}
|
|
|
|
let numeric = (N.Step(Pivot.Schema, "sum")||[]).map(column=>column.Meta.Index);
|
|
console.log(numeric);
|
|
Pivot.Leaves = inRows.map(r =>
|
|
{
|
|
numeric.forEach(index => r[index] = parseFloat(r[index])||0);
|
|
return N.Create({Row:r});
|
|
}
|
|
);
|
|
Pivot.Init = ()=>{};
|
|
},
|
|
Pivot(inRoot, inParent, inPivotIndicies, inSumIndicies, inDepth)
|
|
{
|
|
//arguments:
|
|
// - a Node with leaf Nodes temporarily stored in its Meta.Leaves
|
|
// - where each leaf Node has a row of table data in it's Meta.Row
|
|
// - a list of columns to pivot on
|
|
// - a list of columns to sum
|
|
// - optional traversal depth, defaults to 0
|
|
let depth = inDepth||0;
|
|
let uniques = {};
|
|
let indexPivot = inPivotIndicies[depth];
|
|
inParent.Meta.Leaves.forEach((inLeaf)=>
|
|
{
|
|
let row = inLeaf.Meta.Row; // shorthand for the raw "CSV" row in the leaf Node's Meta
|
|
let value = row[indexPivot]; // get the pivot column
|
|
let match = uniques[value]; // check in the uniques list if this pivot column exists
|
|
if(!match)
|
|
{
|
|
// 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] = {
|
|
Label:value,
|
|
Row:clone,
|
|
IndexPivot:indexPivot,
|
|
IndexSum:inSumIndicies,
|
|
Leaves:[],
|
|
Depth:depth
|
|
};
|
|
// grow a child off of the parent using the meta object
|
|
N.Connect(inParent, N.Create(match), "Hierarchy");
|
|
}
|
|
else
|
|
{
|
|
// if a match does exist, sum the appropriate columns
|
|
inSumIndicies.forEach((inSumIndex) => match.Row[inSumIndex] += row[inSumIndex]);
|
|
}
|
|
// move the leaves into the child
|
|
match.Leaves.push(inLeaf);
|
|
});
|
|
|
|
// get the leaves out of the parent, at this point they have been re-distributed to the children
|
|
delete inParent.Meta.Leaves;
|
|
let iterator = () => {};
|
|
if(depth >= inPivotIndicies.length-1)
|
|
{
|
|
iterator = inLastBranch =>
|
|
{
|
|
inLastBranch.Meta.Leaves.forEach( inLeaf =>
|
|
{
|
|
let modifiers = [];
|
|
let collectModifier = n => modifiers.push(n);
|
|
let connectModifiers = n => modifiers.forEach(inModifier => N.Connect(inModifier, n, "ModifyOut", true));
|
|
|
|
// collect modifiers effecting leaves
|
|
N.Walk(collectModifier, inLeaf, "ModifyAt", false);
|
|
N.Walk(collectModifier, inLeaf, "ModifyDown", false);
|
|
|
|
if(modifiers.length)
|
|
{
|
|
// apply them to the branch
|
|
inLastBranch.ID.Walk = N.ID.Walk;
|
|
connectModifiers(inLastBranch);
|
|
|
|
// also walk them up and connect to parents, but with "check unique" enabled
|
|
console.log("walking modifiers up from", inLastBranch.Meta.Label);
|
|
N.ID.Walk++;
|
|
N.Walk(connectModifiers, inLastBranch, "Hierarchy", false);
|
|
}
|
|
|
|
// lastly connect the leaf to the branch
|
|
N.Connect(inLastBranch, inLeaf, "Leaf");
|
|
|
|
});
|
|
delete inLastBranch.Meta.Leaves;
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
iterator = child => {
|
|
Pivot.Pivot(inRoot, child, inPivotIndicies, inSumIndicies, depth+1);
|
|
return false;
|
|
};
|
|
}
|
|
N.Walk(iterator, inParent, "Hierarchy");
|
|
|
|
return inParent;
|
|
},
|
|
Create(inLabel, inPivotIndicies, inSumIndicies)
|
|
{
|
|
N.ID.Walk++;
|
|
|
|
/*
|
|
let sumColumns = (N.Step(Pivot.Schema, "sum")||[]).map(column=>column.Meta.Index);
|
|
let labelColumns = (N.Step(Pivot.Schema, "label")||[]).map(column=>column.Meta.Index);
|
|
*/
|
|
|
|
let pivotRoot = N.Create({Label:inLabel, Leaves:Pivot.Leaves});
|
|
N.Connect(Pivot.Root, pivotRoot, "Pivot");
|
|
return Pivot.Pivot(pivotRoot, pivotRoot, inPivotIndicies, inSumIndicies);
|
|
},
|
|
Delete(inRoot)
|
|
{
|
|
// disconnect modifiers
|
|
let check = N.Step(inRoot, "ModifyUp", false);
|
|
if(check)
|
|
{
|
|
while(check.length>0)
|
|
{
|
|
Pivot.Unmodify(check[0]);
|
|
}
|
|
}
|
|
|
|
// disconnect leaves from terminal branches
|
|
N.Walk(()=>{}, inRoot, "Hierarchy", true, terminal=>{
|
|
N.Disconnect(terminal, null, "Leaf");
|
|
});
|
|
|
|
// disconnect from app
|
|
N.Disconnect(null, inRoot, "Pivot");
|
|
},
|
|
Modify(inNode)
|
|
{
|
|
let modified = N.Create({Label:"Modifier"});
|
|
|
|
// traverse
|
|
let gatherUp = n => N.Connect(modified, n, "ModifyUp");
|
|
let gatherDown = n => N.Connect(modified, n, "ModifyDown");
|
|
let gatherOut = n => N.Connect(modified, n, "ModifyOut");
|
|
|
|
N.ID.Walk++;
|
|
inNode.ID.Walk = N.ID.Walk;
|
|
|
|
// at
|
|
N.Connect(modified, inNode, "ModifyAt");
|
|
|
|
// up
|
|
N.Walk(gatherUp, inNode, "Hierarchy", false);
|
|
|
|
// down 1
|
|
N.Walk(gatherDown, inNode, "Hierarchy", true, terminal=>
|
|
{
|
|
// down 2
|
|
// for each terminal node, step down into its leaves and gather down
|
|
N.Walk(gatherDown, terminal, "Leaf", true, leaf=>
|
|
{
|
|
// out 1
|
|
// walk back up on the leaf connections on other trees
|
|
N.Walk(gatherOut, leaf, "Leaf", false, terminal=>
|
|
{
|
|
// out 2
|
|
// and continueup the hierarchy
|
|
N.Walk(gatherOut, terminal, "Hierarchy", false);
|
|
});
|
|
});
|
|
});
|
|
|
|
return modified;
|
|
},
|
|
Unmodify(inModifier)
|
|
{
|
|
N.Disconnect(inModifier, null, "ModifyUp");
|
|
N.Disconnect(inModifier, null, "ModifyDown");
|
|
N.Disconnect(inModifier, null, "ModifyOut");
|
|
N.Disconnect(inModifier, null, "ModifyAt");
|
|
N.Disconnect(null, inModifier, "Modifier");
|
|
}
|
|
}; |