Skip to content

Commit e0ba802

Browse files
committed
Allow pack.radius to be specified as constant.
1 parent 822e0af commit e0ba802

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

d3.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6222,10 +6222,12 @@ d3 = function() {
62226222
d3.layout.pack = function() {
62236223
var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort), padding = 0, size = [ 1, 1 ], radius;
62246224
function pack(d, i) {
6225-
var nodes = hierarchy.call(this, d, i), root = nodes[0], w = size[0], h = size[1], r = radius || Math.sqrt;
6225+
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() {
6226+
return radius;
6227+
};
62266228
root.x = root.y = 0;
62276229
d3_layout_treeVisitAfter(root, function(d) {
6228-
d.r = r(d.value);
6230+
d.r = +r(d.value);
62296231
});
62306232
d3_layout_treeVisitAfter(root, d3_layout_packSiblings);
62316233
if (padding) {
@@ -6248,7 +6250,7 @@ d3 = function() {
62486250
};
62496251
pack.radius = function(_) {
62506252
if (!arguments.length) return radius;
6251-
radius = _;
6253+
radius = _ == null || typeof _ === "function" ? _ : +_;
62526254
return pack;
62536255
};
62546256
pack.padding = function(_) {

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layout/pack.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ d3.layout.pack = function() {
1313
root = nodes[0],
1414
w = size[0],
1515
h = size[1],
16-
r = radius || Math.sqrt;
16+
r = radius == null ? Math.sqrt : typeof radius === "function" ? radius : function() { return radius; };
1717

1818
// Recursively compute the layout.
1919
root.x = root.y = 0;
20-
d3_layout_treeVisitAfter(root, function(d) { d.r = r(d.value); });
20+
d3_layout_treeVisitAfter(root, function(d) { d.r = +r(d.value); });
2121
d3_layout_treeVisitAfter(root, d3_layout_packSiblings);
2222

2323
// When padding, recompute the layout using scaled padding.
@@ -42,7 +42,7 @@ d3.layout.pack = function() {
4242

4343
pack.radius = function(_) {
4444
if (!arguments.length) return radius;
45-
radius = _;
45+
radius = _ == null || typeof _ === "function" ? _ : +_;
4646
return pack;
4747
};
4848

test/layout/pack-test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,36 @@ suite.addBatch({
8282
assert.equal(pack().radius(), null);
8383
},
8484
"radius can be specified using a custom function of value": function(pack) {
85-
var p = pack().radius(function(value) { return Math.sqrt(value) * 10; });
85+
var r = function(value) { return Math.sqrt(value) * 10; },
86+
p = pack().radius(r);
87+
assert.strictEqual(p.radius(), r);
8688
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
8789
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 10},
8890
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 10}
8991
]);
92+
},
93+
"radius can be specified as a constant": function(pack) {
94+
var p = pack().radius(5);
95+
assert.equal(p.radius(), 5);
96+
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
97+
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
98+
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
99+
]);
100+
},
101+
"radius constant value is coerced to a number": function(pack) {
102+
var p = pack().radius("5");
103+
assert.equal(p.radius(), 5);
104+
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
105+
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
106+
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
107+
]);
108+
},
109+
"radius function value is coerced to a number": function(pack) {
110+
var p = pack().radius(function() { return "5"; });
111+
assert.deepEqual(p.nodes({children: [{value: 1}]}).map(layout), [
112+
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 5},
113+
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 5}
114+
]);
90115
}
91116
}
92117
});

0 commit comments

Comments
 (0)