Force Network: How to set initial position for select nodes?

21 views
Skip to first unread message

Tim Williams

unread,
Mar 1, 2015, 1:54:25 PM3/1/15
to d3...@googlegroups.com
Hello everyone,

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



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) when I reload the graph. If I were to double-click that node it would return to being d.fixed=FALSE and the usual forces apply.

My larger goal is to fix key nodes of very large graphs. Other secondary nodes will not have x,y coordinates in their source data. I expect some logic similar to if d.init-x > 0 the fix the point at d.init-x, d.init-y   - but where do I put this code?

I wish to do this for ease of arranging the large graphs for presentation purposes, rather than having to manually drag these key nodes each time I reload the display.

I would greatly appreciate any insights you may have.


Tim
Message has been deleted

Tim Williams

unread,
Mar 2, 2015, 9:56:54 AM3/2/15
to d3...@googlegroups.com
Reading through the documentation online it appears I need to set the .x and .y for those nodes I wish to be static (at least initially).

If my data looks like this:
  var dataset = {
   
"nodes":[
     
{"id":0,"name":"A","group":"1", "fix_x":265, "fix_y":143},
     
{"id":1,"name":"B","group":"2"},
     
{"id":2,"name":"C","group":"3"},
     
{"id":3,"name":"D","group":"4"}
     
],
   
"edges":[
     
{"source":0,"target":1,"value":""},
     
{"source":1,"target":2,"value":""},
     
{"source":2,"target":3,"value":""},
     
{"source":3,"target":0,"value":""}
     
]
   
}


I had hoped I could write something like this, but it does not appear to work. Any suggestions on where I am going wrong?

 for (var i=0,  tot=dataset.nodes.length; i < tot; i++) {
   
// console.log("Node: " + dataset.nodes[i].name + " " + dataset.nodes[i].fix_x)
   
if (dataset.nodes[i].fix_x > 0) {
      console
.log("Found one!")
      dataset
.nodes[i].x = dataset.nodes[i].fix_x
      dataset
.nodes[i].y = dataset.nodes[i].fix_y
   
}
 
}




Tim Williams

unread,
Mar 2, 2015, 1:36:48 PM3/2/15
to d3...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages