
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
graphology-layout-force
Advanced tools
JavaScript implementation of a basic force directed layout algorithm for graphology.
In some few cases, for very small graphs, ForceAtlas2 can be "too efficient". This simpler force algorithm cannot spatialize larger networks, but will offer more organic movements which are more suited to some interactions with the graph such as drag and drop etc.
npm install graphology-layout-force
Each node's starting position must be set before running the force layout algorithm. Two attributes called x
and y
must therefore be defined for all the graph nodes.
Note also that the algorithm has an edge-case where the layout cannot be computed if all of your nodes starts with x=0
and y=0
.
0.0005
]: importance of the attraction force, that attracts each pair of connected nodes like elastics.0.1
]: importance of the repulsion force, that attracts each pair of nodes like magnets.0.0001
]: importance of the gravity force, that attracts all nodes to the center.0.6
]: percentage of a node vector displacement that is preserved at each step. 0
means no inertia, 1
means no friction.200
]: Maximum length a node can travel at each step, in pixel.import forceLayout from 'graphology-layout-force';
const positions = forceLayout(graph, {maxIterations: 50});
// With settings:
const positions = forceLayout(graph, {
maxIterations: 50,
settings: {
gravity: 10
}
});
// To directly assign the positions to the nodes:
forceLayout.assign(graph);
Arguments
x
]: name of the x
position attribute for nodes.y
]: name of the y
position attribute for edges.fixed
]: name of the fixed
attribute for nodes or a function getting a node entry (key, attributes) and returning whether the node is fixed.500
]: maximum number of iterations to perform before stopping. Note that the algorithm will also stop as soon as converged.Layout supervisor relying on window.requestAnimationFrame
to run the layout live without hampering the UI thread.
Example
import ForceSupervisor from 'graphology-layout-force/worker';
const layout = new ForceSupervisor(graph, params);
// To start the layout. It will automatically stop when converged
layout.start();
// To stop the layout
layout.stop();
// To kill the layout and release attached memory and listeners
layout.kill();
// Assess whether the layout is currently running
layout.isRunning();
FAQs
Simple force-directed layout algorithm for graphology.
The npm package graphology-layout-force receives a total of 5,097 weekly downloads. As such, graphology-layout-force popularity was classified as popular.
We found that graphology-layout-force demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.