microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.35k stars 304 forks source link

[Question] Enforcing a top to bottom layout? #247

Closed SMTStuck closed 4 years ago

SMTStuck commented 4 years ago

I have a simple sample of three nodes and two edges, and am usinng only the core layout engine and doing all rendering myself.

Node 1 connects to Node 2
Node 3 connects to Node 2

The code below gives the following layout:

Result

I've browsed the code to the best of my ability, but I cannot see anything that enforce a top to bottom layout as follows:

Expected

I know I can transform the graph, but I cannot do this unless the graph is always laid out bottom to top. So, is there a way to enforce the graph to always lay out top to bottom, or can I at least rely on the graph consistently being laid out bottom to top?

Thank you.

Accompanying code:

List<Node> nodes = new List<Node>();
List<Edge> edges = new List<Edge>();

nodes.Add(new Node(CurveFactory.CreateRectangle(50, 50, new Microsoft.Msagl.Core.Geometry.Point(0, 0)), "1"));
nodes.Add(new Node(CurveFactory.CreateRectangle(50, 50, new Microsoft.Msagl.Core.Geometry.Point(0, 0)), "2"));
nodes.Add(new Node(CurveFactory.CreateRectangle(50, 50, new Microsoft.Msagl.Core.Geometry.Point(0, 0)), "3"));
edges.Add(new Edge(nodes[0], nodes[1]));
edges.Add(new Edge(nodes[2], nodes[1]));

IEnumerable<GeometryGraph> subGraphs = GraphConnectedComponents.CreateComponents(nodes, edges);
SugiyamaLayoutSettings layoutSettings = new SugiyamaLayoutSettings();
foreach (GeometryGraph subgraph in subGraphs)
{
    LayeredLayout layout = new LayeredLayout(subgraph, layoutSettings);
    subgraph.Margins = layoutSettings.NodeSeparation + 50;
    layout.Run();
}
Microsoft.Msagl.Layout.MDS.MdsGraphLayout.PackGraphs(subGraphs, layoutSettings);
levnach commented 4 years ago

I believe , the SugiyamaScheme starts exploring depth first from nodes[0] and will reverse each edge that leads back to the visited ones. Maybe it is not the best approach but that is how it works. So, if you have a DAG, to avoid reversed edges add the nodes in the topological sort order.

SMTStuck commented 4 years ago

Much appreciated.