Force directed graph layout in JavaScript
dhotson.tumblr.com
dhotson.tumblr.com
For this demo I was really focusing on simplicity and getting the basics right. I just wanted dots and lines but was too lazy to figure SVG DOM manipulation. Raphael makes it really easy.
In one case, I observed an edge coming to rest just across an unconnected (to that edge) node. Might be hard to detect/work around ...
[Many moons ago, for an exercise, implemented a force-directed placement, with team-mates doing Kernighan-Lin min-cut (also fast) and simulated annealing (slow) ...]
P.S. Two observations:
- You highlight the node on mouse over. If the edges connected directly to that node were also highlighted, it would palliate the false connection problem.
- Adding some edge-edge repulsion ? Sometimes edges cross, and one of them could be flipped to another side for a locally planar solution.
I was thinking about making edges repel nodes in some way to avoid the problem you mentioned. I'll have to experiment some more..
I hit the same problem of crossing edges when doing this a while ago (in Python rather than JavaScript).
Rather than trying to get everything done automatically by the code I added the ability to click and drag a point. So the algorithm gets you close to a good layout and you can then make adjustments to avoid unwanted crossings.
It reminds me of an old HN comment "think cyborg, not robot" (http://news.ycombinator.com/item?id=164833).
I think that's a good idea. The force directed layout algorithm isn't bullet proof, but it's a good approximate layout for a human to tweak.
(If you have a Mac, check the included Grapher tool mentioned in that discussion - may not have edge labels)
http://github.com/blog/621-bye-bye-flash-network-graph-is-no...
and