简体   繁体   中英

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:

  • When a link is drawn, it won't re-position the source and target nodes. Instead the link will be drawn between the current location of the nodes.

  • When dragging an existing node around (using ctrl), it won't affect the position of any attached nodes attached to it. Instead all other nodes will remain in their position, and only the links attached to the dragged node will change their length according to the dragging-around.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM