Skip to content
Draft
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
14 changes: 14 additions & 0 deletions Sources/DotNetGraph.Tests/BasicGraphTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using DotNetGraph.Core;
using DotNetGraph.Extensions;
using DotNetGraph.Node;
using NFluent;
Expand Down Expand Up @@ -64,5 +65,18 @@ public void GraphWithoutStringsFormat()

Check.That(compiled).HasSameValueAs("digraph \"TestGraph\" { \"TestNode\"[label=\"\\lTesting\"]; }");
}

[Fact]
public void GraphWithLayout()
{
var graph = new DotGraph("TestGraph", true)
{
Layout = DotGraphLayout.Neato
};

var compiled = graph.Compile(false, false);

Check.That(compiled).HasSameValueAs("digraph \"TestGraph\" { layout=neato; }");
}
}
}
19 changes: 19 additions & 0 deletions Sources/DotNetGraph/Attributes/DotGraphLayoutAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using DotNetGraph.Core;

namespace DotNetGraph.Attributes
{
public class DotGraphLayoutAttribute : IDotAttribute
{
public DotGraphLayout Layout { get; set; }

public DotGraphLayoutAttribute(DotGraphLayout layout = DotGraphLayout.Dot)
{
Layout = layout;
}

public static implicit operator DotGraphLayoutAttribute(DotGraphLayout? layout)
{
return layout.HasValue ? new DotGraphLayoutAttribute(layout.Value) : null;
}
}
}
20 changes: 20 additions & 0 deletions Sources/DotNetGraph/Compiler/DotCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private void CompileGraph(StringBuilder builder, bool indented, bool formatStrin

indentationLevel++;

CompileGraphAttributes(builder, _graph.Attributes, formatStrings);

foreach (var element in _graph.Elements)
{
if (element is DotEdge edge)
Expand Down Expand Up @@ -256,6 +258,24 @@ private void CompileAttributes(StringBuilder builder, ReadOnlyCollection<IDotAtt
builder.Append("]");
}

private void CompileGraphAttributes(StringBuilder builder, ReadOnlyCollection<IDotAttribute> attributes, bool formatStrings)
{
if (attributes.Count == 0)
return;

foreach (var attribute in attributes)
{
if (attribute is DotGraphLayoutAttribute graphLayoutAttribute)
{
builder.Append($"layout={graphLayoutAttribute.Layout.ToString().ToLowerInvariant()}; ");
}
else
{
throw new DotException($"Attribute type not supported: {attribute.GetType()}");
}
}
}

internal static string FormatString(string value, bool format)
{
if (!format)
Expand Down
12 changes: 12 additions & 0 deletions Sources/DotNetGraph/Core/DotGraphLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace DotNetGraph.Core
{
public enum DotGraphLayout
{
Dot = 0,
Neato = 1,
Fdp = 2,
Sfdp = 3,
Twopi = 4,
Circo= 5
}
}
9 changes: 8 additions & 1 deletion Sources/DotNetGraph/DotGraph.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System.Collections.Generic;
using DotNetGraph.Attributes;
using DotNetGraph.Core;

namespace DotNetGraph
{
public class DotGraph : IDotElement
public class DotGraph : DotElementWithAttributes
{
public bool Directed { get; set; }

public string Identifier { get; set; }

public bool Strict { get; set; }

public DotGraphLayoutAttribute Layout
{
get => GetAttribute<DotGraphLayoutAttribute>();
set => SetAttribute(value);
}

public List<IDotElement> Elements { get; }

public DotGraph()
Expand Down