liveBook · Manning

D3.forceCenter()

As explained in D3.js in Action, Third Edition

d3.forceCenter() creates a D3 force that translates a node system toward a specified center.

Use d3.forceCenter() to move a whole cluster of nodes toward a chosen position. Unlike d3.forceX() and d3.forceY(), which pull nodes individually and can flatten the cluster, the centering force translates the group while preserving its shape. section 12.4.1

Move a cluster

In a force simulation, set the destination with the force’s x() and y() accessors: forceSimulation(nodes) .force("center", forceCenter().x(200).y(150)); A cluster formed with a collision force can then move toward (200, 150) without being flattened by separate x and y positioning forces. 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

The figure shows the nodes moving together toward the coordinates set by x() and y(). section 12.4.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains whole-cluster translation and the x() and y() accessors, without detailing the force’s calculations.
Examples
Shows a collision-formed cluster moving from around (0,0) toward a chosen coordinate.
What you can do after
Apply it via D3 to reposition a node cluster without flattening its shape.
How it connects
Contrasts with d3.forceX() and d3.forceY() and pairs with d3.forceCollide() in a node simulation.

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.