Skip to content
/ D2.Net Public

Fluent .NET Wrapper for creating D2 Language templates.

License

Notifications You must be signed in to change notification settings

Snappey/D2.Net

Repository files navigation

D2.Net

D2

A .NET Library for programatically creating D2 Templates.

D2 Language created by Terrastruct

Latest Version NuGet License

About

This library aims to provide a simple and expressive way to create D2 templates that can be easily integrated into any .NET application. Enabling easy generation of diagrams for documentation, reports, and more based off of data from your application.

Currently this library is in early development and not all D2 features are supported, see below for a high level list of supported features.

Supported D2 Features

  • Shapes
    • Labels
    • Containers / Children
    • Styling
  • Connections
    • Labels
    • Arrow Styling/Directions
    • Styling
  • Tooltips / Links
  • Icons
  • Standalone Text / Markdown / LateX
  • Sequence Diagrams
  • UML/Class Diagrams
  • SQL Table Diagrams

Installation

D2.Net is available on NuGet.

dotnet add package D2.Net

Usage

Here is an example of creating a fairly complex template using D2.Net with most of the implemented features.

You can check Examples to see this and a more basic setup.

using D2;

var styledTemplate = Diagram.Create()
    .CreateShape("D", type: ShapeType.Circle)
    .CreateShape("E", "Styled Shape", ShapeType.Hexagon)
    .CreateShape("F", "Styled Diamond Shape", ShapeType.Diamond)
    .CreateDirectionalConnection("D", "E", "Styled Connection", new ArrowheadOptions(ArrowheadType.CrowsFootManyRequired, "Filled Triangle"))
    .CreateBidirectionalConnection("E", "F", "Bidirectional Connection",
        new ArrowheadOptions(ArrowheadType.CrowsFootManyRequired),
        new ArrowheadOptions(ArrowheadType.CrowsFootManyRequired))
    .CreateConnection("F", "D")
    .CreateMarkdownShape("G", @"  # I can do headers

  - lists
  - lists

  And other normal markdown stuff")
    .CreateLatexShape("H", @"\\lim_{h \\rightarrow 0 } \\frac{f(x+h)-f(x)}{h}")
    .CreateBidirectionalConnection("G", "H");

Console.WriteLine(styledTemplate);

This will output the following D2 code:

D: {
  shape: circle
}
E: Styled Shape {
  shape: hexagon
}
F: Styled Diamond Shape {
  shape: diamond
}
D -> E: Styled Connection {
  target-arrowhead: Filled Triangle { shape: cf-many-required; style.filled: true }
}
E <-> F: Bidirectional Connection {
  source-arrowhead: { shape: cf-many-required; style.filled: true }
  target-arrowhead: { shape: cf-many-required; style.filled: true }
}
F -> D
G: |md
  # I can do headers

  - lists
  - lists

  And other normal markdown stuff|
H: |latex
\\lim_{h \\rightarrow 0 } \\frac{f(x+h)-f(x)}{h}|
G <-> H: Bidirectional Connection {
  source-arrowhead: { shape: cf-many-required; style.filled: true }
  target-arrowhead: { shape: cf-many-required; style.filled: true }
}

That is rendered to the following diagram, using the D2 Playground! Basic Template