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

Make scaling non-recursive.

parent ea7b43e8
......@@ -6887,11 +6887,17 @@
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_hierarchyVisitBefore(root1, secondWalk);
if (nodeSize) d3_layout_hierarchyVisitBefore(root1, 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;
d3_layout_hierarchyVisitBefore(root1, function(node) {
node._.x = (node._.x - x0) / (x1 - x0) * size[0];
node._.y = node._.depth / y1 * size[1];
if (nodeSize) d3_layout_hierarchyVisitBefore(root0, sizeNode); else {
var left = root0, right = root0, bottom = root0;
d3_layout_hierarchyVisitBefore(root0, function(node) {
if (node.x < left.x) left = node;
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;
......@@ -6971,8 +6977,8 @@
return ancestor;
}
function sizeNode(node) {
node._.x *= size[0];
node._.y = node._.depth * size[1];
node.x *= size[0];
node.y = node.depth * size[1];
}
tree.separation = function(x) {
if (!arguments.length) return separation;
......@@ -6994,27 +7000,6 @@
function d3_layout_treeSeparation(a, b) {
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) {
var children = v.children;
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() {
d3_layout_hierarchyVisitBefore(root1, secondWalk);
// 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.
// Compute the left-most, right-most, and depth-most nodes for extents.
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;
d3_layout_hierarchyVisitBefore(root1, function(node) {
node._.x = (node._.x - x0) / (x1 - x0) * size[0];
node._.y = node._.depth / y1 * size[1];
var left = root0,
right = root0,
bottom = root0;
d3_layout_hierarchyVisitBefore(root0, function(node) {
if (node.x < left.x) left = node;
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;
});
}
......@@ -146,8 +152,8 @@ d3.layout.tree = function() {
}
function sizeNode(node) {
node._.x *= size[0];
node._.y = node._.depth * size[1];
node.x *= size[0];
node.y = node.depth * size[1];
}
tree.separation = function(x) {
......@@ -179,33 +185,6 @@ function d3_layout_treeSeparation(a, b) {
// 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
// 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
......
......@@ -18,9 +18,9 @@ suite.addBatch({
]
}).map(layout), [
{name: "1", depth: 0, x: 0.5, y: 0},
{name: "1-1", depth: 1, x: 1/6, y: 1},
{name: "1-2", depth: 1, x: 3/6, y: 1},
{name: "1-3", depth: 1, x: 5/6, y: 1}
{name: "1-1", depth: 1, x: 0.16666666666666666, y: 1},
{name: "1-2", depth: 1, x: 0.5, y: 1},
{name: "1-3", depth: 1, x: 0.8333333333333333, y: 1}
]);
},
"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