Class MermaidWriter
A writer of Mermaid flowcharts that labels and styles a graph with the caller's data by node ID and edge index. Mermaid flowcharts render in Markdown.
public sealed class MermaidWriter
- Inheritance
-
MermaidWriter
- Inherited Members
Examples
Here, the packages of a small program get their names from names. The strongly connected
components sccs tell which packages are in a cycle, and these are drawn in red:
string mermaid = MermaidWriter.Of(dependencies)
.NodeLabel(node => names[node])
.ClassDef("cycle", "stroke:red")
.NodeClass(node => sccs.ContainsCycle(sccs.SccOf(node)) ? "cycle" : null)
.WriteToString();
// model and storage, the cycle, in red:
// classDef cycle stroke:red
// class n4 cycle
// class n5 cycle
Remarks
Of(Graph) returns a writer for a graph, the methods that configure it return the writer
again, and Write(TextWriter) or WriteToString() write the text. The graph holds no
data, so labels, shapes, classes and styles come from hooks that receive a node ID or an edge index
and read the caller's arrays. Nodes are named by their node IDs after an n, such as
n4. The text lists the class definitions, the clusters in group order with their nodes,
then the nodes in no cluster, then the edges in edge index order, the classes of the nodes and
the edge styles, so the same graph and hooks always give the same text. Mermaid numbers the edges
in the order they are written; the writer translates edge indices into these numbers for
linkStyle, also when a filter leaves edges out.
Labels are written as quoted strings: the writer turns quotes, #, %, &,
:, <, > and backticks into Mermaid entity codes and line breaks into
<br>, so that no label reads as HTML, as a Markdown string or as a directive. For the
same reason, whitespace right after the word direction becomes an entity code, and an empty
label, which Mermaid rejects, is written as one space.
Mermaid's renderer still changes some labels. Mermaid 11 applies Markdown emphasis and backslash
escapes to every label, so 2*3*4 loses its asterisks; Mermaid 12 shows a backslash before
n as a line break. Both read ¶ß and fl°, which Mermaid uses internally for
entity codes, as ; and &. Both also turn fa:fa-name into an icon in
a node label with a line break. With htmlLabels set to false, Mermaid shows some
entity codes as written, such as : for :.
Class names must be identifiers - an ASCII letter or underscore, then ASCII letters, digits or
underscores - other than a Mermaid keyword: style, linkStyle, interpolate,
classDef, class, href, click, call, graph,
flowchart, subgraph, end, _self, _blank, _parent and
_top. Keywords are case-sensitive, so End is a valid class name. Styles are written
as given and must not be blank or contain a line break or a semicolon. Whitespace in these rules is
that of the Java library, so that both write the same text. direction is followed by
whitespace if Java's Character.isWhitespace or isSpaceChar holds for the next
character, or if that character is U+FEFF. A style is blank if it is empty or holds only characters
for which Character.isWhitespace holds.
Mermaid renders graphs of a few hundred nodes well; for larger ones, DotWriter in the
package Graphity.Dot writes DOT (the language of Graphviz). A writer keeps its configuration
and can write any number of times.
Instances are mutable and not safe to share between threads.
Methods
ClassDef(string, string)
Defines a class for NodeClass(Func<int, string?>) to assign, written as classDef name style.
public MermaidWriter ClassDef(string name, string style)
Parameters
namestringThe class name, an identifier other than a Mermaid keyword.
stylestringThe style of the class, such as
fill:#f96,stroke:#333.
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
Exceptions
- ArgumentException
nameis not an identifier or is a Mermaid keyword, orstyleis blank or contains a line break or a semicolon.
Clusters(NodePartition, Func<int, string?>)
Draws each group of partition as a cluster (a Mermaid subgraph), with a
label by group ID.
public MermaidWriter Clusters(NodePartition partition, Func<int, string?> label)
Parameters
partitionNodePartitionThe groups, over the nodes of the graph, such as an Sccs.
labelFunc<int, string>Called with a group ID, returns the label of its cluster or null.
Returns
- MermaidWriter
This writer.
Remarks
Nodes in no group stay outside every cluster, and a group without a node to write has no cluster.
A label of null shows the name of the cluster: c and the group ID, such
as c2.
Time: O(1).
Exceptions
- ArgumentException
partitionhas a different node count than the graph.
Direction(MermaidDirection)
Sets the direction of the flowchart; without a call, it is TopDown.
public MermaidWriter Direction(MermaidDirection direction)
Parameters
directionMermaidDirectionThe direction.
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
directionis not a member of MermaidDirection.
EdgeLabel(Func<int, string?>)
Sets the label of each edge, by edge index; null leaves an edge unlabeled.
public MermaidWriter EdgeLabel(Func<int, string?> label)
Parameters
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
EdgeStyle(Func<int, string?>)
Sets the style of each edge, by edge index, written as a linkStyle statement;
null leaves an edge in the default style.
public MermaidWriter EdgeStyle(Func<int, string?> style)
Parameters
styleFunc<int, string>Called with an edge index, returns the style of the edge, such as
stroke:red,stroke-width:2px, or null.
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
NodeClass(Func<int, string?>)
Sets the class of each node, by node ID; null assigns none.
public MermaidWriter NodeClass(Func<int, string?> className)
Parameters
classNameFunc<int, string>Called with a node ID, returns the name of the class of the node, an identifier other than a Mermaid keyword, or null.
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
NodeFilter(Func<int, bool>)
Restricts the text to the nodes that include accepts and the edges between them.
public MermaidWriter NodeFilter(Func<int, bool> include)
Parameters
Returns
- MermaidWriter
This writer.
Remarks
Node IDs stay those of the graph, so the hooks keep reading the same arrays.
Time: O(1).
NodeLabel(Func<int, string?>)
Sets the label of each node, by node ID; null shows the name of the node, such as
n4.
public MermaidWriter NodeLabel(Func<int, string?> label)
Parameters
Returns
- MermaidWriter
This writer.
Remarks
Time: O(1).
NodeShape(Func<int, Shape?>)
Sets the shape of each node, by node ID; null draws a rectangle.
public MermaidWriter NodeShape(Func<int, MermaidWriter.Shape?> shape)
Parameters
shapeFunc<int, MermaidWriter.Shape?>Called with a node ID, returns the shape of the node or null.
Returns
- MermaidWriter
This writer.
Remarks
A value that is not a member of MermaidWriter.Shape makes Write(TextWriter) throw ArgumentOutOfRangeException.
Time: O(1).
Of(Graph)
Returns a writer for graph, top down, with no labels, shapes, classes, clusters
or filter yet.
public static MermaidWriter Of(Graph graph)
Parameters
graphGraphThe graph to write.
Returns
- MermaidWriter
The writer.
Remarks
Time: O(1).
Undirected()
Switches to the undirected reading of the graph: edges without arrowheads.
public MermaidWriter Undirected()
Returns
- MermaidWriter
This writer.
Remarks
The edges a -> b and b -> a become one, written as the edge from the lower node ID;
the edge hooks receive its edge index.
Time: O(1).
Write(TextWriter)
Writes the graph to output, as the text of a Mermaid flowchart.
public void Write(TextWriter output)
Parameters
outputTextWriterWhere the text goes; not closed.
Remarks
Numbers are written with the invariant culture, whatever the FormatProvider
of output.
Time: O(NodeCount + EdgeCount + groupCount), plus the hooks, with groupCount the number of
groups of the partition drawn as clusters; in the undirected reading, O(log OutDegree(target))
more per edge.
Memory: up to twice the size of the linkStyle lines while it runs, as they are
written last.
Exceptions
- IOException
Writing to
outputfails.- ArgumentException
A hook returns a class name that is not an identifier or is a Mermaid keyword, or an edge style that is blank or contains a line break or a semicolon; what was written before stays in
output.- ArgumentOutOfRangeException
The shape hook returns a value that is not a member of MermaidWriter.Shape.
WriteToString()
Returns the text of the Mermaid flowchart, as Write(TextWriter) writes it.
public string WriteToString()
Returns
- string
The Mermaid text.
Remarks
Time: as for Write(TextWriter).
Memory: the text for the result; while it runs, up to twice as much again, and the
linkStyle lines as for Write(TextWriter).