Add d3.forceRadial.
This commit is contained in:
parent
c7d165a72e
commit
40bd984ffa
34
README.md
34
README.md
|
|
@ -360,7 +360,7 @@ If *distance* is specified, sets the maximum distance between nodes over which t
|
|||
|
||||
#### Positioning
|
||||
|
||||
The [*x*](#forceX)- and [*y*](#forceY)-positioning forces push nodes towards a desired position along the given dimension 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. While these forces can be used to position individual nodes, they are intended primarily for global forces that apply to all (or most) nodes.
|
||||
The [*x*](#forceX)- and [*y*](#forceY)-positioning forces push nodes towards a desired position along the given dimension with a configurable strength. The [*radial*](#forceRadial) force is similar, except it pushes nodes towards the closest point on a given circle. The strength of the force is proportional to the one-dimensional distance between the node’s position and the target position. While these forces can be used to position individual nodes, they are intended primarily for global forces that apply to all (or most) nodes.
|
||||
|
||||
<a name="forceX" href="#forceX">#</a> d3.<b>forceX</b>([<i>x</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/x.js "Source")
|
||||
|
||||
|
|
@ -421,3 +421,35 @@ function y() {
|
|||
```
|
||||
|
||||
The *y*-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 *y*-coordinate of each node is only recomputed when the force is initialized or when this method is called with a new *y*, and not on every application of the force.
|
||||
|
||||
<a name="forceRadial" href="#forceRadial">#</a> d3.<b>forceRadial</b>(<i>radius</i>[, <i>x</i>][, <i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/radial.js "Source")
|
||||
|
||||
Creates a new positioning force towards a circle of the specified [*radius*](#radial_radius) centered at [[*x*](#radial_x), [*y*](#radial_y)]. If *x* and *y* are not specified, they default to ⟨0,0⟩.
|
||||
|
||||
<a name="radial_strength" href="#radial_strength">#</a> <i>radial</i>.<b>strength</b>([<i>strength</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/radial.js "Source")
|
||||
|
||||
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 much to increment the node’s *x*- and *y*-velocity. For example, a value of 0.1 indicates that the node should move a tenth of the way from its current position to the closest point on the circle 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 with a new *strength*, and not on every application of the force.
|
||||
|
||||
<a name="radial_radius" href="#radial_radius">#</a> <i>radial</i>.<b>radius</b>([<i>radius</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/radial.js "Source")
|
||||
|
||||
If *radius* is specified, sets the circle *radius* to the specified number or function, re-evaluates the *radius* accessor for each node, and returns this force. If *radius* is not specified, returns the current *radius* accessor.
|
||||
|
||||
The *radius* 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 radius of each node is only recomputed when the force is initialized or when this method is called with a new *radius*, and not on every application of the force.
|
||||
|
||||
<a name="radial_x" href="#radial_x">#</a> <i>radial</i>.<b>x</b>([<i>x</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/radial.js "Source")
|
||||
|
||||
If *x* is specified, sets the *x*-coordinate of the circle center to the specified number and returns this force. If *x* is not specified, returns the current *x*-coordinate of the center, which defaults to zero.
|
||||
|
||||
<a name="radial_y" href="#radial_y">#</a> <i>radial</i>.<b>y</b>([<i>y</i>]) [<>](https://github.com/d3/d3-force/blob/master/src/radial.js "Source")
|
||||
|
||||
If *y* is specified, sets the *y*-coordinate of the circle center to the specified number and returns this force. If *y* is not specified, returns the current *y*-coordinate of the center, which defaults to zero.
|
||||
|
|
|
|||
1
index.js
1
index.js
|
|
@ -2,6 +2,7 @@ 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 forceRadial} from "./src/radial";
|
||||
export {default as forceSimulation} from "./src/simulation";
|
||||
export {default as forceX} from "./src/x";
|
||||
export {default as forceY} from "./src/y";
|
||||
|
|
|
|||
57
src/radial.js
Normal file
57
src/radial.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import constant from "./constant";
|
||||
|
||||
export default function(radius, x, y) {
|
||||
var nodes,
|
||||
strength = constant(0.1),
|
||||
strengths,
|
||||
radiuses;
|
||||
|
||||
if (typeof radius !== "function") radius = constant(+radius);
|
||||
if (x == null) x = 0;
|
||||
if (y == null) y = 0;
|
||||
|
||||
function force(alpha) {
|
||||
for (var i = 0, n = nodes.length; i < n; ++i) {
|
||||
var node = nodes[i],
|
||||
dx = node.x - x || 1e-6,
|
||||
dy = node.y - y || 1e-6,
|
||||
r = Math.sqrt(dx * dx + dy * dy),
|
||||
k = (radiuses[i] - r) * strengths[i] * alpha / r;
|
||||
node.vx += dx * k;
|
||||
node.vy += dy * k;
|
||||
}
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
if (!nodes) return;
|
||||
var i, n = nodes.length;
|
||||
strengths = new Array(n);
|
||||
radiuses = new Array(n);
|
||||
for (i = 0; i < n; ++i) {
|
||||
radiuses[i] = +radius(nodes[i], i, nodes);
|
||||
strengths[i] = isNaN(radiuses[i]) ? 0 : +strength(nodes[i], i, nodes);
|
||||
}
|
||||
}
|
||||
|
||||
force.initialize = function(_) {
|
||||
nodes = _, initialize();
|
||||
};
|
||||
|
||||
force.strength = function(_) {
|
||||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
|
||||
};
|
||||
|
||||
force.radius = function(_) {
|
||||
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius;
|
||||
};
|
||||
|
||||
force.x = function(_) {
|
||||
return arguments.length ? (x = +_, force) : x;
|
||||
};
|
||||
|
||||
force.y = function(_) {
|
||||
return arguments.length ? (y = +_, force) : y;
|
||||
};
|
||||
|
||||
return force;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user