Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to completely reset a graph? #310

Open
it-pro-berlin-de opened this issue Mar 9, 2023 · 2 comments
Open

How to completely reset a graph? #310

it-pro-berlin-de opened this issue Mar 9, 2023 · 2 comments

Comments

@it-pro-berlin-de
Copy link

Hi, I am not by any standards a JavaScript pro so please bear with me if this is a n00b question.

I created a graph where I'd like to offer the possibility to

  • remove a particular sort of links (marked by colour, in this case yellow) by clicking on a hyperlink outside of the graph
  • select a node and show info about that node in a separate div
  • remove one or more nodes (and the links leading to them) by CTRL+clicking on them

I have borrowed the node removal code from the "Dynamic Data" example, the click on node code from the "Multiselect" example (dumbed it down a bit because I only need single select) and added the removal of yellow links code myself. All this stuff works.

What does NOT work is resetting the whole graph to the initial data. All links and nodes that had not been deleted will get recreated, but the previously deleted nodes will not. Instead, there will be fixed points where the links leading to the missing nodes converge, but not the nodes themselves.

I've put it into a CodePen: https://codepen.io/cj_berlin/pen/JjaOBgx

I am sure I'm not seeing the forest for the trees so, if you could nudge me in the right direction, that would be awesome.

Thanks in advance!

Evgenij

@vasturiano
Copy link
Owner

Hi @it-pro-berlin-de, thanks for reaching out.

Those "stale" coordinates of the nodes are stored in x and y attributes in each of the nodes objects. So, if you want to reset their positions, all you need to do is remove them so they get re-generated from scratch. Something like:

myData.nodes.forEach(node => {
  delete node.x;
  delete node.y;
})

@it-pro-berlin-de
Copy link
Author

Hi, and thanks for the prompt reply, much appreciated.

I don't think I managed to articulate my problem clear enough, though, sorry for that. I do need nodes at those coordinates, and I would have thought that the deletion of stale inventory is taken care of by

siteGraph.graphData({ nodes: [], links: [] });
siteData = siteDataOrig; 
siteGraph.graphData(siteData);

I tried putting the loop you suggested above into my reset functions - to no avail. so maybe it's a bug in the library after all.

For my purposes it will be sufficient, or maybe even more suitable, to recolour the (deleted) nodes and only remove the links touching them. This seems to work in all situations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants