d3.forceLink configuration
Force link configuration methods: link.links (set array of links), link.id (link nodes by numeric index or string identifier), link.distance (set link distance), link.strength (set link strength), link.iterations (set number of iterations).
forceLink creates a new link force
forceLink(*links*) creates a new link force with the specified links and default parameters. If links is not specified, it defaults to the empty array. The function is impure and may mutate the passed-in links.
Link force mechanism
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.
link.links sets or gets the array of links
*link*.links(*links*) sets the array of links associated with this force and recomputes the distance and strength parameters for each link. If links is not specified, returns the current array of links. Each link must have source and target properties (the link's source and target nodes) and an index property (the zero-based index into links, assigned by this method). The function is impure and may mutate the passed-in links when the link force is initialized. If the links array is modified externally, this method must be called again to notify the force of the change.
link.id accessor for node identification
*link*.id(*id*) sets the node id accessor to the specified function or returns the current accessor. The default id accessor returns d.index. The id accessor allows each link's source and target to be specified as numeric indices into the nodes array or as string identifiers. The id accessor is invoked for each node whenever the force is initialized, being passed the node and its zero-based index.
link.id default implementation
The default id accessor for link.id is: function id(d) { return d.index; }. This allows each link's source and target to be specified as a zero-based index into the nodes array.
Link identification by numeric index example
With the default id accessor, links can use numeric indices: const nodes = [{"id": "Alice"}, {"id": "Bob"}, {"id": "Carol"}]; const links = [{"source": 0, "target": 1}, {"source": 1, "target": 2}];
Link identification by string id example
With an id accessor that returns d.id, links can use named sources and targets: const nodes = [{"id": "Alice"}, {"id": "Bob"}, {"id": "Carol"}]; const links = [{"source": "Alice", "target": "Bob"}, {"source": "Bob", "target": "Carol"}];
link.distance sets or gets the distance accessor
*link*.distance(*distance*) sets the distance accessor to the specified number or function and returns this force. If distance is not specified, returns the current distance accessor. The distance accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is stored internally and only recomputed when the force is initialized or when this method is called with a new distance.
link.distance default implementation
The default distance accessor for link.distance is: function distance() { return 30; }
link.strength sets or gets the strength accessor
*link*.strength(*strength*) sets the strength accessor to the specified number or function and returns this force. If strength is not specified, returns the current strength accessor. The strength accessor is invoked for each link, being passed the link and its zero-based index. The resulting number is stored internally and only recomputed when the force is initialized or when this method is called with a new strength.
link.strength default implementation
The default strength accessor for link.strength is: function strength(link) { return 1 / Math.min(count(link.source), count(link.target)); } where count(node) returns the number of links with the given node as a source or target. This default reduces the strength of links connected to heavily-connected nodes, improving stability.
link.iterations sets or gets the iteration count
*link*.iterations(*iterations*) sets the number of iterations per application to the specified number and returns this force. If iterations is not specified, returns the current iteration count, which defaults to 1. 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.
forceLink example usage
const link = d3.forceLink(links).id((d) => d.id);