npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngraph.forcelayout3d

v1.2.0

Published

Force directed graph layout in 3d

Downloads

2,427

Readme

Force Directed layout in 3D

Build Status

This is a force directed graph layouter in 3D space. It is using oct tree as an n-body solver. This repository is part of ngraph family, and operates on ngraph.graph data structure.

If two dimensions is enough please also checkout ngraph.forcelayout For higher dimensions use ngraph.forcelayout.nd

Gitter

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:

// graph is an instance of `ngraph.graph` object.
var layout = require('ngraph.forcelayout3d')(graph);
for (var i = 0; i < ITERATIONS_COUNT; ++i) {
  layout.step();
}

// now we can ask layout where each node/link is best positioned:
graph.forEachNode(function(node) {
  console.log(layout.getNodePosition(node.id));
  // Node position is pair of x,y,z coordinates:
  // {x: ... , y: ... , z: ... }
});

graph.forEachLink(function(link) {
  console.log(layout.getLinkPosition(link.id));
  // link position is a pair of two positions:
  // {
  //   from: {x: ..., y: ..., z: ...},
  //   to: {x: ..., y: ..., z: ...}
  // }
});

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); // now layout will not move this node

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)); // toggle it

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, z);

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')(); // empty graph
var layout = require('ngraph.forcelayout3d')(graph); // layout of empty graph

graph.addLink(1, 2); // create node 1 and 2, and make link between them
layout.getNodePosition(1); // returns position.

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 settings. Please refer to ngraph.physics.simulator to see source code and simulator parameters. You can pass physics settings as a second argument to layout constructor.

var physicsSettings = {gravity: -1.2}; // construct physics simulator settings

// pass it as second argument to layout:
var layout = require('ngraph.forcelayout3d')(graph, physicsSettings);

You can always get current physics simulator from layout by checking layout.simulator property. This is read only property.

Configuring integrator

If you find standard Euler integration producing too much errors and jitter, consider using verlet integration:

var physicsSettings = {integrator: 'verlet'};

// pass it as second argument to layout:
var layout = require('ngraph.forcelayout3d')(graph, physicsSettings);

Kudos to @annxingyuan for the PR.

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();
  // rect.x1, rect.y1, rect.z1 - left top back coordinates of bounding box
  // rect.x2, rect.y2, rect.z2 - right bottom front coordinates of bounding box

install

With npm do:

npm install ngraph.forcelayout3d

license

MIT

Feedback?

I'd totally love it! Please email me, open issue here, tweet to me, or join discussion on gitter.