Interface ForceLink<NodeDatum, LinkDatum>

The link force pushes linked nodes together or apart according to the desired link distance. The strength of the force is proportional to the difference between the linked nodes’ distance and the target distance, similar to a spring force.

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

interface ForceLink {
    distance(): ((link, i, links) => number);
    distance(distance): ForceLink<NodeDatum, LinkDatum>;
    id(): ((node, i, nodesData) => string | number);
    id(id): ForceLink<NodeDatum, LinkDatum>;
    initialize(nodes, random): void;
    iterations(): number;
    iterations(iterations): ForceLink<NodeDatum, LinkDatum>;
    links(): LinkDatum[];
    links(links): ForceLink<NodeDatum, LinkDatum>;
    strength(): ((link, i, links) => number);
    strength(strength): ForceLink<NodeDatum, LinkDatum>;
    (alpha): void;
}

Type Parameters

Hierarchy

  • Force<NodeDatum, LinkDatum>
    • ForceLink
  • 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

  • Return the current distance accessor, which defaults to implying a default distance of 30.

    Returns ((link, i, links) => number)

      • (link, i, links): number
      • Return the current distance accessor, which defaults to implying a default distance of 30.

        Parameters

        • link: LinkDatum
        • i: number
        • links: LinkDatum[]

        Returns number

  • Sets the distance accessor to the specified number or function, re-evaluates the distance accessor for each link, and returns this force. The distance accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is then stored internally, such that the distance of each link is only recomputed when the force is initialized or when this method is called with a new distance, and not on every application of the force.

    Parameters

    • distance: number | ((link, i, links) => number)

    Returns ForceLink<NodeDatum, LinkDatum>

  • Return the current node id accessor, which defaults to the numeric node.index.

    Returns ((node, i, nodesData) => string | number)

      • (node, i, nodesData): string | number
      • Return the current node id accessor, which defaults to the numeric node.index.

        Parameters

        • node: NodeDatum
        • i: number
        • nodesData: NodeDatum[]

        Returns string | number

  • Set the node id accessor to the specified function and return this force.

    The default id accessor allows each link’s source and target to be specified as a zero-based index into the nodes array.

    The id accessor is invoked for each node whenever the force is initialized, as when the nodes or links change, being passed the node, the zero-based index of the node in the node array, and the node array.

    Parameters

    • id: ((node, i, nodesData) => string | number)

      A node id accessor function which is invoked for each node in the simulation, being passed the node, the zero-based index of the node in the node array, and the node array. It returns a string or number to represent the node id which can be used for matching link source and link target strings during the ForceLink initialization.

        • (node, i, nodesData): string | number
        • Parameters

          • node: NodeDatum
          • i: number
          • nodesData: NodeDatum[]

          Returns string | number

    Returns ForceLink<NodeDatum, LinkDatum>

  • 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 iteration count which defaults to 1.

    Returns number

  • Sets the number of iterations per application to the specified number and return this force.

    Increasing the number of iterations greatly increases the rigidity of the constraint and is useful for complex structures such as lattices, but also increases the runtime cost to evaluate the force.

    Parameters

    • iterations: number

      Number of iterations.

    Returns ForceLink<NodeDatum, LinkDatum>

  • Return the current array of links, which defaults to the empty array.

    Returns LinkDatum[]

  • Set the array of links associated with this force, recompute the distance and strength parameters for each link, and return this force.

    Each link is an object with the following properties:

    • source - the link’s source node; see simulation.nodes
    • target - the link’s target node; see simulation.nodes
    • index - the zero-based index into links, assigned by this method

    For convenience, a link’s source and target properties may be initialized using numeric or string identifiers rather than object references; see link.id. When the link force is initialized (or re-initialized, as when the nodes or links change), any link.source or link.target property which is not an object is replaced by an object reference to the corresponding node with the given identifier. If the specified array of links is modified, such as when links are added to or removed from the simulation, this method must be called again with the new (or changed) array to notify the force of the change; the force does not make a defensive copy of the specified array.

    Parameters

    • links: LinkDatum[]

      An array of link data.

    Returns ForceLink<NodeDatum, LinkDatum>

  • Return the current strength accessor. For details regarding the default behavior see: https://github.com/d3/d3-force#link_strength

    Returns ((link, i, links) => number)

      • (link, i, links): number
      • Return the current strength accessor. For details regarding the default behavior see: https://github.com/d3/d3-force#link_strength

        Parameters

        • link: LinkDatum
        • i: number
        • links: LinkDatum[]

        Returns number

  • Sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each link, and returns this force. The strength accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is then stored internally, such that the strength of each link 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 | ((link, i, links) => number)

    Returns ForceLink<NodeDatum, LinkDatum>