force-graph icon indicating copy to clipboard operation
force-graph copied to clipboard

Add documentation to README explaining how to access node data

Open tensiondriven opened this issue 5 years ago • 0 comments

Is your feature request related to a problem? Please describe. Yes, I am trying to programmatically zoom the view to a specific node (without clicking on it)

Describe the solution you'd like I'd like the readme to explain how to access the node elements with the (x, y) data added.

Describe alternatives you've considered I read through all of the closed issues and finally found the information I needed (below)

I tried inspecting the value returned by ForceGraph for any kind of attribute that might have a list of nodes, but found none.

I examined my original graphData structure after running the ForceGraph and saw some extra data, but the x and y coords were still missing, which perplexed me.

Additional context

In https://github.com/vasturiano/force-graph/issues/88#issuecomment-574812291 it is stated:

For the centering you just have to grab the node. .onNodeClick provides the node itself back that you can use the x and y from then just graph.centerAt(node.x, node.y, 500). If you need to access the node from some outside location, you just need to get it from the data location you fed into the graph. The graph actually uses and edits that data source. Ex.

const data = { nodes: [ ... ], links: [ ... ] } myGraph.graphData(data);

const getNode = id => { return data.nodes.find(node => node.id === id); } With this example using getNode(id) would get you the node object with all the data the graph has added to it, including it's current x and y to feed into .centerAt

It wasn't obvious to me how to do this until I found this little bit of explanation. It would be helpful if the README contained something about accessing the graph data.

tensiondriven avatar May 03 '20 03:05 tensiondriven