Skip to content

Commit 6e90143

Browse files
committed
Move temporary state to node._tree.
1 parent 111e8e5 commit 6e90143

3 files changed

Lines changed: 107 additions & 111 deletions

File tree

d3.layout.js

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -753,16 +753,11 @@ d3.layout.tree = function() {
753753
y1 = 0; // max depth
754754

755755
function firstWalk(node, previousSibling) {
756-
var children = node.children;
757-
if (!children) {
758-
if (previousSibling) {
759-
node.prelim = previousSibling.prelim + separation(node, previousSibling);
760-
}
761-
} else {
756+
var children = node.children,
757+
layout = node._tree;
758+
if (children) {
762759
var n = children.length,
763-
firstChild = children[0],
764-
lastChild = children[n - 1],
765-
ancestor = firstChild,
760+
ancestor = children[0],
766761
previousChild,
767762
child,
768763
i = -1;
@@ -773,31 +768,35 @@ d3.layout.tree = function() {
773768
previousChild = child;
774769
}
775770
d3_layout_treeShift(node);
776-
var midpoint = .5 * (firstChild.prelim + lastChild.prelim);
771+
var midpoint = .5 * (children[0]._tree.prelim + children[n - 1]._tree.prelim);
777772
if (previousSibling) {
778-
node.prelim = previousSibling.prelim + separation(node, previousSibling);
779-
node.mod = node.prelim - midpoint;
773+
layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling);
774+
layout.mod = layout.prelim - midpoint;
780775
} else {
781-
node.prelim = midpoint;
776+
layout.prelim = midpoint;
777+
}
778+
} else {
779+
if (previousSibling) {
780+
layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling);
782781
}
783782
}
784783
}
785784

786785
function secondWalk(node, x) {
787-
node.breadth = node.prelim + x;
786+
node.x = node._tree.prelim + x;
788787
var children = node.children;
789788
if (children) {
790789
var i = -1,
791790
n = children.length;
792-
x += node.mod;
791+
x += node._tree.mod;
793792
while (++i < n) {
794793
secondWalk(children[i], x);
795794
}
796795
}
797796

798797
// Compute extent of breadth and depth.
799-
if (node.breadth < x0) x0 = node.breadth;
800-
if (node.breadth > x1) x1 = node.breadth;
798+
if (node.x < x0) x0 = node.x;
799+
if (node.x > x1) x1 = node.x;
801800
if (node.depth > y1) y1 = node.depth;
802801
}
803802

@@ -807,65 +806,60 @@ d3.layout.tree = function() {
807806
vop = node,
808807
vim = previousSibling,
809808
vom = node.parent.children[0],
810-
sip = vip.mod,
811-
sop = vop.mod,
812-
sim = vim.mod,
813-
som = vom.mod,
809+
sip = vip._tree.mod,
810+
sop = vop._tree.mod,
811+
sim = vim._tree.mod,
812+
som = vom._tree.mod,
814813
shift;
815814
while (vim = d3_tree_layoutRight(vim), vip = d3_tree_layoutLeft(vip), vim && vip) {
816815
vom = d3_tree_layoutLeft(vom);
817816
vop = d3_tree_layoutRight(vop);
818-
vop.ancestor = node;
819-
shift = vim.prelim + sim - vip.prelim - sip + separation(vim, vip);
817+
vop._tree.ancestor = node;
818+
shift = vim._tree.prelim + sim - vip._tree.prelim - sip + separation(vim, vip);
820819
if (shift > 0) {
821820
d3_layout_treeMove(d3_layout_treeAncestor(vim, node, ancestor), node, shift);
822821
sip += shift;
823822
sop += shift;
824823
}
825-
sim += vim.mod;
826-
sip += vip.mod;
827-
som += vom.mod;
828-
sop += vop.mod;
824+
sim += vim._tree.mod;
825+
sip += vip._tree.mod;
826+
som += vom._tree.mod;
827+
sop += vop._tree.mod;
829828
}
830829
if (vim && !d3_tree_layoutRight(vop)) {
831-
vop.thread = vim;
832-
vop.mod += sim - sop;
830+
vop._tree.thread = vim;
831+
vop._tree.mod += sim - sop;
833832
}
834833
if (vip && !d3_tree_layoutLeft(vom)) {
835-
vom.thread = vip;
836-
vom.mod += sip - som;
834+
vom._tree.thread = vip;
835+
vom._tree.mod += sip - som;
837836
ancestor = node;
838837
}
839838
}
840839
return ancestor;
841840
}
842841

843-
// Initialize temporary layout variables. TODO store separately?
842+
// Initialize temporary layout variables.
844843
d3_layout_treeVisitAfter(root, function(node, previousSibling) {
845-
node.ancestor = node;
846-
node.prelim = 0;
847-
node.mod = 0;
848-
node.change = 0;
849-
node.shift = 0;
850-
node.number = previousSibling ? previousSibling.number + 1 : 0;
844+
node._tree = {
845+
ancestor: node,
846+
prelim: 0,
847+
mod: 0,
848+
change: 0,
849+
shift: 0,
850+
number: previousSibling ? previousSibling._tree.number + 1 : 0
851+
};
851852
});
852853

853854
// Compute the layout using Buchheim et al.'s algorithm.
854855
firstWalk(root);
855-
secondWalk(root, -root.prelim);
856+
secondWalk(root, -root._tree.prelim);
856857

857858
// Clear temporary layout variables; transform depth and breadth.
858859
d3_layout_treeVisitAfter(root, function(node) {
859-
node.x = ((node.breadth - x0) / (x1 - x0)) * size[0];
860+
node.x = ((node.x - x0) / (x1 - x0)) * size[0];
860861
node.y = node.depth / y1 * size[1];
861-
delete node.breadth;
862-
delete node.ancestor;
863-
delete node.prelim;
864-
delete node.mod;
865-
delete node.change;
866-
delete node.shift;
867-
delete node.number;
868-
delete node.thread;
862+
delete node._tree;
869863
});
870864

871865
return nodes;
@@ -898,11 +892,11 @@ function d3_layout_treeSeparation(a, b) {
898892
// }
899893

900894
function d3_tree_layoutLeft(node) {
901-
return node.children ? node.children[0] : node.thread;
895+
return node.children ? node.children[0] : node._tree.thread;
902896
}
903897

904898
function d3_tree_layoutRight(node) {
905-
return node.children ? node.children[node.children.length - 1] : node.thread;
899+
return node.children ? node.children[node.children.length - 1] : node._tree.thread;
906900
}
907901

908902
function d3_layout_treeVisitAfter(node, callback) {
@@ -931,24 +925,28 @@ function d3_layout_treeShift(node) {
931925
i = children.length,
932926
child;
933927
while (--i >= 0) {
934-
child = children[i];
928+
child = children[i]._tree;
935929
child.prelim += shift;
936930
child.mod += shift;
937931
shift += child.shift + (change += child.change);
938932
}
939933
}
940934

941935
function d3_layout_treeMove(ancestor, node, shift) {
942-
var subtrees = node.number - ancestor.number;
943-
node.change -= shift / subtrees;
936+
ancestor = ancestor._tree;
937+
node = node._tree;
938+
var change = shift / (node.number - ancestor.number);
939+
ancestor.change += change;
940+
node.change -= change;
944941
node.shift += shift;
945-
ancestor.change += shift / subtrees;
946942
node.prelim += shift;
947943
node.mod += shift;
948944
}
949945

950946
function d3_layout_treeAncestor(vim, node, ancestor) {
951-
return vim.ancestor.parent == node.parent ? vim.ancestor : ancestor;
947+
return vim._tree.ancestor.parent == node.parent
948+
? vim._tree.ancestor
949+
: ancestor;
952950
}
953951
// Squarified Treemaps by Mark Bruls, Kees Huizing, and Jarke J. van Wijk
954952
d3.layout.treemap = function() {

0 commit comments

Comments
 (0)