Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3.0 #32

Merged
merged 15 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019-2020 Valentin Fritz (aka. VFRZ)
Copyright (c) 2019-2023 Valentin Fritz (aka. VFRZ)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
102 changes: 54 additions & 48 deletions README.md
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
Binary file added Resources/icon_128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/icon_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions Sources/DotNetGraph.Tests/Attributes/DotAttributeTests.cs
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 Sources/DotNetGraph.Tests/Attributes/DotColorAttributeTests.cs
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 Sources/DotNetGraph.Tests/Attributes/DotDoubleAttributeTests.cs
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);
}
}
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 Sources/DotNetGraph.Tests/Attributes/DotEdgeStyleAttributeTests.cs
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");
}
}
Loading