What is d3-force?
The d3-force npm package is a module in the D3 (Data-Driven Documents) suite that provides a force-directed graph layout algorithm. It is used to simulate physical forces on particles and links in network graphs, enabling the creation of dynamic visualizations where nodes and links adjust their positions according to specified forces.
What are d3-force's main functionalities?
Force Simulation
This feature allows the creation of a force simulation for nodes and links. Nodes act like charged particles repelling each other while links act like springs connecting nodes. This code initializes a simulation with forces that affect node positioning and link connections.
const d3 = require('d3-force');
const simulation = d3.forceSimulation(nodes)
.force('link', d3.forceLink(links).id(d => d.id))
.force('charge', d3.forceManyBody())
.force('center', d3.forceCenter(width / 2, height / 2));
Collision Detection
Collision detection prevents nodes from overlapping in the visualization. The 'forceCollide' function is used to keep nodes apart by specifying a radius for each node that determines its collision boundary.
const d3 = require('d3-force');
const simulation = d3.forceSimulation(nodes)
.force('collision', d3.forceCollide().radius(d => d.radius));
Positioning Forces
This feature allows the application of forces that push nodes towards a specific position on the x and y axes. The strength of the force can be adjusted to control how aggressively nodes move towards the target position.
const d3 = require('d3-force');
const simulation = d3.forceSimulation(nodes)
.force('x', d3.forceX(width / 2).strength(0.05))
.force('y', d3.forceY(height / 2).strength(0.05));
Other packages similar to d3-force
cola
Cola.js, also known as WebCoLa, is a JavaScript constraint-based layout library that extends the force-directed graph layout paradigm. It supports additional constraints like alignment and distribution, which are not natively supported by d3-force. Cola.js can be integrated with D3.js but offers more layout customization options.
cytoscape
Cytoscape.js is a graph theory library that enables the analysis and visualization of complex networks. It includes various layout algorithms, including force-directed layouts. Compared to d3-force, Cytoscape.js provides a broader range of features for interaction and analysis, making it suitable for more complex applications.
d3-force
…
Installing
If you use NPM, npm install d3-force
. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0 alpha. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3_force
global is exported:
<script src="https://d3js.org/d3-collection.v0.1.min.js"></script>
<script src="https://d3js.org/d3-dispatch.v0.4.min.js"></script>
<script src="https://d3js.org/d3-quadtree.v0.7.min.js"></script>
<script src="https://d3js.org/d3-timer.v0.4.min.js"></script>
<script src="https://d3js.org/d3-force.v0.0.min.js"></script>
<script>
var simulation = d3_force.forceSimulation(nodes);
</script>
Try d3-force in your browser.
API Reference
…