vasturiano/d3-force-3d

Possible to export layout coordinates?

khughitt opened this issue · 3 comments

Greetings!

I was wondering if there is any way to export the rendered layout coordinates?

I have an application where I would like to generate a layout, save the coordinates, and then restore the layout coordinates in the future (possibly with some additional "new" nodes added around the previously generated network).

It looks like one can restore & fix node coordinates using simulation.nodes, which is really useful.

Is there any way to capture the layout though? i.e. is there any "getter" for the current node positions?

If not, I imagine I could probably compute the layout externally and simply provide the initial coordinates that way, but if there is a way to export the coordinates, or if it would not be too hard to add such a method, that would be really useful.

Thanks for all of your great work on this and 3d-force-graph! These are both incredibly useful and elegant libraries. I appreciate you taking the time to create and share them with the community.

Cheers,
Keith

@khughitt thanks for reaching out.

The nodes coordinates state is kept in the nodes themselves. So at any given time you can look for the { x, y, z } attributes in each of your nodes and that will give all the info necessary to reconstruct the layout. For example, you could have a simple function like this to extract all the coords:

const getLayout = nodes => nodes.map(node => ({
  id: node.id,
  x: node.x,
  y: node.y,
  z: node.z
));

More info about the structure of nodes here.

Awesome! That will do it. Thanks for the quick response and suggestion!

@vasturiano 我想问问,什么情况下,节点位置会丢失,导致出现如下情况
image
鼠标并未移动到球体上,则提示信息出来了。