-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
100 changed files
with
2,841 additions
and
2,602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,99 @@ | ||
# DotNetGraph | ||
|
||
Create **GraphViz DOT graph** with **.NET** | ||
![Logo](Resources/icon_64.png) | ||
|
||
Create **GraphViz DOT graph** with **dotnet**. | ||
|
||
Available on NuGet: [![#](https://img.shields.io/nuget/v/DotNetGraph.svg)](https://www.nuget.org/packages/DotNetGraph/) | ||
|
||
Compatible with **.NET Standard 2.0** and higher | ||
Compatible with **.NET Standard 2.0** and higher. | ||
|
||
# Documentation | ||
# Usage | ||
|
||
## Create a graph (*DotGraph*) | ||
|
||
```csharp | ||
var graph = new DotGraph("MyGraph"); | ||
var graph = new DotGraph().WithIdentifier("MyGraph"); | ||
|
||
var directedGraph = new DotGraph("MyDirectedGraph", true); | ||
var directedGraph = new DotGraph().WithIdentifier("MyDirectedGraph").Directed(); | ||
``` | ||
|
||
## Create and add a node (*DotNode*) | ||
|
||
```csharp | ||
var myNode = new DotNode("MyNode") | ||
{ | ||
Shape = DotNodeShape.Ellipse, | ||
Label = "My node!", | ||
FillColor = Color.Coral, | ||
FontColor = Color.Black, | ||
Style = DotNodeStyle.Dotted, | ||
Width = 0.5f, | ||
Height = 0.5f, | ||
PenWidth = 1.5f | ||
}; | ||
var myNode = new DotNode() | ||
.WithIdentifier("MyNode") | ||
.WithShape(DotNodeShape.Ellipse) | ||
.WithLabel("My node!") | ||
.WithFillColor(Color.Coral) | ||
.WithFontColor(Color.Black) | ||
.WithStyle(DotNodeStyle.Dotted) | ||
.WithWidth(0.5) | ||
.WithHeight(0.5) | ||
.WithPenWidth(1.5); | ||
|
||
// Add the node to the graph | ||
graph.Elements.Add(myNode); | ||
graph.Add(myNode); | ||
``` | ||
|
||
## Create and add an edge (*DotEdge*) | ||
|
||
```csharp | ||
// Create an edge with identifiers | ||
var myEdge = new DotEdge("myNode1", "myNode2"); | ||
|
||
// Create an edge with nodes and attributes | ||
var myEdge = new DotEdge(myNode1, myNode2) | ||
{ | ||
ArrowHead = DotEdgeArrowType.Box, | ||
ArrowTail = DotEdgeArrowType.Diamond, | ||
Color = Color.Red, | ||
FontColor = Color.Black, | ||
Label = "My edge!", | ||
Style = DotEdgeStyle.Dashed, | ||
PenWidth = 1.5f | ||
}; | ||
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(Color.Red) | ||
.WithFontColor(Color.Black) | ||
.WithLabel("My edge!") | ||
.WithStyle(DotEdgeStyle.Dashed) | ||
.WithPenWidth(1.5); | ||
|
||
// Add the edge to the graph | ||
graph.Elements.Add(myEdge); | ||
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("cluster_0"); | ||
var mySubGraph = new DotSubGraph().WithIdentifier("cluster_0"); | ||
|
||
// Create a subgraph with attributes (only used for cluster) | ||
var mySubGraph = new DotSubGraph("cluster_0") | ||
{ | ||
Color = Color.Red, | ||
Style = DotSubGraphStyle.Dashed, | ||
Label = "My subgraph!" | ||
}; | ||
var mySubGraph = new DotSubGraph() | ||
.WithIdentifier("cluster_0") | ||
.WithColor(Color.Red) | ||
.WithStyle(DotSubGraphStyle.Dashed) | ||
.WithLabel("My subgraph!"); | ||
|
||
// Add node, edge, subgraph | ||
subGraph.Elements.Add(myNode); | ||
subGraph.Elements.Add(myEdge); | ||
subGraph.Elements.Add(mySubGraph2); | ||
subGraph.Add(myNode); | ||
subGraph.Add(myEdge); | ||
subGraph.Add(mySubGraph2); | ||
|
||
// Add subgraph to main graph | ||
graph.Elements.Add(mySubGraph); | ||
graph.Add(mySubGraph); | ||
``` | ||
|
||
## Compile to DOT format | ||
|
||
```csharp | ||
// Non indented version | ||
var dot = graph.Compile(); | ||
// Indented version | ||
var dot = graph.Compile(true); | ||
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("myFile.dot", dot); | ||
``` | ||
File.WriteAllText("graph.dot", result); | ||
``` | ||
<hr> | ||
|
||
### Credits | ||
|
||
Logo: https://www.flaticon.com/free-icon/flow-chart_4411911 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNetGraph.Attributes; | ||
using DotNetGraph.Compilation; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotNetGraph.Tests.Attributes; | ||
|
||
[TestClass] | ||
public class DotAttributeTests | ||
{ | ||
[TestMethod] | ||
public async Task Compile() | ||
{ | ||
var attribute = new DotAttribute("testing"); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("testing"); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Sources/DotNetGraph.Tests/Attributes/DotColorAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNetGraph.Attributes; | ||
using DotNetGraph.Compilation; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotNetGraph.Tests.Attributes; | ||
|
||
[TestClass] | ||
public class DotColorAttributeTests | ||
{ | ||
[TestMethod] | ||
public async Task CompileFromString() | ||
{ | ||
var attribute = new DotColorAttribute("red"); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"red\""); | ||
} | ||
|
||
[TestMethod] | ||
public async Task CompileFromColor() | ||
{ | ||
var attribute = new DotColorAttribute(Color.Red); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"#FF0000\""); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromColor() | ||
{ | ||
DotColorAttribute attribute = Color.Red; | ||
attribute.Value.Should().Be("#FF0000"); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromString() | ||
{ | ||
DotColorAttribute attribute = "#FF0000"; | ||
attribute.Value.Should().Be("#FF0000"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Sources/DotNetGraph.Tests/Attributes/DotDoubleAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNetGraph.Attributes; | ||
using DotNetGraph.Compilation; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotNetGraph.Tests.Attributes; | ||
|
||
[TestClass] | ||
public class DotDoubleAttributeTests | ||
{ | ||
[TestMethod] | ||
public async Task CompileWithDefaultFormat() | ||
{ | ||
var attribute = new DotDoubleAttribute(123.456); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("123.46"); | ||
} | ||
|
||
[TestMethod] | ||
public async Task CompileWithSpecifiedFormat() | ||
{ | ||
var attribute = new DotDoubleAttribute(123.456, "F3"); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("123.456"); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromDouble() | ||
{ | ||
DotDoubleAttribute attribute = 123.456d; | ||
attribute.Value.Should().Be(123.456d); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Sources/DotNetGraph.Tests/Attributes/DotEdgeArrowTypeAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNetGraph.Attributes; | ||
using DotNetGraph.Compilation; | ||
using DotNetGraph.Core; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotNetGraph.Tests.Attributes; | ||
|
||
[TestClass] | ||
public class DotEdgeArrowTypeAttributeTests | ||
{ | ||
[TestMethod] | ||
public async Task CompileFromString() | ||
{ | ||
var attribute = new DotEdgeArrowTypeAttribute("custom"); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"custom\""); | ||
} | ||
|
||
[TestMethod] | ||
public async Task CompileFromEnum() | ||
{ | ||
var attribute = new DotEdgeArrowTypeAttribute(DotEdgeArrowType.Box); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"box\""); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromDotEdgeArrowType() | ||
{ | ||
DotEdgeArrowTypeAttribute attribute = DotEdgeArrowType.Box; | ||
attribute.Value.Should().Be("box"); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromString() | ||
{ | ||
DotEdgeArrowTypeAttribute attribute = "box"; | ||
attribute.Value.Should().Be("box"); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Sources/DotNetGraph.Tests/Attributes/DotEdgeStyleAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using DotNetGraph.Attributes; | ||
using DotNetGraph.Compilation; | ||
using DotNetGraph.Core; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotNetGraph.Tests.Attributes; | ||
|
||
[TestClass] | ||
public class DotEdgeStyleAttributeTests | ||
{ | ||
[TestMethod] | ||
public async Task CompileFromString() | ||
{ | ||
var attribute = new DotEdgeStyleAttribute("custom"); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"custom\""); | ||
} | ||
|
||
[TestMethod] | ||
public async Task CompileFromEnum() | ||
{ | ||
var attribute = new DotEdgeStyleAttribute(DotEdgeStyle.Solid); | ||
|
||
await using var writer = new StringWriter(); | ||
var context = new CompilationContext(writer, new CompilationOptions()); | ||
await attribute.CompileAsync(context); | ||
|
||
var result = writer.GetStringBuilder().ToString(); | ||
result.Should().Be("\"solid\""); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromDotEdgeStyle() | ||
{ | ||
DotEdgeStyleAttribute attribute = DotEdgeStyle.Solid; | ||
attribute.Value.Should().Be("solid"); | ||
} | ||
|
||
[TestMethod] | ||
public void ImplicitConversionFromString() | ||
{ | ||
DotEdgeStyleAttribute attribute = "solid"; | ||
attribute.Value.Should().Be("solid"); | ||
} | ||
} |
Oops, something went wrong.