https://github.com/vfrz/dotnetgraph
Create GraphViz DOT graph with .NET / C#
https://github.com/vfrz/dotnetgraph
csharp dot dotnet dotnetcore graph graphviz graphviz-dot graphviz-dot-language netcore netstandard netstandard20
Last synced: 6 months ago
JSON representation
Create GraphViz DOT graph with .NET / C#
- Host: GitHub
- URL: https://github.com/vfrz/dotnetgraph
- Owner: vfrz
- License: mit
- Created: 2017-10-08T09:37:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-13T09:35:36.000Z (about 1 year ago)
- Last Synced: 2025-04-05T06:07:42.892Z (6 months ago)
- Topics: csharp, dot, dotnet, dotnetcore, graph, graphviz, graphviz-dot, graphviz-dot-language, netcore, netstandard, netstandard20
- Language: C#
- Homepage:
- Size: 146 KB
- Stars: 99
- Watchers: 8
- Forks: 21
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# DotNetGraph

Create **GraphViz DOT graph** with **dotnet** (compatible with **.NET Standard 2.0** and higher).
Current stable version: [](https://www.nuget.org/packages/DotNetGraph/)
Latest version: [](https://www.nuget.org/packages/DotNetGraph/absoluteLatest)
# Usage
## Create a graph (*DotGraph*)
```csharp
var graph = new DotGraph().WithIdentifier("MyGraph");var directedGraph = new DotGraph().WithIdentifier("MyDirectedGraph").Directed();
```## Create and add a node (*DotNode*)
```csharp
var myNode = new DotNode()
.WithIdentifier("MyNode")
.WithShape(DotNodeShape.Ellipse)
.WithLabel("My node!")
.WithFillColor(DotColor.Coral)
.WithFontColor(DotColor.Black)
.WithStyle(DotNodeStyle.Dotted)
.WithWidth(0.5)
.WithHeight(0.5)
.WithPenWidth(1.5);// Add the node to the graph
graph.Add(myNode);
```## Create and add an edge (*DotEdge*)
```csharp
// Create an edge with identifiers
var myEdge = new DotEdge().From("Node1").To("Node2");// Or with nodes and attributes
var myEdge = new DotEdge()
.From(node1)
.To(node2)
.WithArrowHead(DotEdgeArrowType.Box)
.WithArrowTail(DotEdgeArrowType.Diamond)
.WithColor(DotColor.Red)
.WithFontColor(DotColor.Black)
.WithLabel("My edge!")
.WithStyle(DotEdgeStyle.Dashed)
.WithPenWidth(1.5);// Add the edge to the graph
graph.Add(myEdge);
```## Create a subgraph / cluster
```csharp
// Subgraph identifier need to start with "cluster" to be identified as a cluster
var mySubgraph = new DotSubgraph().WithIdentifier("cluster_0");// Create a subgraph with attributes (only used for cluster)
var mySubgraph2 = new DotSubgraph()
.WithIdentifier("cluster_1")
.WithColor(DotColor.Red)
.WithStyle(DotSubGraphStyle.Dashed)
.WithLabel("My subgraph!");// Add node, edge, subgraph
subGraph.Add(myNode);
subGraph.Add(myEdge);
subGraph.Add(mySubgraph2);// Add subgraph to main graph
graph.Add(mySubgraph);
```## Compile to DOT format
```csharp
await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
await graph.CompileAsync(context);var result = writer.GetStringBuilder().ToString();
// Save it to a file
File.WriteAllText("graph.dot", result);
```
### Credits
Logo: https://www.flaticon.com/free-icon/flow-chart_4411911