Skip to content

Commit

Permalink
Add {cluster,tree}.nodeSize. Fixes #317.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 20, 2013
1 parent 9f25c5f commit 47d7cad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
33 changes: 25 additions & 8 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5779,7 +5779,7 @@ d3 = function() {
return [ d3.min(values), d3.max(values) ];
}
d3.layout.tree = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ];
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false;
function tree(d, i) {
var nodes = hierarchy.call(this, d, i), root = nodes[0];
function firstWalk(node, previousSibling) {
Expand Down Expand Up @@ -5860,7 +5860,11 @@ d3 = function() {
firstWalk(root);
secondWalk(root, -root._tree.prelim);
var left = d3_layout_treeSearch(root, d3_layout_treeLeftmost), right = d3_layout_treeSearch(root, d3_layout_treeRightmost), deep = d3_layout_treeSearch(root, d3_layout_treeDeepest), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2, y1 = deep.depth || 1;
d3_layout_treeVisitAfter(root, function(node) {
d3_layout_treeVisitAfter(root, nodeSize ? function(node) {
node.x *= size[0];
node.y = node.depth * size[1];
delete node._tree;
} : function(node) {
node.x = (node.x - x0) / (x1 - x0) * size[0];
node.y = node.depth / y1 * size[1];
delete node._tree;
Expand All @@ -5873,8 +5877,13 @@ d3 = function() {
return tree;
};
tree.size = function(x) {
if (!arguments.length) return size;
size = x;
if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null;
return tree;
};
tree.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null;
return tree;
};
return d3_layout_hierarchyRebind(tree, hierarchy);
Expand Down Expand Up @@ -6099,7 +6108,7 @@ d3 = function() {
}
}
d3.layout.cluster = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ];
var hierarchy = d3.layout.hierarchy().sort(null).value(null), separation = d3_layout_treeSeparation, size = [ 1, 1 ], nodeSize = false;
function cluster(d, i) {
var nodes = hierarchy.call(this, d, i), root = nodes[0], previousNode, x = 0;
d3_layout_treeVisitAfter(root, function(node) {
Expand All @@ -6114,7 +6123,10 @@ d3 = function() {
}
});
var left = d3_layout_clusterLeft(root), right = d3_layout_clusterRight(root), x0 = left.x - separation(left, right) / 2, x1 = right.x + separation(right, left) / 2;
d3_layout_treeVisitAfter(root, function(node) {
d3_layout_treeVisitAfter(root, nodeSize ? function(node) {
node.x = (node.x - root.x) * size[0];
node.y = (root.y - node.y) * size[1];
} : function(node) {
node.x = (node.x - x0) / (x1 - x0) * size[0];
node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1];
});
Expand All @@ -6126,8 +6138,13 @@ d3 = function() {
return cluster;
};
cluster.size = function(x) {
if (!arguments.length) return size;
size = x;
if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null;
return cluster;
};
cluster.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null;
return cluster;
};
return d3_layout_hierarchyRebind(cluster, hierarchy);
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions src/layout/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import "tree";
d3.layout.cluster = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
separation = d3_layout_treeSeparation,
size = [1, 1]; // width, height
size = [1, 1], // width, height
nodeSize = false;

function cluster(d, i) {
var nodes = hierarchy.call(this, d, i),
Expand Down Expand Up @@ -36,7 +37,10 @@ d3.layout.cluster = function() {
x1 = right.x + separation(right, left) / 2;

// Second walk, normalizing x & y to the desired size.
d3_layout_treeVisitAfter(root, function(node) {
d3_layout_treeVisitAfter(root, nodeSize ? function(node) {
node.x = (node.x - root.x) * size[0];
node.y = (root.y - node.y) * size[1];
} : function(node) {
node.x = (node.x - x0) / (x1 - x0) * size[0];
node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1];
});
Expand All @@ -51,8 +55,14 @@ d3.layout.cluster = function() {
};

cluster.size = function(x) {
if (!arguments.length) return size;
size = x;
if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null;
return cluster;
};

cluster.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null;
return cluster;
};

Expand Down
19 changes: 15 additions & 4 deletions src/layout/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import "hierarchy";
d3.layout.tree = function() {
var hierarchy = d3.layout.hierarchy().sort(null).value(null),
separation = d3_layout_treeSeparation,
size = [1, 1]; // width, height
size = [1, 1], // width, height
nodeSize = false;

function tree(d, i) {
var nodes = hierarchy.call(this, d, i),
Expand Down Expand Up @@ -119,7 +120,11 @@ d3.layout.tree = function() {
y1 = deep.depth || 1;

// Clear temporary layout variables; transform x and y.
d3_layout_treeVisitAfter(root, function(node) {
d3_layout_treeVisitAfter(root, nodeSize ? function(node) {
node.x *= size[0];
node.y = node.depth * size[1];
delete node._tree;
} : function(node) {
node.x = (node.x - x0) / (x1 - x0) * size[0];
node.y = node.depth / y1 * size[1];
delete node._tree;
Expand All @@ -135,8 +140,14 @@ d3.layout.tree = function() {
};

tree.size = function(x) {
if (!arguments.length) return size;
size = x;
if (!arguments.length) return nodeSize ? null : size;
nodeSize = (size = x) == null;
return tree;
};

tree.nodeSize = function(x) {
if (!arguments.length) return nodeSize ? size : null;
nodeSize = (size = x) != null;
return tree;
};

Expand Down

0 comments on commit 47d7cad

Please sign in to comment.