anvaka / ngraph.svg

Svg based graph rendering
MIT License
7 stars 1 forks source link

Dynamic spring length #1

Closed joerodrig closed 9 years ago

joerodrig commented 9 years ago

How would you create a dynamic spring length/springCoeff(spring transform) in ngraph.svg? It looks like the only accessor in ngraph to modify spring length is to retrieve the bodies of the connected nodes you'd like to modify the spring of, but I don't see a way to retrieve the bodies of those two nodes.

I'd like to be able to set a custom spring length in ngraph both when a link is initiated, and potentially after creation

My default graph initiator looks like this:

    var graph = require('ngraph.graph')();
    var svg = require('simplesvg');
    var renderer = require('ngraph.svg')(graph, {
        physics: {
            springLength: 400
        }
    });

I've tried adding an implementation of Vivagraph's SpringTransform function to no avail.

anvaka commented 9 years ago

You are right. This is a miss on my end. Let me fix it..

anvaka commented 9 years ago

This should be fixed in v.0.0.9. Now there is plenty of options to configure springs/bodies. Check out this customSpringLength example: source code, online demo

I've also made force simulator compatible with vivagraph's springTransform option:

function springTransform() {
  if (link.fromId === 1) {
    // this will make links from node 1 only 300px in length (keep in mind
    // other physical settings will lead to longer/shorter distances)
    spring.length = 300;
  }
}

var renderer = require('ngraph.svg')(graph, {
    physics: {
      // this will be the default length:
      springLength: 400,
      // this function will be called for each link when it's added to simulator
      springTransform: springTransform
    }
});

If you want to modify spring length dynamically, you can now get springs by calling layout.getSpring(fromNodeId, toNodeId); (example)