11.2 Draw a graph with Graphviz
You write a graph in the DOT language of Graphviz, with labels from your own data.
Problem
Section titled “Problem”You want a picture of a graph. Graphviz is a program that lays out graphs by itself and draws them as SVG, PNG or PDF. It reads a small text language called DOT and handles graphs larger than Mermaid does. You need this for:
- a picture of a graph for a document or a review;
- a look at the result of an analysis, to understand it;
- a graph too large for a Mermaid flowchart.
Example
Section titled “Example”The example graph dependencies models the dependencies between the eight packages of a small
program. It has an edge from each package to each package it uses. The array names holds the names
of the packages. The graph should be drawn from left to right, with the names of the packages on the
nodes.
Solution
Section titled “Solution”DotWriter writer = DotWriter.of(dependencies) .graphAttribute("rankdir", "LR") .nodeLabel(node -> names[node]);StringWriter out = new StringWriter(); // or a file, through a Writerwriter.write(out);String dot = out.toString();// digraph {// rankdir="LR";// 0 [label="app"];// ...// 0 -> 1;// ...// }let writer = DotWriter::of(&dependencies) .graph_attribute("rankdir", "LR") .node_label(|node| Some(names[node as usize].into()));let mut out = Vec::new(); // or a BufWriter on a File, through any io::Writewriter.write(&mut out)?;let dot = String::from_utf8(out)?;// digraph {// rankdir="LR";// 0 [label="app"];// ...// 0 -> 1;// ...// }const writer = DotWriter.of(dependencies) .graphAttribute("rankdir", "LR") .nodeLabel((node) => names[node]);const chunks: string[] = [];// or a file, through a WriteStreamconst out = { write: (text: string) => chunks.push(text) };writer.write(out);const dot = chunks.join("");// digraph {// rankdir="LR";// 0 [label="app"];// ...// 0 -> 1;// ...// }DotWriter writer = DotWriter.Of(dependencies) .GraphAttribute("rankdir", "LR") .NodeLabel(node => names[node]);using var output = new StringWriter(); // or a file, through a StreamWriterwriter.Write(output);string dot = output.ToString();// digraph {// rankdir="LR";// 0 [label="app"];// ...// 0 -> 1;// ...// }The writer writes the graph attributes, then every node with its label, then every edge. Nodes and edges follow the order of the node IDs and edge indices, so that the same graph always gives the same text. The result is this text:
digraph { rankdir="LR"; 0 [label="app"]; 1 [label="cli"]; 2 [label="ui"]; 3 [label="service"]; 4 [label="model"]; 5 [label="storage"]; 6 [label="json"]; 7 [label="util"]; 0 -> 1; 0 -> 2; 0 -> 3; 1 -> 7; 2 -> 3; 2 -> 4; 2 -> 7; 3 -> 4; 3 -> 5; 4 -> 5; 5 -> 4; 5 -> 6; 5 -> 7; 6 -> 7;}The command dot -Tsvg packages.dot -o packages.svg turns the text into a picture:
Good to know
Section titled “Good to know”- Other attributes of the whole graph, such as
fontname, go throughgraphAttributegraph_attributegraphAttributeGraphAttributeas well. - Labels are escaped for DOT. A label of
nullNonenullnullleaves a node unlabeled, and Graphviz then shows its node ID. - Which nodes are listed. Every edge is written. A node in no frame gets a line of its own only if it has a label or attributes, or if it has no edge in the text. Graphviz creates the other nodes from their edges.
- Into a file.
DotWriter.writeDotWriter::writeDotWriter.writeDotWriter.Writewrites to anyAppendableio::WriteTextSinkTextWriter, such as aWriterBufWriterWriteStreamStreamWriteron a file;DotWriter.writeToStringDotWriter::write_to_stringDotWriter.writeToStringDotWriter.WriteToStringreturns the text. - Styles and frames. See Highlight nodes, edges and groups in Graphviz.
API used
Section titled “API used”DotWriter.of(Graph)DotWriter::ofDotWriter.ofDotWriter.OfDotWriter.nodeLabel(IntFunction)DotWriter::node_labelDotWriter.nodeLabelDotWriter.NodeLabelDotWriter.edgeLabel(IntFunction)DotWriter::edge_labelDotWriter.edgeLabelDotWriter.EdgeLabelDotWriter.graphAttribute(String, String)DotWriter::graph_attributeDotWriter.graphAttributeDotWriter.GraphAttributeDotWriter.write(Appendable)DotWriter::writeDotWriter.writeDotWriter.WriteDotWriter.writeToString()DotWriter::write_to_stringDotWriter.writeToStringDotWriter.WriteToString