|
1 | 1 | # Mermaid.NET.Flowcharts |
2 | | -Simple class library for modelling basic Mermaid Flowcharts, does not include any huge or complex features. |
| 2 | + |
| 3 | +A lightweight .NET library for generating Mermaid flowchart markup. This library focuses on the core features of Mermaid flowcharts without unnecessary complexity. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Support for all standard Mermaid node shapes |
| 8 | +- Customizable link styles and directions |
| 9 | +- Subgraph support |
| 10 | +- Unicode text support |
| 11 | + |
| 12 | +## Basic Usage |
| 13 | + |
| 14 | +`Mermaid.NET.Flowcharts` allows you to quickly define nodes and links between nodes so that you can add them to a flowchart. The `ToMermaidString()` method handles the formatting of the output automatically, allowing you to copy and paste the output inside a Mermaid block to render it in Markdown. |
| 15 | + |
| 16 | +```cs |
| 17 | +using Mermaid.Flowcharts; |
| 18 | +using Mermaid.Flowcharts.Nodes; |
| 19 | +using Mermaid.Flowcharts.Links; |
| 20 | + |
| 21 | +// Create a new flowchart |
| 22 | +FlowchartTitle flowchartTitle = FlowchartTitle.FromString("Basic usage"); |
| 23 | +Flowchart flowchart = new(flowchartTitle); |
| 24 | + |
| 25 | +// Create nodes |
| 26 | +Node start = Node.Create("start", "Start"); |
| 27 | +Node process = Node.Create("process", "Process Data"); |
| 28 | +Node stop = Node.Create("stop", "Stop"); |
| 29 | + |
| 30 | +// Create links |
| 31 | +Link startToProcess = new(start, process, default); |
| 32 | +Link processToEnd = new(process, stop, default); |
| 33 | + |
| 34 | +// Add nodes and links to the flowchart |
| 35 | +flowchart |
| 36 | + .AddNode(start) |
| 37 | + .AddNode(process) |
| 38 | + .AddNode(stop) |
| 39 | + .AddLink(startToProcess) |
| 40 | + .AddLink(processToEnd); |
| 41 | + |
| 42 | +// Generate the Mermaid output |
| 43 | +string mermaid = flowchart.ToMermaidString(); |
| 44 | +``` |
| 45 | + |
| 46 | +Will generate the following Mermaid output: |
| 47 | + |
| 48 | +```mermaid |
| 49 | +--- |
| 50 | +title: Basic usage |
| 51 | +--- |
| 52 | +flowchart TD |
| 53 | + start["Start"] |
| 54 | + process["Process Data"] |
| 55 | + stop["Stop"] |
| 56 | +
|
| 57 | + start ---> process |
| 58 | + process ---> stop |
| 59 | +
|
| 60 | +``` |
| 61 | + |
| 62 | +## Advanced features |
| 63 | + |
| 64 | +### Node shapes |
| 65 | + |
| 66 | +Use the `Node.Create` factory method with a custom `NodeShape` to generate nodes with different shapes. Nodes are rectangular by default. |
| 67 | + |
| 68 | +```cs |
| 69 | +using Mermaid.Flowcharts; |
| 70 | +using Mermaid.Flowcharts.Nodes; |
| 71 | + |
| 72 | +// Create a new flowchart |
| 73 | +FlowchartTitle flowchartTitle = FlowchartTitle.FromString("Various node shapes"); |
| 74 | +Flowchart flowchart = new(flowchartTitle); |
| 75 | + |
| 76 | +// Create nodes with various shapes |
| 77 | +Node rectangle = Node.Create("rectangle", "Rectangle", NodeShape.Rectangle); |
| 78 | +Node rounded = Node.Create("rounded", "RoundedEdges", NodeShape.RoundedEdges); |
| 79 | +Node stadium = Node.Create("stadium", "Stadium", NodeShape.Stadium); |
| 80 | +Node subroutine = Node.Create("subroutine", "Subroutine", NodeShape.Subroutine); |
| 81 | +Node cylindrical = Node.Create("cylindrical", "Cylindrical", NodeShape.Cylindrical); |
| 82 | +Node circle = Node.Create("circle", "Circle", NodeShape.Circle); |
| 83 | +Node doubleCircle = Node.Create("doubleCircle", "DoubleCircle", NodeShape.DoubleCircle); |
| 84 | +Node asymmetric = Node.Create("asymmetric", "Asymmetric", NodeShape.Asymmetric); |
| 85 | +Node rhombus = Node.Create("rhombus", "Rhombus", NodeShape.Rhombus); |
| 86 | +Node hexagon = Node.Create("hexagon", "Hexagon", NodeShape.Hexagon); |
| 87 | +Node parallelogram = Node.Create("parallelogram", "Parallelogram", NodeShape.Parallelogram); |
| 88 | +Node parallelogramAlt = Node.Create("parallelogramAlt", "ParallelogramAlt", NodeShape.ParallelogramAlt); |
| 89 | +Node trapezoid = Node.Create("trapezoid", "Trapezoid", NodeShape.Trapezoid); |
| 90 | +Node trapezoidAlt = Node.Create("trapezoidAlt", "TrapezoidAlt", NodeShape.TrapezoidAlt); |
| 91 | + |
| 92 | +// Add the nodes to the flowchart |
| 93 | +flowchart |
| 94 | + .AddNode(rectangle) |
| 95 | + .AddNode(rounded) |
| 96 | + .AddNode(stadium) |
| 97 | + .AddNode(subroutine) |
| 98 | + .AddNode(cylindrical) |
| 99 | + .AddNode(circle) |
| 100 | + .AddNode(doubleCircle) |
| 101 | + .AddNode(asymmetric) |
| 102 | + .AddNode(rhombus) |
| 103 | + .AddNode(hexagon) |
| 104 | + .AddNode(parallelogram) |
| 105 | + .AddNode(parallelogramAlt) |
| 106 | + .AddNode(trapezoid) |
| 107 | + .AddNode(trapezoidAlt); |
| 108 | + |
| 109 | +// Generate the Mermaid output |
| 110 | +string mermaid = flowchart.ToMermaidString(); |
| 111 | +``` |
| 112 | + |
| 113 | +Will generate the following Mermaid output: |
| 114 | + |
| 115 | +```mermaid |
| 116 | +--- |
| 117 | +title: Various node shapes |
| 118 | +--- |
| 119 | +flowchart TD |
| 120 | + rectangle["Rectangle"] |
| 121 | + rounded("RoundedEdges") |
| 122 | + stadium(["Stadium"]) |
| 123 | + subroutine[["Subroutine"]] |
| 124 | + cylindrical[("Cylindrical")] |
| 125 | + circle(("Circle")) |
| 126 | + doubleCircle((("DoubleCircle"))) |
| 127 | + asymmetric>"Asymmetric"] |
| 128 | + rhombus{"Rhombus"} |
| 129 | + hexagon{{"Hexagon"}} |
| 130 | + parallelogram[/"Parallelogram"/] |
| 131 | + parallelogramAlt[\"ParallelogramAlt"\] |
| 132 | + trapezoid[/"Trapezoid"\] |
| 133 | + trapezoidAlt[\"TrapezoidAlt"/] |
| 134 | +
|
| 135 | +``` |
| 136 | + |
| 137 | +### Link styles |
| 138 | + |
| 139 | +When creating a link between nodes, you can specify a `LinkStyle`. |
| 140 | +The link style lets you define the *thickness*, the *direction* and the *arrow type* of a link. |
| 141 | + |
| 142 | +```cs |
| 143 | +using Mermaid.Flowcharts; |
| 144 | +using Mermaid.Flowcharts.Nodes; |
| 145 | +using Mermaid.Flowcharts.Links; |
| 146 | + |
| 147 | +// Create a new flowchart |
| 148 | +FlowchartTitle flowchartTitle = FlowchartTitle.FromString("Link styles"); |
| 149 | +Flowchart flowchart = new(flowchartTitle); |
| 150 | + |
| 151 | +// Create two nodes |
| 152 | +Node a = Node.Create("a", "A"); |
| 153 | +Node b = Node.Create("b", "B"); |
| 154 | + |
| 155 | +// Create links with specific styles and text |
| 156 | +LinkStyle arrowLeftToRightNormal = new(LinkArrowType.Arrow, LinkDirection.LeftToRight, LinkThickness.Normal); |
| 157 | +Link arrowLeftToRightNormalLink = new(a, b, arrowLeftToRightNormal); |
| 158 | +LinkStyle circleRightToLeftDotted = new(LinkArrowType.Circle, LinkDirection.RightToLeft, LinkThickness.Dotted); |
| 159 | +Link circleRightToLeftDottedLink = new(a, b, circleRightToLeftDotted); |
| 160 | +LinkStyle crossBothThick = new(LinkArrowType.Cross, LinkDirection.Both, LinkThickness.Thick); |
| 161 | +Link crossBothThickLink = new(a, b, crossBothThick); |
| 162 | +LinkStyle noneNormal = new(LinkArrowType.None, LinkDirection.Both, LinkThickness.Normal); |
| 163 | +LinkText text = LinkText.FromString("link text"); |
| 164 | +Link noneNormalTextLink = new(a, b, noneNormal, text); |
| 165 | + |
| 166 | +// Add nodes and links to the flowchart |
| 167 | +flowchart |
| 168 | + .AddNode(a) |
| 169 | + .AddNode(b) |
| 170 | + .AddLink(arrowLeftToRightNormalLink) |
| 171 | + .AddLink(circleRightToLeftDottedLink) |
| 172 | + .AddLink(crossBothThickLink) |
| 173 | + .AddLink(noneNormalTextLink); |
| 174 | + |
| 175 | +// Generate the Mermaid output |
| 176 | +string mermaid = flowchart.ToMermaidString(); |
| 177 | +``` |
| 178 | + |
| 179 | +Will generate the following Mermaid output: |
| 180 | + |
| 181 | +```mermaid |
| 182 | +--- |
| 183 | +title: Link styles |
| 184 | +--- |
| 185 | +flowchart TD |
| 186 | + a["A"] |
| 187 | + b["B"] |
| 188 | +
|
| 189 | + a ---> b |
| 190 | + a o-.- b |
| 191 | + a x===x b |
| 192 | + a ---|link text| b |
| 193 | +
|
| 194 | +``` |
| 195 | + |
| 196 | +### Subgraphs |
| 197 | + |
| 198 | +It is also possible to create subgraphs, which are a type of node that can themselves contain nodes. |
| 199 | +Subgraphs can be nested within subgraphs, and links can exist between them. |
| 200 | +However, all links are managed on the flowchart level, not on the subgraph level. |
| 201 | + |
| 202 | +```cs |
| 203 | +using Mermaid.Flowcharts; |
| 204 | +using Mermaid.Flowcharts.Nodes; |
| 205 | +using Mermaid.Flowcharts.Links; |
| 206 | + |
| 207 | +// Create a new flowchart with a node |
| 208 | +FlowchartTitle flowchartTitle = FlowchartTitle.FromString("Using subgraphs"); |
| 209 | +Flowchart flowchart = new(flowchartTitle); |
| 210 | +Node node = Node.Create("n", "Node"); |
| 211 | +flowchart.AddNode(node); |
| 212 | + |
| 213 | +// Create a subgraph with one node |
| 214 | +NodeIdentifier subgraphId = NodeIdentifier.FromString("sg"); |
| 215 | +MermaidUnicodeText subgraphLabel = MermaidUnicodeText.FromString("Subgraph"); |
| 216 | +Subgraph subgraph = new(subgraphId, subgraphLabel); |
| 217 | +Node subnode = Node.Create("sn", "Subnode"); |
| 218 | +subgraph.AddNode(subnode); |
| 219 | + |
| 220 | +// Create a subsubgraph with a node inside subgraph |
| 221 | +NodeIdentifier subsubgraphId = NodeIdentifier.FromString("ssg"); |
| 222 | +MermaidUnicodeText subsubgraphLabel = MermaidUnicodeText.FromString("Subsubgraph"); |
| 223 | +Subgraph subsubgraph = new(subsubgraphId, subsubgraphLabel); |
| 224 | +Node subsubnode = Node.Create("ssn", "Subsubnode"); |
| 225 | +subsubgraph.AddNode(subsubnode); |
| 226 | + |
| 227 | +// Add the subsubgraph to the subgraph |
| 228 | +subgraph.AddNode(subsubgraph); |
| 229 | + |
| 230 | +// Add the subgraph to the flowchart |
| 231 | +flowchart.AddNode(subgraph); |
| 232 | + |
| 233 | +// Create a link between the flowchart node and the subsubgraph |
| 234 | +Link nodeToSubSubGraph = new(node, subsubgraph, default); |
| 235 | +flowchart.AddLink(nodeToSubSubGraph); |
| 236 | + |
| 237 | +// Create a link between the flowchart node and the subnode |
| 238 | +Link nodeToSubnode = new(node, subnode, default); |
| 239 | +flowchart.AddLink(nodeToSubnode); |
| 240 | + |
| 241 | +// Generate the Mermaid output |
| 242 | +string mermaid = flowchart.ToMermaidString(); |
| 243 | +``` |
| 244 | + |
| 245 | +Will generate the following Mermaid output: |
| 246 | + |
| 247 | +```mermaid |
| 248 | +--- |
| 249 | +title: Using subgraphs |
| 250 | +--- |
| 251 | +flowchart TD |
| 252 | + n["Node"] |
| 253 | +
|
| 254 | + subgraph sg ["Subgraph"] |
| 255 | + sn["Subnode"] |
| 256 | +
|
| 257 | + subgraph ssg ["Subsubgraph"] |
| 258 | + ssn["Subsubnode"] |
| 259 | + end |
| 260 | + end |
| 261 | + subgraph ssg ["Subsubgraph"] |
| 262 | + ssn["Subsubnode"] |
| 263 | + end |
| 264 | +
|
| 265 | + n ---> ssg |
| 266 | + n ---> sn |
| 267 | +
|
| 268 | +``` |
0 commit comments