Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does d3.js force layout allow dynamic linkDistance?

I'm using the force layout to represent a directed unweighted network. My inspiration comes from the following example: http://bl.ocks.org/mbostock/1153292

enter image description here

My problem is that in my case there much more links between nodes and I have the feeling that giving a fixed linkDistance, which is always the same, is a very big constraint for my layout.

Is it possible to set a dynamic linkDistance, such that a link grows in length if it's useful to reduce link crossing inside the graph?

like image 566
riccardo.tasso Avatar asked May 15 '13 14:05

riccardo.tasso


2 Answers

From the documentation:

force.linkDistance([distance])

If distance is specified, sets the target distance between linked nodes to the specified value. If distance is not specified, returns the layout's current link distance, which defaults to 20. If distance is a constant, then all links are the same distance. Otherwise, if distance is a function, then the function is evaluated for each link (in order), being passed the link and its index, with the this context as the force layout; the function's return value is then used to set each link's distance. The function is evaluated whenever the layout starts. Typically, the distance is specified in pixels; however, the units are arbitrary relative to the layout's size.

Note that the link distance is still adjusted as the layout runs. You may also find setting linkStrength() useful.

like image 170
Lars Kotthoff Avatar answered Jan 06 '23 08:01

Lars Kotthoff


Just two steps:

Step1: Add a parameter in main json as follows:

{source: "Microsoft", target: "Amazon", type: "licensing", value: 60}, {source: "Microsoft", target: "HTC", type: "licensing",value: 60}, {source: "Samsung", target: "Apple", type: "suit",value:60},

Step2: Update linkdistance to accept method.

.linkDistance(function(d) { return  d.value; }) 
like image 29
Gomes Avatar answered Jan 06 '23 08:01

Gomes