Skip to content

Commit

Permalink
Don’t expose d3_functor for link properties.
Browse files Browse the repository at this point in the history
Fixes #895.
  • Loading branch information
mbostock committed Feb 6, 2013
1 parent 7b37e7d commit d287b63
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 53 deletions.
36 changes: 12 additions & 24 deletions d3.js
Expand Up @@ -4165,18 +4165,18 @@
};
force.linkDistance = function(x) {
if (!arguments.length) return linkDistance;
linkDistance = d3_functor(x);
linkDistance = typeof x === "function" ? x : +x;
return force;
};
force.distance = force.linkDistance;
force.linkStrength = function(x) {
if (!arguments.length) return linkStrength;
linkStrength = d3_functor(x);
linkStrength = typeof x === "function" ? x : +x;
return force;
};
force.friction = function(x) {
if (!arguments.length) return friction;
friction = x;
friction = +x;
return force;
};
force.charge = function(x) {
Expand All @@ -4186,16 +4186,17 @@
};
force.gravity = function(x) {
if (!arguments.length) return gravity;
gravity = x;
gravity = +x;
return force;
};
force.theta = function(x) {
if (!arguments.length) return theta;
theta = x;
theta = +x;
return force;
};
force.alpha = function(x) {
if (!arguments.length) return alpha;
x = +x;
if (alpha) {
if (x > 0) alpha = x; else alpha = 0;
} else if (x > 0) {
Expand All @@ -4213,14 +4214,10 @@
(o = nodes[i]).index = i;
o.weight = 0;
}
distances = [];
strengths = [];
for (i = 0; i < m; ++i) {
o = links[i];
if (typeof o.source == "number") o.source = nodes[o.source];
if (typeof o.target == "number") o.target = nodes[o.target];
distances[i] = linkDistance.call(this, o, i);
strengths[i] = linkStrength.call(this, o, i);
++o.source.weight;
++o.target.weight;
}
Expand All @@ -4231,16 +4228,12 @@
if (isNaN(o.px)) o.px = o.x;
if (isNaN(o.py)) o.py = o.y;
}
distances = [];
if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i); else for (i = 0; i < m; ++i) distances[i] = linkDistance;
strengths = [];
if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i); else for (i = 0; i < m; ++i) strengths[i] = linkStrength;
charges = [];
if (typeof charge === "function") {
for (i = 0; i < n; ++i) {
charges[i] = +charge.call(this, nodes[i], i);
}
} else {
for (i = 0; i < n; ++i) {
charges[i] = charge;
}
}
if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i); else for (i = 0; i < n; ++i) charges[i] = charge;
function position(dimension, size) {
var neighbors = neighbor(i), j = -1, m = neighbors.length, x;
while (++j < m) if (!isNaN(x = neighbors[j][dimension])) return x;
Expand Down Expand Up @@ -4319,12 +4312,7 @@
quad.cx = cx / quad.charge;
quad.cy = cy / quad.charge;
}
function d3_layout_forceLinkDistance() {
return 20;
}
function d3_layout_forceLinkStrength() {
return 1;
}
var d3_layout_forceLinkDistance = 20, d3_layout_forceLinkStrength = 1;
d3.layout.partition = function() {
var hierarchy = d3.layout.hierarchy(), size = [ 1, 1 ];
function position(node, x, dx, dy) {
Expand Down
8 changes: 4 additions & 4 deletions d3.min.js

Large diffs are not rendered by default.

43 changes: 18 additions & 25 deletions src/layout/force.js
Expand Up @@ -135,7 +135,7 @@ d3.layout.force = function() {

force.linkDistance = function(x) {
if (!arguments.length) return linkDistance;
linkDistance = d3_functor(x);
linkDistance = typeof x === "function" ? x : +x;
return force;
};

Expand All @@ -144,13 +144,13 @@ d3.layout.force = function() {

force.linkStrength = function(x) {
if (!arguments.length) return linkStrength;
linkStrength = d3_functor(x);
linkStrength = typeof x === "function" ? x : +x;
return force;
};

force.friction = function(x) {
if (!arguments.length) return friction;
friction = x;
friction = +x;
return force;
};

Expand All @@ -162,19 +162,20 @@ d3.layout.force = function() {

force.gravity = function(x) {
if (!arguments.length) return gravity;
gravity = x;
gravity = +x;
return force;
};

force.theta = function(x) {
if (!arguments.length) return theta;
theta = x;
theta = +x;
return force;
};

force.alpha = function(x) {
if (!arguments.length) return alpha;

x = +x;
if (alpha) { // if we're already running
if (x > 0) alpha = x; // we might keep it hot
else alpha = 0; // or, next tick will dispatch "end"
Expand All @@ -201,14 +202,10 @@ d3.layout.force = function() {
o.weight = 0;
}

distances = [];
strengths = [];
for (i = 0; i < m; ++i) {
o = links[i];
if (typeof o.source == "number") o.source = nodes[o.source];
if (typeof o.target == "number") o.target = nodes[o.target];
distances[i] = linkDistance.call(this, o, i);
strengths[i] = linkStrength.call(this, o, i);
++o.source.weight;
++o.target.weight;
}
Expand All @@ -221,16 +218,17 @@ d3.layout.force = function() {
if (isNaN(o.py)) o.py = o.y;
}

distances = [];
if (typeof linkDistance === "function") for (i = 0; i < m; ++i) distances[i] = +linkDistance.call(this, links[i], i);
else for (i = 0; i < m; ++i) distances[i] = linkDistance;

strengths = [];
if (typeof linkStrength === "function") for (i = 0; i < m; ++i) strengths[i] = +linkStrength.call(this, links[i], i);
else for (i = 0; i < m; ++i) strengths[i] = linkStrength;

charges = [];
if (typeof charge === "function") {
for (i = 0; i < n; ++i) {
charges[i] = +charge.call(this, nodes[i], i);
}
} else {
for (i = 0; i < n; ++i) {
charges[i] = charge;
}
}
if (typeof charge === "function") for (i = 0; i < n; ++i) charges[i] = +charge.call(this, nodes[i], i);
else for (i = 0; i < n; ++i) charges[i] = charge;

// initialize node position based on first neighbor
function position(dimension, size) {
Expand Down Expand Up @@ -348,10 +346,5 @@ function d3_layout_forceAccumulate(quad, alpha, charges) {
quad.cy = cy / quad.charge;
}

function d3_layout_forceLinkDistance() {
return 20;
}

function d3_layout_forceLinkStrength() {
return 1;
}
var d3_layout_forceLinkDistance = 20,
d3_layout_forceLinkStrength = 1;
114 changes: 114 additions & 0 deletions test/layout/force-test.js
@@ -0,0 +1,114 @@
require("../env");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.layout.force");

suite.addBatch({
"force": {
topic: d3.layout.force,

"friction": {
"defaults to .9": function(force) {
assert.equal(force.friction(), .9);
},
"can be a number": function(force) {
force.friction(.5);
assert.equal(force.friction(), .5);
},
"coerces to a number": function(force) {
force.friction(".5");
assert.strictEqual(force.friction(), .5);
}
},

"gravity": {
"defaults to .1": function(force) {
assert.equal(force.gravity(), .1);
},
"can be a number": function(force) {
force.gravity(.5);
assert.equal(force.gravity(), .5);
},
"coerces to a number": function(force) {
force.gravity(".5");
assert.strictEqual(force.gravity(), .5);
}
},

"theta": {
"defaults to .8": function(force) {
assert.equal(force.theta(), .8);
},
"can be a number": function(force) {
force.theta(.5);
assert.equal(force.theta(), .5);
},
"coerces to a number": function(force) {
force.theta(".5");
assert.strictEqual(force.theta(), .5);
}
},

"charge": {
"defaults to -30": function(force) {
assert.equal(force.charge(), -30);
},
"can be a number": function(force) {
force.charge(-40);
assert.equal(force.charge(), -40);
},
"can be a function": function(force) { // TODO expose the computed value?
force.charge(foo);
assert.equal(force.charge(), foo);
},
"coerces to a number": function(force) {
force.charge("-40");
assert.strictEqual(force.charge(), -40);
}
},

"linkDistance": {
"defaults to 20": function(force) {
assert.equal(force.linkDistance(), 20);
},
"can be a number": function(force) {
force.linkDistance(40);
assert.equal(force.linkDistance(), 40);
},
"can be a function": function(force) { // TODO expose the computed value?
force.linkDistance(foo);
assert.equal(force.linkDistance(), foo);
},
"coerces to a number": function(force) {
force.linkDistance("40");
assert.strictEqual(force.linkDistance(), 40);
}
},

"linkStrength": {
"defaults to 1": function(force) {
assert.equal(force.linkStrength(), 1);
},
"can be a number": function(force) {
force.linkStrength(.5);
assert.equal(force.linkStrength(), .5);
},
"can be a function": function(force) { // TODO expose the computed value?
force.linkStrength(foo);
assert.equal(force.linkStrength(), foo);
},
"coerces to a number": function(force) {
force.linkStrength(".5");
assert.strictEqual(force.linkStrength(), .5);
}
}
}
});

function foo(d) {
return d.foo;
}

suite.export(module);

0 comments on commit d287b63

Please sign in to comment.