Skip to content

Commit 7a6f04e

Browse files
committed
Cluster & tree layouts don't use value.
These hierarchical layouts inadvertantly exposed the `value` attribute and default sort-by-value, even though they don't depend on a numeric node value to compute the layout. Additionally, the hierarchical layout would filter any nodes with nonpositive (or undefined) values, which was overly strict. Fixes d3#121.
1 parent 43e2ccc commit 7a6f04e

5 files changed

Lines changed: 21 additions & 27 deletions

File tree

d3.layout.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,13 @@ d3.layout.hierarchy = function() {
666666
j = depth + 1;
667667
while (++i < n) {
668668
d = recurse(datas[i], j, nodes);
669-
if (d.value > 0) { // ignore NaN, negative, etc.
670-
c.push(d);
671-
v += d.value;
672-
d.parent = node;
673-
}
669+
d.parent = node;
670+
c.push(d);
671+
v += d.value;
674672
}
675673
if (sort) c.sort(sort);
676-
node.value = v;
677-
} else {
674+
if (value) node.value = v;
675+
} else if (value) {
678676
node.value = value.call(hierarchy, data, depth);
679677
}
680678
return node;
@@ -689,10 +687,11 @@ d3.layout.hierarchy = function() {
689687
n = children.length,
690688
j = depth + 1;
691689
while (++i < n) v += revalue(children[i], j);
692-
} else {
690+
} else if (value) {
693691
v = value.call(hierarchy, node.data, depth);
694692
}
695-
return node.value = v;
693+
if (value) node.value = v;
694+
return v;
696695
}
697696

698697
function hierarchy(d) {
@@ -945,7 +944,7 @@ function d3_layout_packPlace(a, b, c) {
945944
}
946945
// Implements a hierarchical layout using the cluster (or dendogram) algorithm.
947946
d3.layout.cluster = function() {
948-
var hierarchy = d3.layout.hierarchy(),
947+
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
949948
separation = d3_layout_treeSeparation,
950949
size = [1, 1]; // width, height
951950

@@ -986,7 +985,6 @@ d3.layout.cluster = function() {
986985

987986
cluster.sort = d3.rebind(cluster, hierarchy.sort);
988987
cluster.children = d3.rebind(cluster, hierarchy.children);
989-
cluster.value = d3.rebind(cluster, hierarchy.value);
990988
cluster.links = d3_layout_treeLinks;
991989

992990
cluster.separation = function(x) {
@@ -1027,7 +1025,7 @@ function d3_layout_clusterRight(node) {
10271025
}
10281026
// Node-link tree diagram using the Reingold-Tilford "tidy" algorithm
10291027
d3.layout.tree = function() {
1030-
var hierarchy = d3.layout.hierarchy(),
1028+
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
10311029
separation = d3_layout_treeSeparation,
10321030
size = [1, 1]; // width, height
10331031

@@ -1154,7 +1152,6 @@ d3.layout.tree = function() {
11541152

11551153
tree.sort = d3.rebind(tree, hierarchy.sort);
11561154
tree.children = d3.rebind(tree, hierarchy.children);
1157-
tree.value = d3.rebind(tree, hierarchy.value);
11581155
tree.links = d3_layout_treeLinks;
11591156

11601157
tree.separation = function(x) {

d3.layout.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layout/cluster.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Implements a hierarchical layout using the cluster (or dendogram) algorithm.
22
d3.layout.cluster = function() {
3-
var hierarchy = d3.layout.hierarchy(),
3+
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
44
separation = d3_layout_treeSeparation,
55
size = [1, 1]; // width, height
66

@@ -41,7 +41,6 @@ d3.layout.cluster = function() {
4141

4242
cluster.sort = d3.rebind(cluster, hierarchy.sort);
4343
cluster.children = d3.rebind(cluster, hierarchy.children);
44-
cluster.value = d3.rebind(cluster, hierarchy.value);
4544
cluster.links = d3_layout_treeLinks;
4645

4746
cluster.separation = function(x) {

src/layout/hierarchy.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ d3.layout.hierarchy = function() {
1717
j = depth + 1;
1818
while (++i < n) {
1919
d = recurse(datas[i], j, nodes);
20-
if (d.value > 0) { // ignore NaN, negative, etc.
21-
c.push(d);
22-
v += d.value;
23-
d.parent = node;
24-
}
20+
d.parent = node;
21+
c.push(d);
22+
v += d.value;
2523
}
2624
if (sort) c.sort(sort);
27-
node.value = v;
28-
} else {
25+
if (value) node.value = v;
26+
} else if (value) {
2927
node.value = value.call(hierarchy, data, depth);
3028
}
3129
return node;
@@ -40,10 +38,11 @@ d3.layout.hierarchy = function() {
4038
n = children.length,
4139
j = depth + 1;
4240
while (++i < n) v += revalue(children[i], j);
43-
} else {
41+
} else if (value) {
4442
v = value.call(hierarchy, node.data, depth);
4543
}
46-
return node.value = v;
44+
if (value) node.value = v;
45+
return v;
4746
}
4847

4948
function hierarchy(d) {

src/layout/tree.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Node-link tree diagram using the Reingold-Tilford "tidy" algorithm
22
d3.layout.tree = function() {
3-
var hierarchy = d3.layout.hierarchy(),
3+
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
44
separation = d3_layout_treeSeparation,
55
size = [1, 1]; // width, height
66

@@ -127,7 +127,6 @@ d3.layout.tree = function() {
127127

128128
tree.sort = d3.rebind(tree, hierarchy.sort);
129129
tree.children = d3.rebind(tree, hierarchy.children);
130-
tree.value = d3.rebind(tree, hierarchy.value);
131130
tree.links = d3_layout_treeLinks;
132131

133132
tree.separation = function(x) {

0 commit comments

Comments
 (0)