Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log the x,y coordinates of nodes in a converged D3 force layout

I have a graph with about 600 nodes. After a while, d3.layout.force() converges. How can I display the (x,y) coordinates of the nodes such that I can store them as data and use them as a cached layout as described in this answer? The answer may be quite simple, as I'm a novice with javascript.

like image 702
Dan Hook Avatar asked Oct 16 '25 10:10

Dan Hook


1 Answers

After the force layout converged, you can iterate over the list of nodes to get the current coordinates:

var force = d3.layout.force().nodes(nodes);

// after convergence
var positions = nodes.map(function(d) { return [d.x, d.y]; });

This assumes that your browser supports map, if not see here.

You can of course run this at every tick and save the result each time; this saves you figuring out when the layout has converged.

like image 127
Lars Kotthoff Avatar answered Oct 19 '25 10:10

Lars Kotthoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!