|
import {Node} from "./hierarchy/index.js"; |
|
|
|
function defaultSeparation(a, b) { |
|
return a.parent === b.parent ? 1 : 2; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function nextLeft(v) { |
|
var children = v.children; |
|
return children ? children[0] : v.t; |
|
} |
|
|
|
|
|
function nextRight(v) { |
|
var children = v.children; |
|
return children ? children[children.length - 1] : v.t; |
|
} |
|
|
|
|
|
|
|
function moveSubtree(wm, wp, shift) { |
|
var change = shift / (wp.i - wm.i); |
|
wp.c -= change; |
|
wp.s += shift; |
|
wm.c += change; |
|
wp.z += shift; |
|
wp.m += shift; |
|
} |
|
|
|
|
|
|
|
|
|
function executeShifts(v) { |
|
var shift = 0, |
|
change = 0, |
|
children = v.children, |
|
i = children.length, |
|
w; |
|
while (--i >= 0) { |
|
w = children[i]; |
|
w.z += shift; |
|
w.m += shift; |
|
shift += w.s + (change += w.c); |
|
} |
|
} |
|
|
|
|
|
|
|
function nextAncestor(vim, v, ancestor) { |
|
return vim.a.parent === v.parent ? vim.a : ancestor; |
|
} |
|
|
|
function TreeNode(node, i) { |
|
this._ = node; |
|
this.parent = null; |
|
this.children = null; |
|
this.A = null; |
|
this.a = this; |
|
this.z = 0; |
|
this.m = 0; |
|
this.c = 0; |
|
this.s = 0; |
|
this.t = null; |
|
this.i = i; |
|
} |
|
|
|
TreeNode.prototype = Object.create(Node.prototype); |
|
|
|
function treeRoot(root) { |
|
var tree = new TreeNode(root, 0), |
|
node, |
|
nodes = [tree], |
|
child, |
|
children, |
|
i, |
|
n; |
|
|
|
while (node = nodes.pop()) { |
|
if (children = node._.children) { |
|
node.children = new Array(n = children.length); |
|
for (i = n - 1; i >= 0; --i) { |
|
nodes.push(child = node.children[i] = new TreeNode(children[i], i)); |
|
child.parent = node; |
|
} |
|
} |
|
} |
|
|
|
(tree.parent = new TreeNode(null, 0)).children = [tree]; |
|
return tree; |
|
} |
|
|
|
|
|
export default function() { |
|
var separation = defaultSeparation, |
|
dx = 1, |
|
dy = 1, |
|
nodeSize = null; |
|
|
|
function tree(root) { |
|
var t = treeRoot(root); |
|
|
|
|
|
t.eachAfter(firstWalk), t.parent.m = -t.z; |
|
t.eachBefore(secondWalk); |
|
|
|
|
|
if (nodeSize) root.eachBefore(sizeNode); |
|
|
|
|
|
|
|
else { |
|
var left = root, |
|
right = root, |
|
bottom = root; |
|
root.eachBefore(function(node) { |
|
if (node.x < left.x) left = node; |
|
if (node.x > right.x) right = node; |
|
if (node.depth > bottom.depth) bottom = node; |
|
}); |
|
var s = left === right ? 1 : separation(left, right) / 2, |
|
tx = s - left.x, |
|
kx = dx / (right.x + s + tx), |
|
ky = dy / (bottom.depth || 1); |
|
root.eachBefore(function(node) { |
|
node.x = (node.x + tx) * kx; |
|
node.y = node.depth * ky; |
|
}); |
|
} |
|
|
|
return root; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function firstWalk(v) { |
|
var children = v.children, |
|
siblings = v.parent.children, |
|
w = v.i ? siblings[v.i - 1] : null; |
|
if (children) { |
|
executeShifts(v); |
|
var midpoint = (children[0].z + children[children.length - 1].z) / 2; |
|
if (w) { |
|
v.z = w.z + separation(v._, w._); |
|
v.m = v.z - midpoint; |
|
} else { |
|
v.z = midpoint; |
|
} |
|
} else if (w) { |
|
v.z = w.z + separation(v._, w._); |
|
} |
|
v.parent.A = apportion(v, w, v.parent.A || siblings[0]); |
|
} |
|
|
|
|
|
function secondWalk(v) { |
|
v._.x = v.z + v.parent.m; |
|
v.m += v.parent.m; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function apportion(v, w, ancestor) { |
|
if (w) { |
|
var vip = v, |
|
vop = v, |
|
vim = w, |
|
vom = vip.parent.children[0], |
|
sip = vip.m, |
|
sop = vop.m, |
|
sim = vim.m, |
|
som = vom.m, |
|
shift; |
|
while (vim = nextRight(vim), vip = nextLeft(vip), vim && vip) { |
|
vom = nextLeft(vom); |
|
vop = nextRight(vop); |
|
vop.a = v; |
|
shift = vim.z + sim - vip.z - sip + separation(vim._, vip._); |
|
if (shift > 0) { |
|
moveSubtree(nextAncestor(vim, v, ancestor), v, shift); |
|
sip += shift; |
|
sop += shift; |
|
} |
|
sim += vim.m; |
|
sip += vip.m; |
|
som += vom.m; |
|
sop += vop.m; |
|
} |
|
if (vim && !nextRight(vop)) { |
|
vop.t = vim; |
|
vop.m += sim - sop; |
|
} |
|
if (vip && !nextLeft(vom)) { |
|
vom.t = vip; |
|
vom.m += sip - som; |
|
ancestor = v; |
|
} |
|
} |
|
return ancestor; |
|
} |
|
|
|
function sizeNode(node) { |
|
node.x *= dx; |
|
node.y = node.depth * dy; |
|
} |
|
|
|
tree.separation = function(x) { |
|
return arguments.length ? (separation = x, tree) : separation; |
|
}; |
|
|
|
tree.size = function(x) { |
|
return arguments.length ? (nodeSize = false, dx = +x[0], dy = +x[1], tree) : (nodeSize ? null : [dx, dy]); |
|
}; |
|
|
|
tree.nodeSize = function(x) { |
|
return arguments.length ? (nodeSize = true, dx = +x[0], dy = +x[1], tree) : (nodeSize ? [dx, dy] : null); |
|
}; |
|
|
|
return tree; |
|
} |
|
|