Using GoDiagram in Console Applications

GoDiagram can be used in console environments without a visual control. However there are some considerations:

  • A separate library will be used, Northwoods.GoDiagram.Console.
  • Because there is no UI message pump like a typical application, you must wrap your application with a call to Northwoods.Go.Console.Application.Run. This allows the diagram to be treated as if it were single threaded as it would be in an environment with a view.
  • Since there is no control, you must instead set the Diagram.ViewSize property. This affects all the same values as the control's size, like Diagram.Position and layout results from layouts that are viewport-sized.

Console example

If you save the following code into a console application, it will output Model JSON results in the console, which include the locations of laid-out Nodes. You can use the console in this way to do server-side operations like large layouts, and then send the JSON to the client.


  // Program.cs
  // This example loads the GoDiagram library, creates a Diagram with a layout and prints the JSON results.

  using System;
  using System.Collections.Generic;

  using Northwoods.Go;
  using Northwoods.Go.Console;
  using Northwoods.Go.Layouts;
  using Northwoods.Go.Models;

  Application.Run(() => {
    var myDiagram = new Diagram {
      ViewSize = new Size(400, 400),  // Set this property in viewless environments
      Layout = new LayeredDigraphLayout(),
    };
    myDiagram.AnimationManager.IsEnabled = false;

    myDiagram.NodeTemplate =
      new Node(PanelType.Auto)
        // automatically save the Node.Location to the node's data object
        .Bind("Location", "Loc", Point.Parse, Point.Stringify)
        .Add(
          new Shape("RoundedRectangle") { StrokeWidth = 0 }
            .Bind("Fill", "Color"),
          new TextBlock().Bind("Text", "Key")
        );

    myDiagram.InitialLayoutCompleted += (s, e) => {
      Console.WriteLine(myDiagram.Model.ToJson());
    };

    myDiagram.Model =
      new Model {
        NodeDataSource = new List {
          new NodeData { Key = "Alpha", Color = "lightblue" },
          new NodeData { Key = "Beta", Color = "orange" },
          new NodeData { Key = "Gamma", Color = "lightgreen" },
          new NodeData { Key = "Delta", Color = "pink" }
        },
        LinkDataSource = new List {
          new LinkData { Key = "AB", From = "Alpha", To = "Beta" },
          new LinkData { Key = "AG", From = "Alpha", To = "Gamma" },
          new LinkData { Key = "GD", From = "Gamma", To = "Delta" },
          new LinkData { Key = "DA", From = "Delta", To = "Alpha" }
        }
      };

    // Could call Diagram.MaybeUpdate here to ensure diagram updates are complete
    // prior to performing any operations on the diagram.
    // Another option is to pass an async function to Application.Run and call await Task.Delay(500),
    // which would give time for async loading of web images, for example.

    // myDiagram.MakeImage("diagramImg.png");
  });

  // define the model data
  public class Model : GraphLinksModel { }
  public class NodeData {
    public string Key { get; set; }
    public string Loc { get; set; }
    public string Color { get; set; }

  }
  public class LinkData {
    public string Key { get; set; }
    public string From { get; set; }
    public string To { get; set; }
  }