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
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?
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.
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; })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With