Skip to content

Commit

Permalink
Separate d3.forcePosition into d3.force{X,Y}.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 29, 2016
1 parent cf287b8 commit a1ee534
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,15 @@ If *distance* is specified, sets the maximum distance between nodes over which t

#### Positioning

The positioning force pushes nodes towards a desired position ⟨[*x*](#position_x),[*y*](#position_y) with a configurable [strength](#position_strength). The strength of the force is proportional to the distance between the node’s position and the target position, similar to a spring force.
The positioning forces pushes nodes towards a desired [*x*](#forceX)- or [*y*](#forceY)-position with a configurable strength. The strength of the force is proportional to the one-dimensional distance between the node’s position and the target position.

<a name="forcePosition" href="#forcePosition">#</a> d3.<b>forcePosition</b>([<i>x</i>, <i>y</i>])
<a name="forceX" href="#forceX">#</a> d3.<b>forceX</b>([<i>x</i>])

Creates a new positioning force towards the given position [*x*](#position_x),[*y*](#position_y). If *x* and *y* are not specified, the position defaults to ⟨0,0⟩.
Creates a new positioning force along the *x*-axis towards the given position [*x*](#x_x). If *x* is not specified, it defaults to 0.

<a name="position_strength" href="#position_strength">#</a> <i>position</i>.<b>strength</b>([<i>strength</i>])
<a name="x_strength" href="#x_strength">#</a> <i>x</i>.<b>strength</b>([<i>strength</i>])

If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The *strength* corresponds to the proportion of the vector from the node’s position to the force’s target position that should be added to the node’s velocity: *velocity* += (*target* - *position*) × *strength*. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current position to the target position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.
If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The *strength* determines how to modify the node’s *x*-velocity according to the following formula: *node*.vx += ([*x*](#x_x) - *node*.x) × *strength*. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current *x*-position to the target *x*-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.

If *strength* is not specified, returns the current strength accessor, which defaults to:

Expand All @@ -351,7 +351,7 @@ function strength() {

The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force.

<a name="position_x" href="#position_x">#</a> <i>position</i>.<b>x</b>([<i>x</i>])
<a name="x_x" href="#x_x">#</a> <i>x</i>.<b>x</b>([<i>x</i>])

If *x* is specified, sets the *x*-coordinate accessor to the specified number or function, re-evaluates the *x*-accessor for each node, and returns this force. If *x* is not specified, returns the current *x*-accessor, which defaults to:

Expand All @@ -363,7 +363,25 @@ function x() {

The *x*-accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the target *x*-coordinate of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force.

<a name="position_y" href="#position_y">#</a> <i>position</i>.<b>y</b>([<i>y</i>])
<a name="forceY" href="#forceY">#</a> d3.<b>forceY</b>([<i>y</i>])

Creates a new positioning force along the *y*-axis towards the given position [*y*](#y_y). If *y* is not specified, it defaults to 0.

<a name="y_strength" href="#y_strength">#</a> <i>y</i>.<b>strength</b>([<i>strength</i>])

If *strength* is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. The *strength* determines how to modify the node’s *y*-velocity according to the following formula: *node*.vy += ([*y*](#y_y) - *node*.y) × *strength*. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current *y*-position to the target *y*-position with each application. Higher values moves nodes more quickly to the target position, often at the expense of other forces or constraints. A value outside the range [0,1] is not recommended.

If *strength* is not specified, returns the current strength accessor, which defaults to:

```js
function strength() {
return 0.1;
}
```

The strength accessor is invoked for each [node](#simulation_nodes) in the simulation, being passed the *node* and its zero-based *index*. The resulting number is then stored internally, such that the strength of each node is only recomputed when the force is initialized or when this method is called, and not on every application of the force.

<a name="y_y" href="#y_y">#</a> <i>y</i>.<b>y</b>([<i>y</i>])

If *y* is specified, sets the *y*-coordinate accessor to the specified number or function, re-evaluates the *y*-accessor for each node, and returns this force. If *y* is not specified, returns the current *y*-accessor, which defaults to:

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export {default as forceCenter} from "./src/center";
export {default as forceCollide} from "./src/collide";
export {default as forceLink} from "./src/link";
export {default as forceManyBody} from "./src/manyBody";
export {default as forcePosition} from "./src/position";
export {default as forceSimulation} from "./src/simulation";
export {default as forceX} from "./src/x";
export {default as forceY} from "./src/y";
42 changes: 42 additions & 0 deletions src/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import constant from "./constant";

export default function(x) {
var strength = constant(0.1),
nodes,
strengths,
xz;

if (typeof x !== "function") x = constant(x == null ? 0 : +x);

function force(alpha) {
for (var i = 0, n = nodes.length, node; i < n; ++i) {
node = nodes[i], node.vx += (xz[i] - node.x) * strengths[i] * alpha;
}
}

function initialize() {
if (!nodes) return;
var i, n = nodes.length;
strengths = new Array(n);
xz = new Array(n);
for (i = 0; i < n; ++i) {
strengths[i] = +strength(nodes[i], i, nodes);
xz[i] = +x(nodes[i], i, nodes);
}
}

force.initialize = function(_) {
nodes = _;
initialize();
};

force.strength = function(_) {
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
};

force.x = function(_) {
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x;
};

return force;
}
17 changes: 3 additions & 14 deletions src/position.js → src/y.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import constant from "./constant";

export default function(x, y) {
export default function(y) {
var strength = constant(0.1),
nodes,
strengths,
xz,
yz;

if (typeof x !== "function") x = constant(x == null ? 0 : +x);
if (typeof y !== "function") y = constant(y == null ? 0 : +y);

function force(alpha) {
for (var i = 0, n = nodes.length, node, k; i < n; ++i) {
node = nodes[i];
k = strengths[i] * alpha;
node.vx += (xz[i] - node.x) * k;
node.vy += (yz[i] - node.y) * k;
for (var i = 0, n = nodes.length, node; i < n; ++i) {
node = nodes[i], node.vy += (yz[i] - node.y) * strengths[i] * alpha;
}
}

function initialize() {
if (!nodes) return;
var i, n = nodes.length;
strengths = new Array(n);
xz = new Array(n);
yz = new Array(n);
for (i = 0; i < n; ++i) {
strengths[i] = +strength(nodes[i], i, nodes);
xz[i] = +x(nodes[i], i, nodes);
yz[i] = +y(nodes[i], i, nodes);
}
}
Expand All @@ -41,10 +34,6 @@ export default function(x, y) {
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
};

force.x = function(_) {
return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x;
};

force.y = function(_) {
return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y;
};
Expand Down

0 comments on commit a1ee534

Please sign in to comment.