Commit 1896136d authored by Mike Bostock's avatar Mike Bostock
Browse files

Make scaling non-recursive.

parent ea7b43e8
...@@ -6887,11 +6887,17 @@ ...@@ -6887,11 +6887,17 @@
var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0); var nodes = hierarchy.call(this, d, i), root0 = nodes[0], root1 = wrapTree(root0);
d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z; d3_layout_hierarchyVisitAfter(root1, firstWalk), root1.parent.m = -root1.z;
d3_layout_hierarchyVisitBefore(root1, secondWalk); d3_layout_hierarchyVisitBefore(root1, secondWalk);
if (nodeSize) d3_layout_hierarchyVisitBefore(root1, sizeNode); else { if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else {
var left = d3_layout_treeSearch(root0, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root0, d3_layout_treeRightmost), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1; var left = root0, right = root0, bottom = root0;
d3_layout_hierarchyVisitBefore(root1, function(node) { d3_layout_hierarchyVisitBefore(root0, function(node) {
node._.x = (node._.x - x0) / (x1 - x0) * size[0]; if (node.x < left.x) left = node;
node._.y = node._.depth / y1 * size[1]; if (node.x > right.x) right = node;
if (node.depth > bottom.depth) bottom = node;
});
var tx = separation(left, right) / 2 - left.x, kx = size[0] / (right.x + separation(right, left) / 2 + tx), ky = size[1] / (bottom.depth || 1);
d3_layout_hierarchyVisitBefore(root0, function(node) {
node.x = (node.x + tx) * kx;
node.y = node.depth * ky;
}); });
} }
return nodes; return nodes;
...@@ -6971,8 +6977,8 @@ ...@@ -6971,8 +6977,8 @@
return ancestor; return ancestor;
} }
function sizeNode(node) { function sizeNode(node) {
node._.x *= size[0]; node.x *= size[0];
node._.y = node._.depth * size[1]; node.y = node.depth * size[1];
} }
tree.separation = function(x) { tree.separation = function(x) {
if (!arguments.length) return separation; if (!arguments.length) return separation;
...@@ -6994,27 +7000,6 @@ ...@@ -6994,27 +7000,6 @@
function d3_layout_treeSeparation(a, b) { function d3_layout_treeSeparation(a, b) {
return a.parent == b.parent ? 1 : 2; return a.parent == b.parent ? 1 : 2;
} }
function d3_layout_treeSearch(node, compare) {
var children = node.children;
if (children && (n = children.length)) {
var child, n, i = -1;
while (++i < n) {
if (compare(child = d3_layout_treeSearch(children[i], compare), node) > 0) {
node = child;
}
}
}
return node;
}
function d3_layout_treeRightmost(a, b) {
return a.x - b.x;
}
function d3_layout_treeLeftmost(a, b) {
return b.x - a.x;
}
function d3_layout_treeDeepest(a, b) {
return a.depth - b.depth;
}
function d3_layout_treeLeft(v) { function d3_layout_treeLeft(v) {
var children = v.children; var children = v.children;
return children.length ? children[0] : v.t; return children.length ? children[0] : v.t;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -18,19 +18,25 @@ d3.layout.tree = function() { ...@@ -18,19 +18,25 @@ d3.layout.tree = function() {
d3_layout_hierarchyVisitBefore(root1, secondWalk); d3_layout_hierarchyVisitBefore(root1, secondWalk);
// If a fixed node size is specified, scale x and y. // If a fixed node size is specified, scale x and y.
if (nodeSize) d3_layout_hierarchyVisitBefore(root1, sizeNode); if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode);
// If a fixed tree size is specified, scale x and y based on the extent. // If a fixed tree size is specified, scale x and y based on the extent.
// Compute the left-most, right-most, and depth-most nodes for extents. // Compute the left-most, right-most, and depth-most nodes for extents.
else { else {
var left = d3_layout_treeSearch(root0, d3_layout_treeLeftmost), var left = root0,
right = d3_layout_treeSearch(root0, d3_layout_treeRightmost), right = root0,
x0 = left.x - separation(left, right) / 2, bottom = root0;
x1 = right.x + separation(right, left) / 2, d3_layout_hierarchyVisitBefore(root0, function(node) {
y1 = d3_layout_treeSearch(root0, d3_layout_treeDeepest).depth || 1; if (node.x < left.x) left = node;
d3_layout_hierarchyVisitBefore(root1, function(node) { if (node.x > right.x) right = node;
node._.x = (node._.x - x0) / (x1 - x0) * size[0]; if (node.depth > bottom.depth) bottom = node;
node._.y = node._.depth / y1 * size[1]; });
var tx = separation(left, right) / 2 - left.x,
kx = size[0] / (right.x + separation(right, left) / 2 + tx),
ky = size[1] / (bottom.depth || 1);
d3_layout_hierarchyVisitBefore(root0, function(node) {
node.x = (node.x + tx) * kx;
node.y = node.depth * ky;
}); });
} }
...@@ -146,8 +152,8 @@ d3.layout.tree = function() { ...@@ -146,8 +152,8 @@ d3.layout.tree = function() {
} }
function sizeNode(node) { function sizeNode(node) {
node._.x *= size[0]; node.x *= size[0];
node._.y = node._.depth * size[1]; node.y = node.depth * size[1];
} }
tree.separation = function(x) { tree.separation = function(x) {
...@@ -179,33 +185,6 @@ function d3_layout_treeSeparation(a, b) { ...@@ -179,33 +185,6 @@ function d3_layout_treeSeparation(a, b) {
// return (a.parent == b.parent ? 1 : 2) / a.depth; // return (a.parent == b.parent ? 1 : 2) / a.depth;
// } // }
function d3_layout_treeSearch(node, compare) {
var children = node.children;
if (children && (n = children.length)) {
var child,
n,
i = -1;
while (++i < n) {
if (compare(child = d3_layout_treeSearch(children[i], compare), node) > 0) {
node = child;
}
}
}
return node;
}
function d3_layout_treeRightmost(a, b) {
return a.x - b.x;
}
function d3_layout_treeLeftmost(a, b) {
return b.x - a.x;
}
function d3_layout_treeDeepest(a, b) {
return a.depth - b.depth;
}
// NEXT LEFT // NEXT LEFT
// This function is used to traverse the left contour of a subtree (or // This function is used to traverse the left contour of a subtree (or
// subforest). It returns the successor of v on this contour. This successor is // subforest). It returns the successor of v on this contour. This successor is
......
...@@ -18,9 +18,9 @@ suite.addBatch({ ...@@ -18,9 +18,9 @@ suite.addBatch({
] ]
}).map(layout), [ }).map(layout), [
{name: "1", depth: 0, x: 0.5, y: 0}, {name: "1", depth: 0, x: 0.5, y: 0},
{name: "1-1", depth: 1, x: 1/6, y: 1}, {name: "1-1", depth: 1, x: 0.16666666666666666, y: 1},
{name: "1-2", depth: 1, x: 3/6, y: 1}, {name: "1-2", depth: 1, x: 0.5, y: 1},
{name: "1-3", depth: 1, x: 5/6, y: 1} {name: "1-3", depth: 1, x: 0.8333333333333333, y: 1}
]); ]);
}, },
"can handle an empty children array": function(tree) { "can handle an empty children array": function(tree) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment