How do I make my force-directed graph stay within canvas bounds?

4,712 views
Skip to first unread message

chizeng

unread,
Apr 10, 2012, 3:39:33 AM4/10/12
to d3-js
I have a force-directed graph with over 100 nodes constructed in d3.
http://goo.gl/UDmqh

Many of the nodes lie off of the screen. What can I do to keep the
nodes within the bounds of the canvas and in the view area? Thank you.

Kai Chang

unread,
Apr 10, 2012, 3:50:31 AM4/10/12
to d3...@googlegroups.com
See the tick function in this example:

Mike Bostock

unread,
Apr 10, 2012, 12:29:45 PM4/10/12
to d3...@googlegroups.com
Another option is to adjust the force layout's parameters dynamically
based on the data. For example, by increasing the gravity force, you
make it harder for nodes to escape the canvas. More discussion here:

http://stackoverflow.com/questions/9901565/charge-based-on-size-d3-force-layout

Mike

Chi Zeng

unread,
Apr 10, 2012, 1:41:39 PM4/10/12
to d3...@googlegroups.com
Thank you! The gravity idea is a smart one. Obtaining the minimum position of either the boundary of the canvas or the calculated position in the tick function works fine too.
Reply all
Reply to author
Forward
0 new messages