Skip to content

Commit 2c03029

Browse files
committed
Compute wrap-around separation.
1 parent 7151746 commit 2c03029

4 files changed

Lines changed: 77 additions & 23 deletions

File tree

d3.layout.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,7 @@ d3.layout.tree = function() {
747747

748748
function tree(d, i) {
749749
var nodes = hierarchy.call(this, d, i),
750-
root = nodes[0],
751-
x0 = 0, // min x
752-
x1 = 0, // max x
753-
y1 = 0; // max y
750+
root = nodes[0];
754751

755752
function firstWalk(node, previousSibling) {
756753
var children = node.children,
@@ -792,11 +789,6 @@ d3.layout.tree = function() {
792789
while (++i < n) {
793790
secondWalk(children[i], x);
794791
}
795-
} else {
796-
// Compute extent of breadth and depth.
797-
if (node.x < x0) x0 = node.x;
798-
if (node.x > x1) x1 = node.x;
799-
if (node.depth > y1) y1 = node.depth;
800792
}
801793
}
802794

@@ -855,7 +847,15 @@ d3.layout.tree = function() {
855847
firstWalk(root);
856848
secondWalk(root, -root._tree.prelim);
857849

858-
// Clear temporary layout variables; transform depth and breadth.
850+
// Compute the left-most, right-most, and depth-most nodes for extents.
851+
var left = d3_tree_layoutSearch(root, d3_tree_layoutLeftmost),
852+
right = d3_tree_layoutSearch(root, d3_tree_layoutRightmost),
853+
deep = d3_tree_layoutSearch(root, d3_tree_layoutDeepest),
854+
x0 = left.x - separation(left, right) / 2,
855+
x1 = right.x + separation(right, left) / 2,
856+
y1 = deep.depth;
857+
858+
// Clear temporary layout variables; transform x and y.
859859
d3_layout_treeVisitAfter(root, function(node) {
860860
node.x = (node.x - x0) / (x1 - x0) * size[0];
861861
node.y = node.depth / y1 * size[1];
@@ -899,6 +899,33 @@ function d3_tree_layoutRight(node) {
899899
return node.children ? node.children[node.children.length - 1] : node._tree.thread;
900900
}
901901

902+
function d3_tree_layoutSearch(node, compare) {
903+
var children = node.children;
904+
if (children) {
905+
var child,
906+
n = children.length,
907+
i = -1;
908+
while (++i < n) {
909+
if (compare(child = d3_tree_layoutSearch(children[i], compare), node) > 0) {
910+
node = child;
911+
}
912+
}
913+
}
914+
return node;
915+
}
916+
917+
function d3_tree_layoutRightmost(a, b) {
918+
return a.x - b.x;
919+
}
920+
921+
function d3_tree_layoutLeftmost(a, b) {
922+
return b.x - a.x;
923+
}
924+
925+
function d3_tree_layoutDeepest(a, b) {
926+
return a.depth - b.depth;
927+
}
928+
902929
function d3_layout_treeVisitAfter(node, callback) {
903930
function visit(node, previousSibling) {
904931
var children = node.children;

0 commit comments

Comments
 (0)