Nodes in force graphs beginning position random

2,599 views
Skip to first unread message

rafael lage

unread,
Jan 28, 2012, 8:31:53 AM1/28/12
to d3...@googlegroups.com
Hi people, I'm having another problem with force directed graphs, but I couldn't express myself well in the title, I suppose.

When my graph is loaded, the nodes and paths starts in a random position, sometimes even outside the svg frame, and then they are sucked by that gravity function. I couldn't find anything that helps me draw the graph in the center of svg. Is there a way to do it?

Rafael Lage Tavares
Unirio - Universidade Federal do Estado do Rio de Janeiro

wlindner

unread,
Jan 28, 2012, 12:01:38 PM1/28/12
to d3-js
Just set the 'x' and 'y' attributes of the nodes that are being added
to the force directed layout to be the center of the graph:

https://github.com/mbostock/d3/wiki/Force-Layout#wiki-nodes

rafael lage

unread,
Jan 28, 2012, 1:08:48 PM1/28/12
to d3...@googlegroups.com
Hey thanks for the response. I didn't know this was possible, I thought that the force.start() would mess the coordinates.


Rafael Lage Tavares
Unirio - Universidade Federal do Estado do Rio de Janeiro


2012/1/28 wlindner <wlind...@gmail.com>

Mike Bostock

unread,
Jan 28, 2012, 1:41:42 PM1/28/12
to d3...@googlegroups.com
> I thought that the force.start() would mess the coordinates.

It only initializes the current position or the previous position if
those fields are undefined:

https://github.com/mbostock/d3/blob/master/src/layout/force.js#L202-205

Mike

rafael lage

unread,
Jan 28, 2012, 1:50:20 PM1/28/12
to d3...@googlegroups.com
Oh I see. Thanks for that Mike!

Rafael Lage Tavares
Unirio - Universidade Federal do Estado do Rio de Janeiro


2012/1/28 Mike Bostock <mbos...@cs.stanford.edu>
Reply all
Reply to author
Forward
0 new messages