Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  96] [ 5]  / answers: 1 / hits: 61656  / 12 Years ago, tue, may 1, 2012, 12:00:00

I want some of the nodes in my force-directed layout to ignore all forces and stay in fixed positions based on an attribute of the node, while still being able to be dragged and exert repulsion on other nodes and maintain their link lines.



I thought it would be as simple as this:



force.on(tick, function() {
vis.selectAll(g.node)
.attr(transform, function(d) {
return (d.someAttribute == true) ?
translate( + d.xcoordFromAttribute + , + d.ycoordFromAttribute +) :
translate( + d.x + , + d.y + )
});
});


I have also tried to manually set the node's x and y attributes each tick, but then the links continue to float out to where the node would be if it was affected by the force.



Obviously I have a basic misunderstanding of how this is supposed to work. How can I fix nodes in a position, while keeping links and still allowing for them to be draggable?


More From » d3.js

 Answers
35

Set d.fixed on the desired nodes to true, and initialize d.x and d.y to the desired position. These nodes will then still be part of the simulation, and you can use the normal display code (e.g., setting a transform attribute); however, because they are marked as fixed, they can only be moved by dragging and not by the simulation.



See the force layout documentation for more details (v3 docs, current docs), and also see how the root node is positioned in this example.


[#85876] Saturday, April 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ira

Total Points: 298
Total Questions: 112
Total Answers: 103

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;