4

I have a simple force network graph of four nodes as an example, here:

https://jsfiddle.net/NovasTaylor/o1qesn6k/

I would like to set the initial position of node "A" using its accompanying data so the node is initially fixed at point (265,143). If I were to double-click that node it would return to being d.fixed=FALSE and the usual forces apply.

From my research here on Stackoverflow I know I need to set the inital .x and .y values as per Bostock's suggestion: [Fix Node Position in D3 Force-Directed Layout

But I do not quite grasp where and how to do this. I have tried the following code, which I may not have placed in the right sequence. It appears to be the same as having .x and .y in the source data...which also did not work.

for (var i=0,  tot=dataset.nodes.length; i < tot; i++) {
    if (dataset.nodes[i].fix_x > 0) {
        dataset.nodes[i].x = dataset.nodes[i].fix_x
        dataset.nodes[i].y = dataset.nodes[i].fix_y
    }
}

Your advice would be very much appreciated!

Tim

Community
  • 1
  • 1
Tim
  • 929
  • 12
  • 30
  • 2
    You need to set the `.x` and `.y` values *and* `.fixed`. – Lars Kotthoff Mar 02 '15 at 18:50
  • Thank you Lars. Using your advice (along with Bostock's original) I see I can approach this two ways: 1. Directly in the data: {"id":0,"name":"A","group":"1", "x":265, "y":143, "fixed":"TRUE"} or 2. adding a line in my loop: dataset.nodes[i].fixed = true – Tim Mar 02 '15 at 19:55
  • Yep, that sounds right. Keep in mind that this needs to be in place before you start the force layout. – Lars Kotthoff Mar 02 '15 at 20:14

0 Answers0