|
| 1 | +function defaultSeparation(a, b) { |
| 2 | + return a.parent === b.parent ? 0 : 1; |
| 3 | +} |
| 4 | + |
| 5 | +function defaultStacking(a, b, n) { |
| 6 | + return a.parent === b.parent ? 1 / n : 0; |
| 7 | +} |
| 8 | + |
| 9 | +function meanX(children) { |
| 10 | + return children.reduce(meanXReduce, 0) / children.length; |
| 11 | +} |
| 12 | + |
| 13 | +function meanXReduce(x, c) { |
| 14 | + return x + c.x; |
| 15 | +} |
| 16 | + |
| 17 | +function maxY(children) { |
| 18 | + return children.reduce(maxYReduce, 1); |
| 19 | +} |
| 20 | + |
| 21 | +function maxYReduce(y, c) { |
| 22 | + return Math.max(y, c.y); |
| 23 | +} |
| 24 | + |
| 25 | +function leafLeft(node) { |
| 26 | + var children; |
| 27 | + while (children = node.children) node = children[0]; |
| 28 | + return node; |
| 29 | +} |
| 30 | + |
| 31 | +function leafRight(node) { |
| 32 | + var children; |
| 33 | + while (children = node.children) node = children[children.length - 1]; |
| 34 | + return node; |
| 35 | +} |
| 36 | + |
| 37 | +export default function() { |
| 38 | + var separation = defaultSeparation, |
| 39 | + stacking = defaultStacking, |
| 40 | + ratio = 1, |
| 41 | + dx = 1, |
| 42 | + dy = 1, |
| 43 | + nodeSize = false; |
| 44 | + |
| 45 | + function stackedtree(root) { |
| 46 | + var previousNode, |
| 47 | + stackHeight = 1, |
| 48 | + y = 0, |
| 49 | + x = 0; |
| 50 | + |
| 51 | + // Find longest children array to calculate stacking distance |
| 52 | + root.each(function(node){ |
| 53 | + stackHeight = node.children ? Math.max(node.children.length) : stackHeight; |
| 54 | + }) |
| 55 | + |
| 56 | + // First walk, computing the initial x & y values. |
| 57 | + root.eachAfter(function(node) { |
| 58 | + |
| 59 | + // TODO: Is this flexible enough? |
| 60 | + // Resetting y for new stack |
| 61 | + y = previousNode && previousNode.parent !== node.parent ? 0 : y; |
| 62 | + |
| 63 | + var children = node.children; |
| 64 | + if (children) { |
| 65 | + node.x = meanX(children); |
| 66 | + node.y = ratio + maxY(children); |
| 67 | + } else { |
| 68 | + node.x = previousNode ? x += separation(node, previousNode) : 0; |
| 69 | + node.y = previousNode ? y += stacking(node, previousNode, stackHeight) : 0; |
| 70 | + previousNode = node; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + var left = leafLeft(root), |
| 75 | + right = leafRight(root), |
| 76 | + x0 = left.x - separation(left, right) / 2, |
| 77 | + x1 = right.x + separation(right, left) / 2; |
| 78 | + |
| 79 | + // Second walk, normalizing x & y to the desired size. |
| 80 | + return root.eachAfter(nodeSize ? function(node) { |
| 81 | + node.x = (node.x - root.x) * dx; |
| 82 | + node.y = (root.y - node.y) * dy; |
| 83 | + } : function(node) { |
| 84 | + node.x = (node.x - x0) / (x1 - x0) * dx; |
| 85 | + node.y = (1 - (root.y ? node.y / root.y : 1)) * dy; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + stackedtree.separation = function(x) { |
| 90 | + return arguments.length ? (separation = x, stackedtree) : separation; |
| 91 | + }; |
| 92 | + |
| 93 | + stackedtree.stacking = function(y) { |
| 94 | + return arguments.length ? (stacking = y, stackedtree) : stacking; |
| 95 | + }; |
| 96 | + |
| 97 | + stackedtree.ratio = function(x) { |
| 98 | + // TODO: This a good solution? |
| 99 | + // Tree-to-Stack Ratio from 0 to 1 (default: 1) |
| 100 | + // Lower value means less emphasis on the tree, more on the stacks. |
| 101 | + return arguments.length ? (ratio = x, stackedtree) : ratio; |
| 102 | + }; |
| 103 | + |
| 104 | + stackedtree.size = function(x) { |
| 105 | + return arguments.length ? (nodeSize = false, dx = +x[0], dy = +x[1], stackedtree) : (nodeSize ? null : [dx, dy]); |
| 106 | + }; |
| 107 | + |
| 108 | + stackedtree.nodeSize = function(x) { |
| 109 | + return arguments.length ? (nodeSize = true, dx = +x[0], dy = +x[1], stackedtree) : (nodeSize ? [dx, dy] : null); |
| 110 | + }; |
| 111 | + |
| 112 | + return stackedtree; |
| 113 | +} |
0 commit comments