vasturiano/force-graph

Await drawing of graph to get color from graphData().nodes

Closed this issue · 2 comments

What I try to do is using the colors that nodeAutoColorBy('nodeType') adds to the Graph object. In the webpage I want to add buttons to show/hide nodes of a particular type. I would like to give these buttons the same color as the color assigned to the nodeType by force-graph.

I found #305 but I think I'm missing / overlooking something.

My scenario is that I draw two graphs, one with proposed changes and one being the current one. So two API calls (fetches), both providing [nodes, links]. When I draw the graphs and then try to access Graph[0].graphData().nodes[0].color (for instance, Graph being a global holding the objects of the graphs), this field is not filled in yet (undefined).

My guess is that the graphs are drawn async, and that I should wait on "something" for them (both) to really complete.

Perhaps this is a dumb question, but I hope someone is willing to point me in the right direction.

@hvoerman thanks for reaching out.

As you've mentioned the rendering runs asynchronously. You can use onRenderFramePost.

If you want to just run it the first frame, you can do something like:

myGraph.onRenderFramePost(() => {
  myGraph.onRenderFramePost(null);
  const myNodes = myGraph.graphData().nodes;
  // do something with the node colors
});

@vasturiano thanks for the response and keep up your good work please! Issue may be closed now as far as I am concerned.