Commit e0ba8027 authored by Mike Bostock's avatar Mike Bostock
Browse files

Allow pack.radius to be specified as constant.

parent 822e0af7
......@@ -6222,10 +6222,12 @@ d3 = function() {
d3.layout.pack = function() {
var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius;
function pack(d, i) {
var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius || Math.sqrt;
var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() {
return radius;
};
root.x = root.y = 0;
d3_layout_treeVisitAfter(root, function(d) {
d.r = r(d.value);
d.r = +r(d.value);
});
d3_layout_treeVisitAfter(root, d3_layout_packSiblings);
if (padding) {
......@@ -6248,7 +6250,7 @@ d3 = function() {
};
pack.radius = function(_) {
if (!arguments.length) return radius;
radius = _;
radius = _ == null || typeof _ === "function" ? _ : +_;
return pack;
};
pack.padding = function(_) {
......
This diff is collapsed.
......@@ -13,11 +13,11 @@ d3.layout.pack = function() {
root = nodes[0],
w = size[0],
h = size[1],
r = radius || Math.sqrt;
r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { return radius; };
// Recursively compute the layout.
root.x = root.y = 0;
d3_layout_treeVisitAfter(root, function(d) { d.r = r(d.value); });
d3_layout_treeVisitAfter(root, function(d) { d.r = +r(d.value); });
d3_layout_treeVisitAfter(root, d3_layout_packSiblings);
// When padding, recompute the layout using scaled padding.
......@@ -42,7 +42,7 @@ d3.layout.pack = function() {
pack.radius = function(_) {
if (!arguments.length) return radius;
radius = _;
radius = _ == null || typeof _ === "function" ? _ : +_;
return pack;
};
......
......@@ -82,11 +82,36 @@ suite.addBatch({
assert.equal(pack().radius(), null);
},
"radius can be specified using a custom function of value": function(pack) {
var p = pack().radius(function(value) { return Math.sqrt(value) * 10; });
var r = function(value) { return Math.sqrt(value) * 10; },
p = pack().radius(r);
assert.strictEqual(p.radius(), r);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 10},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 10}
]);
},
"radius can be specified as a constant": function(pack) {
var p = pack().radius(5);
assert.equal(p.radius(), 5);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
},
"radius constant value is coerced to a number": function(pack) {
var p = pack().radius("5");
assert.equal(p.radius(), 5);
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
},
"radius function value is coerced to a number": function(pack) {
var p = pack().radius(function() { return "5"; });
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
]);
}
}
});
......
Supports Markdown
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