Table of Contents

Class DotWriter

Namespace
Graphity.Dot
Assembly
Graphity.Dot.dll

A writer of DOT (the language of Graphviz) that labels and styles a graph with the caller's data by node ID and edge index.

public sealed class DotWriter
Inheritance
DotWriter
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 dot = DotWriter.Of(dependencies)
    .NodeLabel(node => names[node])
    .NodeAttributes((node, attributes) =>
    {
        if (sccs.ContainsCycle(sccs.SccOf(node)))
        {
            attributes["color"] = "red";
        }
    })
    .WriteToString();
// model and storage, the cycle, in red:
//   4 [label="model", color="red"];
//   5 [label="storage", color="red"];

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 and attributes 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. The text lists the graph attributes, the clusters in group order with their nodes, then the nodes in no cluster, then the edges in edge index order, so the same graph and hooks always give the same text. A node outside the clusters is listed only if it has a label or attributes, or no edge in the text; Graphviz creates the other nodes from their edges.

Labels and attribute values are written as quoted strings: the writer escapes quotes and backslashes and turns line breaks into DOT's \n. Graphviz decodes HTML entities such as < in the attributes label, xlabel, headlabel and taillabel. In these, the writer also turns & into &, so that a label shows exactly its text. Other values, such as a URL, keep their &. NUL, which DOT cannot hold, is written as U+FFFD.

Attribute names must be identifiers - an ASCII letter or underscore, then ASCII letters, digits or underscores - other than the DOT keywords node, edge, graph, digraph, subgraph and strict, in any letter case.

A writer keeps its configuration and can write any number of times. For Markdown and documentation, MermaidWriter in the package Graphity.Mermaid writes a Mermaid flowchart.

Instances are mutable and not safe to share between threads.

Methods

Clusters(NodePartition, Func<int, string?>)

Draws each group of partition as a cluster, with a label by group ID.

public DotWriter Clusters(NodePartition partition, Func<int, string?> label)

Parameters

partition NodePartition

The groups, over the nodes of the graph, such as an Sccs.

label Func<int, string>

Called with a group ID, returns the label of its cluster or null.

Returns

DotWriter

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 leaves a cluster unlabeled, also when the graph has a label, which Graphviz would otherwise repeat on every cluster.

Time: O(1).

Exceptions

ArgumentException

partition has a different node count than the graph.

EdgeAttributes(AttributeHook)

Sets the hook that adds DOT attributes, such as color or style, to each edge, by edge index.

public DotWriter EdgeAttributes(AttributeHook hook)

Parameters

hook AttributeHook

Called with each edge that is written, puts its attributes.

Returns

DotWriter

This writer.

Remarks

The hook receives the label from EdgeLabel(Func<int, string?>) as the attribute label, which it can replace or remove.

Time: O(1).

EdgeLabel(Func<int, string?>)

Sets the label of each edge, by edge index; null leaves an edge unlabeled.

public DotWriter EdgeLabel(Func<int, string?> label)

Parameters

label Func<int, string>

Called with an edge index, returns the label of the edge or null.

Returns

DotWriter

This writer.

Remarks

Time: O(1).

GraphAttribute(string, string)

Sets an attribute of the graph, such as rankdir to LR.

public DotWriter GraphAttribute(string name, string value)

Parameters

name string

The attribute name, an identifier other than a DOT keyword.

value string

The attribute value.

Returns

DotWriter

This writer.

Remarks

Time: O(1).

Exceptions

ArgumentException

name is not an identifier or is a DOT keyword.

NodeAttributes(AttributeHook)

Sets the hook that adds DOT attributes, such as shape, color or style, to each node, by node ID.

public DotWriter NodeAttributes(AttributeHook hook)

Parameters

hook AttributeHook

Called with each node that is written, puts its attributes.

Returns

DotWriter

This writer.

Remarks

The hook receives the label from NodeLabel(Func<int, string?>) as the attribute label, which it can replace or remove.

Time: O(1).

NodeFilter(Func<int, bool>)

Restricts the text to the nodes that include accepts and the edges between them.

public DotWriter NodeFilter(Func<int, bool> include)

Parameters

include Func<int, bool>

Called with a node ID, returns whether to write the node.

Returns

DotWriter

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 leaves a node unlabeled, and Graphviz then shows its node ID.

public DotWriter NodeLabel(Func<int, string?> label)

Parameters

label Func<int, string>

Called with a node ID, returns the label of the node or null.

Returns

DotWriter

This writer.

Remarks

Time: O(1).

Of(Graph)

Returns a writer for graph, with no labels, attributes, clusters or filter yet.

public static DotWriter Of(Graph graph)

Parameters

graph Graph

The graph to write.

Returns

DotWriter

The writer.

Remarks

Time: O(1).

Undirected()

Switches to the undirected reading of the graph: a graph with -- edges instead of a digraph.

public DotWriter Undirected()

Returns

DotWriter

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 DOT text.

public void Write(TextWriter output)

Parameters

output TextWriter

Where 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: NodeCount bytes while it runs.

Exceptions

IOException

Writing to output fails.

ArgumentException

An attribute hook puts a name that is not an identifier or is a DOT keyword, or a null value; what was written before stays in output.

WriteToString()

Returns the DOT text of the graph, as Write(TextWriter) writes it.

public string WriteToString()

Returns

string

The DOT text.

Remarks

Time: as for Write(TextWriter).
Memory: the text for the result; while it runs, up to twice as much again and NodeCount bytes more.

Exceptions

ArgumentException

As for Write(TextWriter).