Skip to content

11.4 Draw a graph as a Mermaid flowchart

You write a graph as a Mermaid flowchart, with labels, a direction and node shapes.

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.

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.

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
// ...
Full example
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
// ...
Full example
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
// ...
Full example
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
// ...
Full example

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:

generatecompilelintcompileTestsdocstestpackagesignpublish