microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.36k stars 307 forks source link

Assigning point position to node #363

Closed wbarisch closed 10 months ago

wbarisch commented 10 months ago

Is there a way to specify the position of a node in the graph? For example: Graph graph = graphViewer.Graph; graph.AddNode("2"); Microsoft.Msagl.Core.Geometry.Point position = (199,991); graph.FindNode("2").GeometryNode.Center = position; graphViewer.Graph = graph;

This code doesn't change the position of the point. Up to the "graphViewer.Graph = graph;" graph still has the right node("2") position, but after the last command, both graph and graphViewer.Graph change node's position to the original. Any tips how to solve this? Thank you

levnach commented 10 months ago

The statement graphViewer.Graph = graph; actually does a lot of work: it calls the layout algorithm that sets the node positions and routes the edges, and then graph is rendered. Setting the center of the node before this statement will not have any effect.

It is not a popular scenario, but you can take over the layout and pre-calculate all the geometry, then set graphViewer.NeedToCalculateLayout = false; before calling graphViewer.Graph = graph;. In this case your geometry will be preserved.

wbarisch commented 10 months ago

Thank you for answering. It does work with graphViewer.NeedToCalculateLayout = false;. Does the statement graph.AddEdge("node1", "node2"); also calls for the layout algorithm? Assuming both nodes are already in the graph, but their original positions have been changed. I've tried to set an edge after changing the nodes position and got an NullReferenceException. image

levnach commented 10 months ago

No, it does not.