Interface ForceManyBody<NodeDatum>

The many-body (or n-body) force applies mutually amongst all nodes. It can be used to simulate gravity (attraction) if the strength is positive, or electrostatic charge (repulsion) if the strength is negative. This implementation uses quadtrees and the Barnes–Hut approximation to greatly improve performance; the accuracy can be customized using the theta parameter.

Unlike links, which only affect two linked nodes, the charge force is global: every node affects every other node, even if they are on disconnected subgraphs.

The generic refers to the type of data for a node.

interface ForceManyBody {
    distanceMax(): number;
    distanceMax(distance): ForceManyBody<NodeDatum>;
    distanceMin(): number;
    distanceMin(distance): ForceManyBody<NodeDatum>;
    initialize(nodes, random): void;
    strength(): ((d, i, data) => number);
    strength(strength): ForceManyBody<NodeDatum>;
    theta(): number;
    theta(theta): ForceManyBody<NodeDatum>;
    (alpha): void;
}

Type Parameters

Hierarchy

  • Force<NodeDatum, any>
    • ForceManyBody
  • Apply this force, optionally observing the specified alpha. Typically, the force is applied to the array of nodes previously passed to force.initialize, however, some forces may apply to a subset of nodes, or behave differently. For example, d3.forceLink applies to the source and target of each link.

    Parameters

    • alpha: number

    Returns void

Methods

  • Returns the current maximum distance over which this force is considered, which defaults to infinity.

    Returns number

  • Sets the maximum distance between nodes over which this force is considered.

    Specifying a finite maximum distance improves performance and produces a more localized layout.

    The default value is infinity.

    Parameters

    • distance: number

      The maximum distance between nodes over which this force is considered.

    Returns ForceManyBody<NodeDatum>

  • Returns the current minimum distance over which this force is considered, which defaults to 1.

    Returns number

  • Sets the minimum distance between nodes over which this force is considered.

    A minimum distance establishes an upper bound on the strength of the force between two nearby nodes, avoiding instability. In particular, it avoids an infinitely-strong force if two nodes are exactly coincident; in this case, the direction of the force is random.

    The default value is 1.

    Parameters

    • distance: number

      The minimum distance between nodes over which this force is considered.

    Returns ForceManyBody<NodeDatum>

  • Supplies the array of nodes and random source to this force. This method is called when a force is bound to a simulation via simulation.force and when the simulation’s nodes change via simulation.nodes.

    A force may perform necessary work during initialization, such as evaluating per-node parameters, to avoid repeatedly performing work during each application of the force.

    Parameters

    • nodes: NodeDatum[]
    • random: (() => number)
        • (): number
        • Returns number

    Returns void

  • Return the current strength accessor.

    For details regarding the default behavior see: https://github.com/d3/d3-force#manyBody_strength

    Returns ((d, i, data) => number)

  • sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. A positive value causes nodes to attract each other, similar to gravity, while a negative value causes nodes to repel each other, similar to electrostatic charge. The strength accessor is invoked for each node 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.

    Parameters

    • strength: number | ((d, i, data) => number)

    Returns ForceManyBody<NodeDatum>

  • Return the current value of the Barnes–Hut approximation criterion , which defaults to 0.9

    Returns number

  • Set the Barnes–Hut approximation criterion to the specified number and returns this force.

    To accelerate computation, this force implements the Barnes–Hut approximation which takes O(n log n) per application where n is the number of nodes. For each application, a quadtree stores the current node positions; then for each node, the combined force of all other nodes on the given node is computed. For a cluster of nodes that is far away, the charge force can be approximated by treating the cluster as a single, larger node. The theta parameter determines the accuracy of the approximation: if the ratio w / l of the width w of the quadtree cell to the distance l from the node to the cell’s center of mass is less than theta, all nodes in the given cell are treated as a single node rather than individually.

    The default value is 0.9.

    Parameters

    • theta: number

      Value for the theta parameter.

    Returns ForceManyBody<NodeDatum>