Gabis
Gabis

Reputation: 173

d3 force directed layout - drawing links without changing the location of the nodes

I am trying to modify the directed graph editor in the following manner:

I tried supplying this function to force's linkDistance:

force.linkDistance(function(link) {
   var deltaX = d.target.x - d.source.x,
    deltaY = d.target.y - d.source.y,
    dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
   return dist;
})

Thinking that this would lead force to assume that there is no need for re-positioning the nodes at each side of the link (following the documentation).

However, this led to a run-time error, which I couldn't resolve.

Any ideas on how this behavior of the graph can be achieved?

Upvotes: 1

Views: 831

Answers (1)

meetamit
meetamit

Reputation: 25157

Sounds like you want to use the fixed options, by setting it as a property of each node node, eg, { id:123, fixed:true }.

Here's a modified version

Fixed nodes don't get moved around by the force layout at all, so unless you explicitly give them an initial position, they just get assigned a random one by the layout. Note too that with all the nodes being fixed, there's no real reason to use a force directed layout.

Upvotes: 2

Related Questions