Visualize Custom Graph with MS Graph Layout Engine | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

How to Visualize a Custom Graph Layout Using Microsoft Automatic Graph Layout Engine in Diagram for WPF

Diagram for WPF has some built-in, automatic layouts to arrange nodes based on their relationships. Currently, we have three standard layouts hierarchical tree layout, radial tree layout, and organizational layout. If these layouts are not enough, you can use any other third-party or open-source layout engine for arrangements and you can use diagram’s visualization and other cool features. Microsoft Automatic Graph Layout (MSAGL) has advanced layouts in its layout engine. In this blog, we’ll see how to use this layout engine and visualize a layout using Syncfusion’s diagram control. Following are some sample graphs rendered using the MSAGL layout engine.

MSD layout with Spline bundling

 

Sugiyama layout with Sugiyama spline routing
MDS layout with straight line connection
Ranking layout with rectilinear routing
MSD layout with spline bundling
Fast incremental layout with spline routing

Now let’s see the step-by-step process on how to visualize the graph layout and line routing from the MSAGL layout engine into diagram.

Load or prepare a diagram for diagram model

If you are new to diagram, please go through the following topics to understand what diagram is and how to build it using nodes and connectors:

Prepare a diagram with nodes and connectors as you wish. You can create it programmatically or load it from a saved file.

Convert model from diagram to MSAGL

Iterate through each node and connector in the diagram, create an equivalent in MSAGL, and add it as shown in the following example.
// Convert model of SfDiagram to MSAGL.
public static GeometryGraph ToMSAGLGraph(this IGraph sfDiagramModel)
{
    // Create a graph.
    GeometryGraph MSAGLmodel = new GeometryGraph();

    foreach (var node in (sfDiagramModel.Nodes as IEnumerable<INode>))
    {
        // Create MSAGL node.
        Microsoft.Msagl.Core.Layout.Node msaglNode = new Microsoft.Msagl.Core.Layout.Node(

            CurveFactory.CreateRectangle(

                // Specify size of a node.
                node.UnitWidth,
                node.UnitHeight,

                // Specify empty position, as layout will take care of positioning.
                new Microsoft.Msagl.Core.Geometry.Point()),

            // Give reference to diagram node.
            node);

        // Add node into MSAGL model.
        MSAGLmodel.Nodes.Add(msaglNode);
    }

    foreach (var con in sfDiagramModel.Connectors as IEnumerable<IConnector>)
    {
        // Create MSAGL connector.
        MSAGLmodel.Edges.Add(
            new Edge(
                // Set source and target by finding MSAGL node based on SfDiagram node.
                MSAGLmodel.FindNodeByUserData(con.SourceNode), 
                MSAGLmodel.FindNodeByUserData(con.TargetNode))
            {
                Weight = 1,
                UserData = con
            });
    }
    return MSAGLmodel;
}
Configure layout
Choose a layout you wish to use for your diagram from any of the following and configure it using its properties:
  • RankingLayoutSettings: Layout to arrange graph in a tree structure. It will also rearrange in such a way that it minimizes the sum of edge length and edges crossing over each other.
  • MdsLayoutSettings: Multidimensional scaling layout algorithm.
  • FastIncrementalLayoutSettings: Fast incremental layout is a force-directed layout strategy with approximate computation of long-range node-node repulsive forces to achieve O(n log n) running time per iteration.
  • SugiyamaLayoutSettings: Layout to arrange a tree-like structure.

Configure routing technique

Choose one of the following routing techniques and configure it using its properties:
  • Spline: Routing is done using curved segments such as Bezier and Arc segments.
  • SplineBundling: This is also a spline routing. Additionally, it will group similar connections close to each other.
  • StraightLine: Just a straight line connecting the source to target.
  • SugiyamaSplines: A spline curve more suitable for Sugiyama layout.
  • Rectilinear: Orthogonal or perpendicular segments to connect the nodes.
  • RectilinearToCenter: A rectilinear routing but connecting toward the center of a node.

Run MSAGL layout and routing

Run MSAGL layout using the following code.
LayoutHelpers.CalculateLayout(graph, settings, null);
Here, the graph is the MSAGL model and the settings are one of the LayoutAlgorithmSettings.

Convert model from MSAGL back to diagram

To visualize the layout, update node positions and the connector’s segment as shown in the following example.
// Sync SfDiagram model based on MSAGL model.
public static void UpdateSfDiagram(this IGraph diagram, GeometryGraph graph)
{
    // Move model to positive axis.
    graph.UpdateBoundingBox();
    graph.Translate(new Microsoft.Msagl.Core.Geometry.Point(-graph.Left, -graph.Bottom));

    // Update node position.
    foreach (var node in graph.Nodes)
    {
        (node.UserData as INode).OffsetX = node.BoundingBox.Center.X;
        (node.UserData as INode).OffsetY = node.BoundingBox.Center.Y;
    }

    // Update connector segments based on routing.
    foreach (var edge in graph.Edges)
    {
        IConnector connector = edge.UserData as IConnector;
        connector.Segments = new ObservableCollection<IConnectorSegment>();
        SyncSegments(connector, edge);
    }
}

// Sync segments of connector.
private static void SyncSegments(IConnector connector, Edge edge)
{
    var segments = connector.Segments as ICollection<IConnectorSegment>;

    // When curve is a line segment.
    if (edge.Curve is LineSegment)
    {
        var line = edge.Curve as LineSegment;
        connector.SourcePoint = new Point(line.Start.X, line.Start.Y);
        segments.Add(new StraightSegment
        {
            Point = new Point(line.Start.X, line.Start.Y)
        });
        segments.Add(new StraightSegment
        {
            Point = new Point(line.End.X, line.End.Y)
        });
    }

    // When curve is a complex segment.
    else if (edge.Curve is Curve)
    {
        Point? pt = null;
        foreach (var segment in (edge.Curve as Curve).Segments)
        {
            // When curve contains a line segment.
            if (segment is LineSegment)
            {
                var line = segment as LineSegment;
                if (pt == null)
                {
                    pt = new Point(line.Start.X, line.Start.Y);
                    segments.Add(new StraightSegment
                    {
                        Point = pt
                    });
                }
                segments.Add(new StraightSegment
                {
                    Point = new Point(line.End.X, line.End.Y)
                });
            }

            // When curve contains a cubic Bezier segment.
            else if (segment is CubicBezierSegment)
            {
                var bezier = segment as CubicBezierSegment;
                pt = new Point(bezier.B(0).X, bezier.B(0).Y);
                if (pt == null)
                {
                    segments.Add(new StraightSegment
                    {
                        Point = pt
                    });
                }
                segments.Add(new CubicCurveSegment
                {
                    Point1 = new Point(bezier.B(1).X, bezier.B(1).Y),
                    Point2 = new Point(bezier.B(2).X, bezier.B(2).Y),
                    Point3 = new Point(bezier.B(3).X, bezier.B(3).Y),
                });
            }

            // When curve contains an arc.
            else if (segment is Ellipse)
            {
                var ellipse = segment as Ellipse;
                var interval = (ellipse.ParEnd - ellipse.ParStart) / 5.0;
                for (var i = ellipse.ParStart;
                            i < ellipse.ParEnd;
                            i += interval)
                {
                    var p = ellipse.Center
                        + (Math.Cos(i) * ellipse.AxisA)
                        + (Math.Sin(i) * ellipse.AxisB);
                    segments.Add(new StraightSegment
                    {
                        Point = new Point(p.X, p.Y)
                    });
                }
            }
            else
            {

            }
        }
        segments.Add(new StraightSegment());
    }
    else
    {

    }
}
You can refer the sample from GitHub, that uses the MSAGL layout engine for diagram.

Summary

In this post, we have seen how to visualize the MSAGL layout and routing technique using diagram. There are some built-in layouts and routing available in diagram. To explore more in depth, please refer to the help documentation Automatic-Layouts. Refer to MSAGL to learn more about its layout engine.
Try it and give us your feedback.
If you’re already a Syncfusion user, you can download the product setup on Direct-Trac. If you’re not yet a Syncfusion user, you can download a free, 30-day trial on our website.
If you have any questions or require clarification about these features, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you like this blog post, we think you’ll also like the following free e-books:

WPF Succinctly
WPF Debugging and Performance Succinctly
C# Succinctly

Tags:

Share this post:

Comments (2)

Hi,
please it is possible to setup MSAGL Edges corners to 90° degrees:
This is my setup:
GeometryGraph graph = new GeometryGraph();
graph.Nodes!.Add(new Node(CurveFactory.CreateRectangle(80, 60, new Point(100, 100)), null));
graph.Nodes!.Add(new Node(CurveFactory.CreateRectangle(80, 60, new Point(94, 80)), null));
graph.Edges!.Add(new Edge(graph.Nodes[0], graph.Nodes[1]) {
Weight = 1,
UserData = null
});
graph.UpdateBoundingBox();
graph.Translate(new Point(-graph.Left, -graph.Bottom));

This is my result still without 90° corners:
https://i.stack.imgur.com/jhWBa.png

Thanks.

Hi Lukas,

We have modified the sample, based on your requirement for the edges of the Connector in 90 degrees. From the MSAGL Layout, we get the curve of the edge(Connectors) with many types like LineSegment, CubicBezierSegment, and Ellipse then we converted those edges into our SfDiagram connector segments using their points to achieve your requirement. Please find the modified sample for your requirement in the below link.

Link: https://github.com/SyncfusionExamples/WPF-Diagram-Examples/tree/master/Samples/Automatic%20Layout/Custom%20Layout/wpf-diagram-custom-layout-master

Regards,
Sarath

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed