ngraph.forcelayout

This is a force directed graph layouter in 2d. It is using quad tree as an n-body solver. This repository is part of ngraph family, and operates on ngraph.graph
data structure. If you want to go the 3D space, please check out ngraph.forcelayout3d
API
First of all it's worth to mention all force directed algorithms are iterative. We need to
perform multiple iterations of an algorithm, before graph starts looking aesthetically pleasing.
With that in mind, the easiest way to make graph look nice is:
var layout = require('ngraph.forcelayout')(graph);
for (var i = 0; i < ITERATIONS_COUNT; ++i) {
layout.step();
}
graph.forEachNode(function(node) {
console.log(layout.getNodePosition(node.id));
});
graph.forEachLink(function(link) {
console.log(layout.getLinkPosition(link.id));
});
Result of getNodePosition()
/getLinkPosition()
will be always the same for the same node. This is true:
layout.getNodePosition(1) === layout.getNodePosition(1);
Reason for this is performance. If you are interested in storing positions somewhere else, you can do it and they still will be updated after each force directed layout iteration.
"Pin" node and initial position
Sometimes it's desirable to tell layout algorithm not to move certain nodes. This can be done with pinNode()
method:
var nodeToPin = graph.getNode(nodeId);
layout.pinNode(nodeToPin, true);
If you want to check whether node is pinned or not you can use isNodePinned()
method. Here is an example how to toggle node pinning, without knowing it's original state:
var node = graph.getNode(nodeId);
layout.pinNode(node, !layout.isNodePinned(node));
What if you still want to move your node according to some external factor (e.g. you have initial positions, or user drags pinned node)? To do this, call setNodePosition()
method:
layout.setNodePosition(nodeId, x, y);
Monitoring changes
Like many other algorithms in ngraph
family, force layout monitors graph changes via graph events. It keeps layout up to date whenever graph changes:
var graph = require('ngraph.graph')();
var layout = require('ngraph.layout')(graph);
graph.addLink(1, 2);
layout.getNodePosition(1);
If you want to stop monitoring graph events, call dispose()
method:
layout.dispose();
Configuring physics
Since this is force directed layout, sometimes it's desirable to adjust physics simulator. Please refer to ngraph.physics.simulator to see source code and simulator parameters. Once you have instance of physics simulator you can pass it as a second argument to layout constructor:
var physics = require('ngraph.physics.simulator')({
springLength: 30,
springCoeff: 0.0008,
gravity: -1.2,
theta: 0.8,
dragCoeff: 0.02,
timeStep : 20
});
var layout = require('ngraph.forcelayout')(graph, physics);
You can always get current physics simulator from layout by checking layout.simulator
property. This is read only property.
Space occupied by graph
Finally, it's often desirable to know how much space does our graph occupy. To quickly get bounding box use getGraphRect()
method:
var rect = layout.getGraphRect();
install
With npm do:
npm install ngraph.layout
license
MIT
Feedback?
I'd totally love it! Please email me, open issue here, or tweet to me