Coder Social home page Coder Social logo

ngraph.forcelayout3d's Introduction

Force Directed layout in 3D

Build Status

Note Please use ngraph.forcelayout instead. It is able to perform layout in 2D, 3D and higher dimensional spaces.

Old readme

This repository is a force directed graph layouter that works only in 3D space. The repository is part of ngraph family, and operates on ngraph.graph data structure.

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.

ngraph.forcelayout3d's People

Contributors

anvaka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ngraph.forcelayout3d's Issues

Obsolete version of ngraph.forcelayout as dependency

The problem is that according to the package.json -file, the library requires ngraph.forcelayout to be of the version ^1.2.0, so by default it installs aforementioned package of the version 1.2.0. Version 1.2.0 of ngraph.forcelayout though doesnt split functions that calculate the node mass into defaultArrayNodeMass and defaultSetNodeMass. Instead, there is only one function called defaultNodeMass. That function cant calculate the length of the set links properly because it refers to the length-attribute, not to the size. Therefore, e.g. in my case by executing the function createLayout, which is provided by ngraph.offline.layout-library I receive the following error: Error: Node mass should be a number. I assume that this issue can be neutralized by updating the version of ngraph.forcelayout at the package.json to the one, where this function is able to calculate the size of the set properly.

no screenshot

Can you add a screenshot of a layout that was generated by this algorithm? The technique that was used for the visualization is not important, but I would like the result of this algorithm can look like, before I spend a lot of time to feed it with my data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.