From c46be8de85b5d69cbe0ae426cf36fadeec9e2de7 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Wed, 12 Aug 2020 14:16:18 -0700 Subject: [PATCH] Add center.strength. Fixes #81. --- README.md | 10 +++++++--- src/center.js | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bf24aef..ffcc00a 100644 --- a/README.md +++ b/README.md @@ -179,18 +179,22 @@ Assigns the array of *nodes* to this force. This method is called when a force i The centering force translates nodes uniformly so that the mean position of all nodes (the center of mass if all nodes have equal weight) is at the given position ⟨[*x*](#center_x),[*y*](#center_y)⟩. This force modifies the positions of nodes on each application; it does not modify velocities, as doing so would typically cause the nodes to overshoot and oscillate around the desired center. This force helps keeps nodes in the center of the viewport, and unlike the [positioning force](#positioning), it does not distort their relative positions. -# d3.forceCenter([x, y]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L1 "Source") +# d3.forceCenter([x, y]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js "Source") Creates a new centering force with the specified [*x*-](#center_x) and [*y*-](#center_y) coordinates. If *x* and *y* are not specified, they default to ⟨0,0⟩. -# center.x([x]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L27 "Source") +# center.x([x]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js "Source") If *x* is specified, sets the *x*-coordinate of the centering position to the specified number and returns this force. If *x* is not specified, returns the current *x*-coordinate, which defaults to zero. -# center.y([y]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js#L31 "Source") +# center.y([y]) [<>](https://github.com/d3/d3-force/blob/master/src/center.js "Source") If *y* is specified, sets the *y*-coordinate of the centering position to the specified number and returns this force. If *y* is not specified, returns the current *y*-coordinate, which defaults to zero. +# center.strength([strength]) · [<>](https://github.com/d3/d3-force/blob/master/src/center.js "Source") + +If *strength* is specified, sets the centering force’s strength. A reduced strength of e.g. 0.05 softens the movements on interactive graphs in which new nodes enter or exit the graph. If *strength* is not specified, returns the force’s current strength, which defaults to 1. + #### Collision The collision force treats nodes as circles with a given [radius](#collide_radius), rather than points, and prevents nodes from overlapping. More formally, two nodes *a* and *b* are separated so that the distance between *a* and *b* is at least *radius*(*a*) + *radius*(*b*). To reduce jitter, this is by default a “soft” constraint with a configurable [strength](#collide_strength) and [iteration count](#collide_iterations). diff --git a/src/center.js b/src/center.js index b8ce38e..280d3e4 100644 --- a/src/center.js +++ b/src/center.js @@ -1,5 +1,5 @@ export default function(x, y) { - var nodes; + var nodes, strength = 1; if (x == null) x = 0; if (y == null) y = 0; @@ -15,7 +15,7 @@ export default function(x, y) { node = nodes[i], sx += node.x, sy += node.y; } - for (sx = sx / n - x, sy = sy / n - y, i = 0; i < n; ++i) { + for (sx = (sx / n - x) * strength, sy = (sy / n - y) * strength, i = 0; i < n; ++i) { node = nodes[i], node.x -= sx, node.y -= sy; } } @@ -32,5 +32,9 @@ export default function(x, y) { return arguments.length ? (y = +_, force) : y; }; + force.strength = function(_) { + return arguments.length ? (strength = +_, force) : strength; + }; + return force; }