A link force pulls connected nodes toward one another in a force-directed network diagram. Stronger connections pull their nodes closer. It works alongside other forces, so the final positions reflect more than links alone. section 12.4.1
To use a link force in D3:
- Prepare separate node and edge data. Give each node a unique ID so an edge’s source and target can be matched to nodes. section 12.4.1 section 12.5
- Add d3.forceLink() to a d3.forceSimulation(). Use id() to identify nodes and strength() to make link strength depend on an edge’s weight. section 12.4.1
- Draw an SVG line for each edge and a circle for each node. On every simulation tick, update both the lines and circles to follow the changing positions. section 12.4.1
Shared scenes as links
In a Romeo and Juliet network, each circle is a character and an edge records how many scenes two characters share. That count supplies the edge weight: characters sharing more scenes are pulled closer. Centering keeps the network in view, while many-body repulsion and collision help spread nodes and prevent overlap. section 12.4.1
Listing 12.8 Creating a network diagram (network.js)
import { select, selectAll } from "d3-selection";
import { max } from "d3-array";
import { forceSimulation, forceCollide, forceCenter, forceManyBody,
forceLink } from "d3-force";
import { colorScale, getRadius } from "./scales";
export const drawNetwork = (nodes, edges) => {
const width = 850;
const height = 600;
const svg = select("#network")
.append("svg")
.attr("viewBox", `0 0 ${width} ${height}`)
.append("g")
.attr("transform", `translate(${width/2}, ${height/2})`);
svg #1
.selectAll(".network-link")
.data(edges)
.join("line")
.attr("class", "network-link")
.attr("stroke", "#364652")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", d => d.weight);
const maxLines = max(nodes, d => d.totalLinesNumber)
svg
.selectAll(".network-node")
.data(nodes)
.join("circle")
.attr("class", "network-node")
.attr("r", d => {
d.radius = getRadius(maxLines, d.totalLinesNumber);
return d.radius;
})
.attr("fill", d => colorScale(d.house))
.attr("stroke", "#FAFBFF")
.attr("stroke-width", 1);
const updateNetwork = () => {
selectAll(".network-link") #2
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
selectAll(".network-node")
.attr("cx", d => d.x)
.attr("cy", d => d.y);
};
const simulation = forceSimulation()
.force("charge", forceManyBody()
.strength(-1000)) #3
.force("collide", forceCollide()
.radius(d => d.radius + 2) )
.force("center", forceCenter().x(0).y(0))
.force("link", forceLink()
.id(d => d.id)
.strength(d => d.weight/10))
.nodes(nodes)
.on("tick", updateNetwork);
simulation #4
.force("link")
.links(edges);
};
The network-diagram listing brings together SVG links and nodes with forceSimulation(), forceLink(), and the other forces used to lay them out. section 12.4.1
A link force is one layout choice, not a complete network design. Choose the representation to suit the relationships and audience; force layouts can also be resource-intensive. section 12.5
