Getting started
editInstall the package
Add the Meziantou.Framework.Graphviz package to a project:
dotnet package add Meziantou.Framework.GraphvizThe types are in the Meziantou.Framework.Graphviz namespace.
Read a graph
GraphvizDocument.Parse reads a DOT text. Its Diagram is a FlowchartDiagram that you can change and draw:
var text = """
digraph Orders {
rankdir=LR;
node [shape=box, style=rounded];
start [label="Order received", shape=ellipse];
check [label="Payment valid?", shape=diamond];
subgraph cluster_fulfillment {
label="Fulfillment";
ship [label="Ship the order"];
notify [label="Notify the customer"];
}
cancel [label="Cancel the order", color=red];
start -> check;
check -> ship [label="yes"];
ship -> notify;
check -> cancel [label="no", style=dashed];
}
""";
var document = GraphvizDocument.Parse(text);
var flowchart = document.Diagram;
var svg = flowchart.ToSvg();The clusters (the subgraphs whose name starts with cluster) become the Subgraphs of the flowchart. The document also gives the Name of the graph, and whether it is directed and strict. Only the first graph of a text is read.
ToSvg draws the flowchart as SVG; see image rendering to draw it as PNG, WebP or JPEG, and themes for its colors. GraphvizParseOptions.DefaultTheme sets the theme of the diagrams a parser reads.
Errors
A text that is not valid DOT throws a GraphvizParseException, whose Error gives the line and the column of the problem. TryParse returns it instead:
var text = """
digraph {
a -> b
b -- c
}
""";
// TryParse returns the position of the first error instead of throwing a GraphvizParseException
Assert.False(GraphvizDocument.TryParse(text, options: null, out _, out var error));
Assert.Equal((3, 7), (error.Line, error.Column));GraphvizParseOptions limits the size of the text, the number of nodes and edges, and the depth of the nesting of the subgraphs. The edges between groups of nodes multiply: {a b c} -> {d e f} is nine edges, and each counts. The default attributes multiply too: each node, edge and subgraph gets a copy of them, and the copies count against MaxInheritedAttributes. Keep the limits when the text comes from users.
Warnings
The parts of DOT the flowcharts cannot draw are approximated or ignored, and reported in Warnings with their position: an attribute that is not supported, such as fontname or width, a port, an HTML-like label drawn as plain text, an arrowhead drawn as an arrow. Each kind of warning is reported once, at its first occurrence.
Write DOT
GraphvizWriter.Write writes a flowchart as DOT:
var flowchart = new FlowchartDiagram { Direction = DiagramDirection.LeftToRight };
var start = flowchart.AddNode("start", "Start", FlowchartNodeShape.Stadium);
var end = flowchart.AddNode("end_node", "End", FlowchartNodeShape.DoubleCircle);
flowchart.AddEdge(start, end, "done").LineStyle = FlowchartLineStyle.Dotted;
var dot = GraphvizWriter.Write(flowchart);The writer writes a digraph, and the subgraphs of the flowchart as clusters. A shape that Graphviz does not have is written as the closest shape of Graphviz, the styles of the classes are written on each element, and an edge to a subgraph is written as an edge to a node of the subgraph, clipped at its frame (lhead, ltail). Reading the text it writes gives a flowchart that it writes the same way.