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

Exploding graph issue

Open kongwei-y opened this issue 6 years ago • 4 comments

Describe the bug When the graph updates from a local version, to the exact same version fetched from the server, the graphs nodes and links get separated.

To Reproduce Steps to reproduce the behavior:

Refer to this sandbox for a demonstration:

[Sandbox removed after resolution - message me if you need help with this issue]

In the console, I have logged the graphData state before and after loading from the server. They appear to be the same graph, but things like the vx coordinates differ. I'm unable to simply maintain a "simple" version of the graph, without these extra properties. By simple, I mean just the nodes and links. It seems like the library parses the simple version, into the complete version and there's no way of going in the other direction.

You can comment out the useEffect() function to prevent the app from fetching from the server, to see what the local copy by itself looks like.

Expected behavior The graph should remain stable. In future I was planning to continuously update the local and server versions but at present this isn't possible.

Screenshots Refer to the sandbox above.

Desktop (please complete the following information):

  • OS: [e.g. iOS] Mac
  • Browser [e.g. chrome, safari] Chrome
  • Version [e.g. 22] Latest

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context

This appears to be similar to #234

Thanks for taking the time to reply and to maintain this library. Much appreciated!

kongwei-y avatar Nov 12 '20 03:11 kongwei-y

@TheAussieStew thanks for reaching out.

I've looked into your case and it seems that in the data structure that gets returned by your firebase DB call, your link objects have source and target attributes that reference node objects directly, and these node object references are not in the nodes structure. That is leading to the node/link detachment.

Please note that in order to avoid this issue altogether, it's better to reference the nodes in your source/target link attributes by node id (as you have in your initial/local data structure) rather than by direct reference.

If you correct your data structures returned by the server you shouldn't have this issue anymore.

vasturiano avatar Nov 13 '20 06:11 vasturiano

@vasturiano

I see! I suspected that was the issue. However, I'm not sure how to resolve this, since I believe that the library itself seems to parse the prop "graphData" into this format, if I'm not mistaken. I pass my initial data structure to ForceGraph as you've recommended, without a direct reference to a node object. However, when I log the data that's being uploaded, it appears with references. I'm checking by disabling the server fetch and then clicking on a node object and clicking away. The onClose of the popup then triggers an upload to the server which is logged. This way I'm making sure that the server isn't applying any transformations.

kongwei-y avatar Nov 13 '20 22:11 kongwei-y

@TheAussieStew yes, internally the library modifies those source/target from numeric ids to references. Actually that is happening in the d3-force library, you can read more about it here: https://github.com/d3/d3-force#link_links

In anycase, the safest is if when you save your graph state to your database you transform the links structure and do the inverse conversion of these attributes back into their numeric ids, so that they can be safely re-added back into the module without these discrepancies.

vasturiano avatar Nov 14 '20 02:11 vasturiano

That sounds like the right solution, thanks for replying!

kongwei-y avatar Nov 14 '20 06:11 kongwei-y