vasturiano / d3-force-3d

Force-directed graph layout in 1D, 2D or 3D using velocity Verlet integration.
https://observablehq.com/@vasturiano/multi-dimensional-d3-force-simulation
MIT License
369 stars 52 forks source link

Possible to export layout coordinates? #15

Closed khughitt closed 3 years ago

khughitt commented 3 years ago

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

vasturiano commented 3 years ago

@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.

khughitt commented 3 years ago

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

BISDMI commented 1 year ago

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