9951 explained code solutions for 126 technologies


javascript-d3How do I create a network graph using D3.js?


To create a network graph using D3.js, you will need to use the forceSimulation() method. This method allows you to create a simulation of forces such as gravity, friction, and charge to create a network graph.

Example code

// Create a simulation
var simulation = d3.forceSimulation()
  .nodes(nodes)
  .force("charge", d3.forceManyBody().strength(-50))
  .force("link", d3.forceLink(links).distance(50))
  .force("center", d3.forceCenter(width / 2, height / 2))
  .on("tick", ticked);

// Create a link between nodes
var link = svg.append("g")
  .attr("class", "links")
  .selectAll("line")
  .data(links)
  .enter().append("line")
  .attr("stroke-width", 2);

// Create a node
var node = svg.append("g")
  .attr("class", "nodes")
  .selectAll("circle")
  .data(nodes)
  .enter().append("circle")
  .attr("r", 5)
  .attr("fill", function(d) { return color(d.group); })
  .call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended));

// Start the simulation
function ticked() {
  link
    .attr("x1", function(d) { return d.source.x; })
    .attr("y1", function(d) { return d.source.y; })
    .attr("x2", function(d) { return d.target.x; })
    .attr("y2", function(d) { return d.target.y; });

  node
    .attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; });
}

This example code will create a network graph with nodes and links and start the simulation. The forceSimulation() method creates a simulation of forces, link creates a link between nodes, node creates a node, and ticked starts the simulation.

Helpful links

Edit this code on GitHub