D3.js forceSimulation Radial Force

Explains radial force in D3.js forceSimulation.

Example program

This is a D3.js forceSimulation forceRadial demo. It can arrange elements in a circle.

View code

Example code

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>D3 v7 force simulation force radial</title>
</head>

<body>
  <svg width="800" height="600"></svg>
  <script src="https://d3js.org/d3.v7.min.js"></script>
  <script>
    // 1. Prepare the data to draw
    var width = document.querySelector("svg").clientWidth;
    var height = document.querySelector("svg").clientHeight;
    var nodesData = [];
    for (var i = 0; i < 50; i++) {
      nodesData.push({
        "x": width * Math.random(),
        "y": height * Math.random(),
        "r": parseInt(20 * Math.random() + 2)
      });
    }

    // 2. Add SVG elements
    var node = d3.select("svg")
      .selectAll("circle")
      .data(nodesData)
      .enter()
      .append("circle")
      .attr("r", function (d) { return d.r })
      .attr("fill", "Gold")
      .attr("stroke", "black")
      .attr("opacity", 0.7)
      .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

    // 3. Configure forceSimulation
    var simulation = d3.forceSimulation()
      .force("collide",
        d3.forceCollide()
          .radius(function (d) { return d.r + 1 })
          .iterations(16))
      .force("charge", d3.forceManyBody().strength(-30))
      .force("r",
        d3.forceRadial()
          .radius(height * 0.35)
          .x(width / 2)
          .y(height / 2)
          .strength(0.5)
      );

    simulation.velocityDecay(0.2);

    simulation
      .nodes(nodesData)
      .on("tick", ticked);

    // 4. forceSimulation drawing update function
    function ticked() {
      node
        .attr("cx", function (d) { return d.x; })
        .attr("cy", function (d) { return d.y; });
    }

    // 5. Drag event functions
    function dragstarted(event, d) {
      if (!event.active) simulation.alphaTarget(0.3).restart();
      d.fx = d.x;
      d.fy = d.y;
    }

    function dragged(event, d) {
      d.fx = event.x;
      d.fy = event.y;
    }

    function dragended(event, d) {
      if (!event.active) simulation.alphaTarget(0);
      d.fx = null;
      d.fy = null;
    }
  </script>
</body>

</html>

Explanation

The basics are the same as the node interaction sample. This page only explains “3. Configure forceSimulation”.

3. Configure forceSimulation

// 3. Configure forceSimulation
var simulation = d3.forceSimulation()
  .force("collide",
    d3.forceCollide()
    .radius(function(d) { return d.r + 1 })
    .iterations(16))
  .force("charge", d3.forceManyBody().strength(-30))
  // Explanation starts here ---------------
  .force("r",
    d3.forceRadial()
    .radius(height / 2 * 0.7)
    .x(width / 2)
    .y(height / 2)
    .strength(0.5)
  );
  // Explanation ends here -----------------

simulation.velocityDecay(0.2);

simulation
  .nodes(nodesData)
  .on("tick", ticked);

The parameters that can be set with forceSimulation.force() are summarized below.

“r”: radial force

Function Description
radius Circle radius.
If no value is specified, it returns the current setting.
x Center x coordinate of the circle.
The default is 0.
y Center y coordinate of the circle.
The default is 0.
strength Speed at which nodes move toward the circle position.
In one step, the position changes by distance * strength.
Numbers greater than or equal to 1.0 are no longer used.
Set it as a decimal from 0.0 to 1.0.
The default is 0.1.