Microsoft Automatic Graph Layout (MSAGL)

repository·master·Indexed 23 days ago

https://github.com/microsoft/automatic-graph-layout

A .NET library and toolset for high-performance graph layout computation and visualization. It supports WPF, Windows Forms, and web-based JavaScript/TypeScript via WebMSAGL. The library provides core layout engines, drawing attributes, and viewer controls for rendering graphs to SVG, HTML Canvas, or native UI components. Features include support for subgraphs, specialized node types, and GraphMaps for large-scale visualization.

Tokens
2.4K
Snippets
3
Records
9
Agent score
81%

What's inside MSAGL

  1. Explore GraphMaps for large-scale graph visualization

    master

    GraphMaps is a feature designed for viewing very large graphs using a 'map' metaphor, where detail is revealed as the user zooms in.

    To run the GraphMaps demonstration:

    1. Open GraphLayout.sln and build the solution.
    2. Run TestGraphMaps.

    Note: Use the Release/x64 configuration to load large graphs successfully. If loading .dot files, MSAGL will regenerate the .msagl and tiles directory files.

  2. Produce a release of agl.exe

    master

    Releases containing agl.exe can be triggered in two ways:

    Option 1: GitHub Actions Create a new tag following the v* format (e.g., v_11) and push it to the origin:

    git tag -a v_11 -m "some comment here"
    git push origin v_11

    Option 2: Python Script Use the provided createRelease.py script:

    python createRelease.py 1.1.1
  3. Use WebMSAGL for JavaScript/TypeScript graph layout

    master

    WebMSAGL is a JavaScript version of MSAGL (transcompiled via SharpKit) with a TypeScript wrapper. It allows you to:

    • Create graphs programmatically or from JSON.
    • Run layout operations in a web worker to keep the UI responsive.
    • Render graphs to HTML Canvas or SVG.

    To use it, build the WebMsagl.sln solution and use an index.html from the sample folders as your entry point.

  4. Install MSAGL modules via NuGet

    master

    MSAGL is distributed via several NuGet packages depending on your requirements:

    • AutomaticGraphLayout.dll: The core layout engine. Use this if you only need layout computation and will handle rendering separately.
    • AutomaticGraphLayout.Drawing.dll: Provides drawing attributes (colors, line styles) and core graph objects (Node, Edge, Graph).
    • Microsoft.Msagl.WpfGraphControl.dll: A WPF control for visualizing graphs with features like Pan/Zoom, navigation, tooltips, and entity searching.
    • Microsoft.Msagl.GraphViewerGdi.dll: A Windows Forms viewer control with similar visualization features (Pan/Zoom, navigation, tooltips, and searching).
  5. Quickly preview SVG diagrams using the clipboard

    master

    To test your generated diagrams, you can copy the resulting SVG string directly to your clipboard and paste it into an HTML previewer like JSBin.

    In the sample code, this is achieved using:

    TextCopy.ClipboardService.SetText(doc.ToString());

    Workflow:

    1. Run your C# code.
    2. The SVG text is automatically copied to your clipboard.
    3. Paste the text into the HTML section of JSBin to view the rendered diagram.
  6. Interact with the MSAGL Viewer to edit graphs

    master

    The MSAGL Viewer provides interactive controls for modifying graph structures. Use the following mouse and keyboard interactions to manage nodes and edges:

    Nodes

    • Insert a node: Right-click anywhere on the graph drawing and select an "Insert ..." option from the popup menu.
    • Delete a node: Select the node with a left-click, then right-click and select "Delete selected" from the popup menu.
    • Select multiple nodes: Use a mouse rubber band window or hold the Control key while left-clicking nodes.

    Edges

    • Insert an edge: Position the cursor at the source node, hold the middle mouse button to drag a "rubber string" to the target node.
    • Edit an edge polyline: Select an edge with a left-click. Drag the circular markers at the polyline corners to reshape the edge. You can also right-click a selected edge to insert or delete polyline corners, or delete the edge entirely.
    • Move an edge label: Drag the edge label using the left mouse button.

    Labels and History

    • Edit labels: To edit a node or edge label, type the new text into the textbox located above the viewer control.
    • Undo/Redo: Use the undo and redo buttons located in the viewer toolbar to revert or re-apply changes.
  7. Extract data from an MsaglGraph without a viewer

    master

    The DrawingFromGeometryGraphSample demonstrates how to create an MsaglGraph and manually extract its underlying geometric data for rendering purposes, rather than using the standard MSAGL viewer. This is useful when you need to integrate MSAGL layout results into a custom rendering engine or a non-standard UI component.

    Note that this sample is a partial implementation and does not cover all available node shapes or curve types.

  8. Create an SVG diagram using MSAGL

    master

    The SvgLayerSample demonstrates how to use MSAGL to construct a graph and render it as an SVG. You can define nodes (including specialized types like ComponentNode and LabeledNode), organize them into Subgraph structures, and connect them with edges. The Diagram class is used to process the Graph and generate the SVG output.

    var drawingGraph = new Graph();
    
    // Add various node types
    drawingGraph.AddNode(new ComponentNode("Foo"));
    drawingGraph.AddNode(new ComponentNode("Bar", "Bar Component", "[Azure Functions]", "This is the Bar component, really really important!"));
    drawingGraph.AddNode(new LabeledNode("Component05", new System.Collections.Generic.List<string> { "Component Nr. 5" }));
    
    // Organize nodes into subgraphs
    var subGraph = new Subgraph("Section01");
    subGraph.AddNode(drawingGraph.FindNode("Component02"));
    drawingGraph.RootSubgraph.AddSubgraph(subGraph);
    
    // Add custom labels and styling to nodes
    var labels = new List<Svg.Label> {
        new Svg.Label("Component Nr. 9") {
            Font = new System.Drawing.Font("Consolas", 20f, System.Drawing.FontStyle.Bold),
            Color = Color.Azure
        }
    };
    var labeledNode = new LabeledNode("Component09", labels);
    labeledNode.SvgElement.BackgroundColor = Color.Maroon;
    drawingGraph.AddNode(labeledNode);
    
    // Define edges
    drawingGraph.AddEdge("Foo", "Something", "Bar");
    
    // Render the diagram to SVG
    var doc = new Diagram(drawingGraph);
    doc.Run();
    
    // Output the SVG string
    System.Console.WriteLine(doc.ToString());
  9. Create and visualize a graph in Windows Forms

    master

    To visualize a graph in a Windows Forms application, use the Microsoft.Msagl.GraphViewerGdi.GViewer control. You define a Microsoft.Msagl.Drawing.Graph object, add edges and configure node/edge attributes (like Color, FillColor, or Shape), and then bind the graph to the viewer instance.

    using System;
    using System.Collections.Generic; 
    using System.Windows.Forms; 
    class ViewerSample { 
        public static void Main() { 
        //create a form 
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
        //create a viewer object 
            Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
        //create a graph object 
            Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
        //create the graph content 
            graph.AddEdge("A", "B");
            graph.AddEdge("B", "C");
            graph.AddEdge("A", "C").Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
            graph.FindNode("A").Attr.FillColor = Microsoft.Msagl.Drawing.Color.Magenta;
            graph.FindNode("B").Attr.FillColor = Microsoft.Msagl.Drawing.Color.MistyRose;
            Microsoft.Msagl.Drawing.Node c = graph.FindNode("C");
            c.Attr.FillColor = Microsoft.Msagl.Drawing.Color.PaleGreen;
            c.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Diamond;
        //bind the graph to the viewer 
            viewer.Graph = graph;
        //associate the viewer with the form 
            form.SuspendLayout();
            viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            form.Controls.Add(viewer);
            form.ResumeLayout();
        //show the form 
            form.ShowDialog();
        } 
    }