liveBook · Manning

Centering force

As explained in D3.js in Action, Third Edition

A centering force translates an entire group of nodes toward a specified coordinate while preserving the group's shape.

A centering force moves a whole group of nodes toward a chosen coordinate. Unlike horizontal and vertical positioning forces, it translates the group together rather than pulling each node separately, so the group keeps its shape. section 12.4.1

Center a simulated cluster

Add `forceCenter` to a D3 force simulation: `.force("center", forceCenter().x(centerX).y(centerY))`. Set `centerX` and `centerY` to the desired coordinate. If a collision force has already spread the nodes into a nonoverlapping cluster, centering moves that cluster together instead of flattening it. section 12.4.1

Figure 12.16 The centering force moves an entire system of particles, or nodes, toward the coordinates passed to the x() and y() accessor functions.
figure

Compare the cluster before and after centering: the nodes shift toward the specified coordinate as a group. section 12.4.1

Use centering alongside other forces when arranging a network. Positioning forces pull individual nodes toward coordinates; collision prevents overlap; centering places the resulting group without changing its overall shape. section 12.4.1 section 12.5

How D3.js in Action, Third Edition covers it

How deep it goes
Defines whole-cluster translation, contrasts individual positioning forces, and identifies x() and y() accessors without deriving force calculations.
Examples
Shows a collision-spaced group moving from around (0,0) toward another coordinate without losing its shape.
What it assumes
Assumes nodes in a force simulation and explains centering by comparing it with positioning and collision forces.
What you can do after
Apply it via D3’s forceCenter() and its x() and y() accessors to reposition a simulated cluster.
How it connects
Connects centering to force simulations, positioning forces, and collision forces in network visualization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.