The layered graph layout is used to layout a graph in layers. The layout strives to minimize the number of crossings between edges and produces a polyline graph drawing. This type of layout is very useful for the arrangement of flow diagrams, since it works well on all types of graphs (including those with self-loops and duplicate edges).
The following code example will help you create a random graph and apply Layered Graph Layout to it, exploring most of the layout properties:
[C#]
using
System;
using
System.Windows.Forms;
using
Nevron.Diagram;
using
Nevron.Diagram.Filters;
using
Nevron.Diagram.Layout;
using
Nevron.Diagram.Templates;
using
Nevron.Dom;
...
private
void
Form1_Load(
object
sender, EventArgs e)
{
view.BeginInit();
view.Dock = DockStyle.Fill;
view.ViewLayout = ViewLayout.Fit;
view.Grid.Visible =
false
;
view.GlobalVisibility.ShowPorts =
false
;
document.BeginInit();
// create a random graph
NRandomGraphTemplate randomGraph =
new
NRandomGraphTemplate();
randomGraph.VertexCount = 30;
randomGraph.EdgeCount = 35;
randomGraph.Create(document);
// get the shapes to layout
NNodeList shapes = document.ActiveLayer.Children(NFilters.Shape2D);
// use layered graph layout
NLayeredGraphLayout layout =
new
NLayeredGraphLayout();
layout.Direction = LayoutDirection.TopToBottom;
layout.EdgeRouting = LayeredLayoutEdgeRouting.Orthogonal;
layout.NodeRank = LayeredLayoutNodeRank.Optimal;
layout.PlugSpacing.Mode = PlugSpacingMode.Proportional;
layout.PlugSpacing.Offset = 0;
layout.LayerAlignment = RelativeAlignment.Near;
layout.NodeAlignment = RelativeAlignment.Center;
layout.SelfLoopSpacingFactor = 0.25f;
layout.VertexSpacing = 40;
layout.LayerSpacing = 40;
layout.StraightenLines =
false
;
layout.UseSingleBus =
false
;
layout.Compact =
false
;
layout.Layout(shapes,
new
NDrawingLayoutContext(document));
// size to visible shapes
document.SizeToContent(document.AutoBoundsMinSize, document.AutoBoundsPadding, NFilters.Visible);
document.EndInit();
view.EndInit();
}
[VB.NET]
Imports
System
Imports
System.Windows.Forms
Imports
Nevron.Diagram
Imports
Nevron.Diagram.Filters
Imports
Nevron.Diagram.Layout
Imports
Nevron.Diagram.Templates
Imports
Nevron.Dom
...
Private
Sub
Form1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
view.BeginInit()
view.Dock = DockStyle.Fill
view.ViewLayout = ViewLayout.Fit
view.Grid.Visible =
False
view.GlobalVisibility.ShowPorts =
False
document.BeginInit()
' create a random graph
Dim
randomGraph
As
New
NRandomGraphTemplate()
randomGraph.VertexCount = 30
randomGraph.EdgeCount = 35
randomGraph.Create(document)
' get the shapes to layout
Dim
shapes
As
NNodeList = document.ActiveLayer.Children(NFilters.Shape2D)
' use layered graph layout
Dim
layout
As
New
NLayeredGraphLayout()
layout.Direction = LayoutDirection.TopToBottom
layout.EdgeRouting = LayeredLayoutEdgeRouting.Orthogonal
layout.NodeRank = LayeredLayoutNodeRank.Optimal
layout.PlugSpacing.Mode = PlugSpacingMode.Proportional
layout.PlugSpacing.Offset = 0
layout.LayerAlignment = RelativeAlignment.Near
layout.NodeAlignment = RelativeAlignment.Center
layout.SelfLoopSpacingFactor = 0.25F
layout.VertexSpacing = 40
layout.LayerSpacing = 40
layout.StraightenLines =
False
layout.UseSingleBus =
False
layout.Compact =
False
layout.Layout(shapes,
New
NDrawingLayoutContext(document))
' size to visible shapes
document.SizeToContent(document.AutoBoundsMinSize, document.AutoBoundsPadding, NFilters.Visible)
document.EndInit()
view.EndInit()
End
Sub