11.4 Draw a graph as a Mermaid flowchart
You write a graph as a Mermaid flowchart, with labels, a direction and node shapes.
Problem
Section titled “Problem”You want a picture of a graph in a place where people read Markdown. Mermaid draws flowcharts from a short text inside Markdown. Many viewers render it without any extra tool, GitHub among them. You need this for:
- a pull request or an issue that shows the graph a change affects;
- a wiki page or a README that documents how the modules of a system depend on each other;
- a report in Markdown with the result of an analysis.
Example
Section titled “Example”The example graph steps models the nine steps that build and publish a small program. It has an
edge a -> b if step b may start only after step a. The array names holds the names of the
steps. The graph should be drawn from left to right, with the names of the steps on the nodes. The
last step is publish. It should be drawn as a stadium, which is a box with round ends.
Solution
Section titled “Solution”String mermaid = MermaidWriter.of(steps) .direction(Direction.LEFT_RIGHT) .nodeLabel(step -> names[step]) .nodeShape(step -> names[step].equals("publish") ? Shape.STADIUM : null) .writeToString();// flowchart LR// n0["generate"]// ...// n8(["publish"])// n0 --> n1// ...let mermaid = MermaidWriter::of(&steps) .direction(Direction::LeftRight) .node_label(|step| Some(names[step as usize].into())) .node_shape(|step| (names[step as usize] == "publish").then_some(Shape::Stadium)) .write_to_string();// flowchart LR// n0["generate"]// ...// n8(["publish"])// n0 --> n1// ...const mermaid = MermaidWriter.of(steps) .direction(Direction.LEFT_RIGHT) .nodeLabel((step) => names[step]) .nodeShape((step) => (names[step] === "publish" ? Shape.STADIUM : null)) .writeToString();// flowchart LR// n0["generate"]// ...// n8(["publish"])// n0 --> n1// ...string mermaid = MermaidWriter.Of(steps) .Direction(MermaidDirection.LeftRight) .NodeLabel(step => names[step]) .NodeShape(step => names[step] == "publish" ? MermaidWriter.Shape.Stadium : null) .WriteToString();// flowchart LR// n0["generate"]// ...// n8(["publish"])// n0 --> n1// ...The writer names each node by its node ID after an n, such as n8, and shows the label in it.
Rendered, the text looks like this. The picture below is Graphity’s output, unchanged:
Good to know
Section titled “Good to know”- Into Markdown. Put the text in a code block marked
mermaid. - Into a file.
MermaidWriter.writeMermaidWriter::writeMermaidWriter.writeMermaidWriter.Writewrites the text to anyAppendableio::WriteTextSinkTextWriter, such as aWriterBufWriterWriteStreamStreamWriteron a file. - Labels are escaped for Mermaid. Characters that Mermaid would read as markup become entity
codes, so that a label
0: programis written as0#58; programand still shows as0: program. Depending on its version, Mermaid itself still changes a few labels, such as asterisks it reads as emphasis. The API documentation ofMermaidWriterMermaidWriterMermaidWriterMermaidWriterlists them. - Edge labels come from
MermaidWriter.edgeLabelMermaidWriter::edge_labelMermaidWriter.edgeLabelMermaidWriter.EdgeLabel, by edge index. - Size. Mermaid draws graphs of up to a few hundred nodes well. For larger ones, see Draw a graph with Graphviz.
- Colors and frames. See Highlight nodes, edges and groups in Mermaid.
API used
Section titled “API used”MermaidWriter.of(Graph)MermaidWriter::ofMermaidWriter.ofMermaidWriter.OfMermaidWriter.direction(MermaidWriter.Direction)MermaidWriter::directionMermaidWriter.directionMermaidWriter.DirectionMermaidWriter.nodeLabel(IntFunction)MermaidWriter::node_labelMermaidWriter.nodeLabelMermaidWriter.NodeLabelMermaidWriter.nodeShape(IntFunction)MermaidWriter::node_shapeMermaidWriter.nodeShapeMermaidWriter.NodeShapeMermaidWriter.edgeLabel(IntFunction)MermaidWriter::edge_labelMermaidWriter.edgeLabelMermaidWriter.EdgeLabelMermaidWriter.writeToString()MermaidWriter::write_to_stringMermaidWriter.writeToStringMermaidWriter.WriteToStringMermaidWriter.write(Appendable)MermaidWriter::writeMermaidWriter.writeMermaidWriter.Write