Flowcharts Syntax
Flowcharts are composed of nodes (geometric shapes) and edges (arrows or lines). The Mermaid code defines how nodes and edges are made and accommodates different arrow types, multi-directional arrows, and any linking to and from subgraphs.
This page is a reference for the Mermaid syntax for flowcharts. For a more detailed explanation of the syntax, visit the official Mermaid documentation.
A node (default)
flowchart LR id
A node with text
It is also possible to set text in the box that differs from the id. If this is done several times, it is the last text found for the node that will be used. Also if you define edges for the node later on, you can omit text definitions. The one previously defined will be used when rendering the box.
flowchart LR id1[This is the text in the box]
Unicode text
Use "
to enclose the unicode text.
flowchart LR id["This ❤ Unicode"]
Markdown formatting
Use double quotes and backticks "` text `" to enclose the markdown text.
flowchart LR A -- text --> B -- text2 --> C
Direction
This statement declares the direction of the Flowchart.
This declares the flowchart is oriented from top to bottom (TD
or TB
).
flowchart LR Start --> Stop
This declares the flowchart is oriented from left to right (LR
).
flowchart LR Start --> Stop
Possible FlowChart orientations are:
- TB - Top to bottom
- TD - Top-down/ same as top to bottom
- BT - Bottom to top
- RL - Right to left
- LR - Left to right
Node shapes
A node with round edges
flowchart LR id1(This is the text in the box)
A stadium-shaped node
flowchart LR id1([This is the text in the box])
A node in a subroutine shape
flowchart LR id1[[This is the text in the box]]
A node in a cylindrical shape
flowchart LR id1[(Database)]
A node in the form of a circle
flowchart LR id1((This is the text in the circle))
flowchart LR id1>This is the text in the box
Currently only the shape above is possible and not its mirror. This might change with future releases.
A node (rhombus)
flowchart LR id1{This is the text in the box}
A hexagon node
flowchart LR id1{{This is the text in the box}}
Parallelogram
flowchart TD id1[/This is the text in the box/]
Parallelogram alt
flowchart TD id1[This is the text in the box]
Double circle
flowchart TD id1(((This is the text in the circle)))
Links between nodes
Nodes can be connected with links/edges. It is possible to have different types of links or attach a text string to a link.
A link with arrow head
flowchart LR A-->B
An open link
flowchart LR A --- B
Text on links
flowchart LR A-- This is the text! ---B
A link with arrow head and text
flowchart LR A-->|text|B
Dotted link
flowchart LR A-.->B;
Dotted link with text
flowchart LR A-. text .-> B
Thick link
flowchart LR A ==> B
Thick link with text
flowchart LR A == text ==> B
An invisible link
This can be a useful tool in some instances where you want to alter the default positioning of a node.
flowchart LR A ~~~ B
Chaining of links
It is possible declare many links in the same line as per below:
flowchart LR A -- text --> B -- text2 --> C
It is also possible to declare multiple nodes links in the same line as per below:
flowchart LR a --> b & c--> d
You can then describe dependencies in a very expressive way. Like the one-liner below:
flowchart TB A & B--> C & D
If you describe the same diagram using the basic syntax, it will take four lines. A
word of warning, one could go overboard with this making the flowchart harder to read in
markdown form. The Swedish word lagom
comes to mind. It means, not too much and not too little.
This goes for expressive syntaxes as well.
New arrow types
There are new types of arrows supported:
- circle edge
- cross edge
Circle edge example
flowchart LR A --o B
Cross edge example
flowchart LR A --x B
Multi directional arrows
There is the possibility to use multidirectional arrows.
flowchart LR A o--o B B <--> C C x--x D
Minimum length of a link
Each node in the flowchart is ultimately assigned to a rank in the rendered graph, i.e. to a vertical or horizontal level (depending on the flowchart orientation), based on the nodes to which it is linked. By default, links can span any number of ranks, but you can ask for any link to be longer than the others by adding extra dashes in the link definition.
In the following example, two extra dashes are added in the link from node B to node E, so that it spans two more ranks than regular links:
Note Links may still be made longer than the requested number of ranks by the rendering engine to accommodate other requests.
When the link label is written in the middle of the link, the extra dashes must be added on the right side of the link. The following example is equivalent to the previous one:
flowchart TD A[Start] --> B{Is it?} B -- Yes --> C[OK] C --> D[Rethink] D --> B B -- No ----> E[End]
For dotted or thick links, the characters to add are equals signs or dots, as summed up in the following table:
Length | 1 | 2 | 3 |
---|---|---|---|
Normal | --- | ---- | ----- |
Normal with arrow | --> | ---> | ----> |
Thick | === | ==== | ===== |
Thick with arrow | ==> | ===> | ====> |
Dotted | -.- | -..- | -...- |
Dotted with arrow | -.-> | -..-> | -...-> |
Subgraphs
An example below:
flowchart TB c1-->a2 subgraph one a1-->a2 end subgraph two b1-->b2 end subgraph three c1-->c2 end
You can also set an explicit id for the subgraph.
flowchart TB c1-->a2 subgraph ide1 [one] a1-->a2 end
flowcharts
With the graphtype flowchart it is also possible to set edges to and from subgraphs as in the flowchart below.
flowchart TB c1-->a2 subgraph one a1-->a2 end subgraph two b1-->b2 end subgraph three c1-->c2 end one --> two three --> two two --> c2
Direction in subgraphs
With the graphtype flowcharts you can use the direction statement to set the direction which the subgraph will render like in this example.
flowchart LR subgraph TOP direction TB subgraph B1 direction RL i1 -->f1 end subgraph B2 direction BT i2 -->f2 end end A --> TOP --> B B1 --> B2
Limitation
If any of a subgraph's nodes are linked to the outside, subgraph direction will be ignored. Instead the subgraph will inherit the direction of the parent graph:
flowchart LR subgraph subgraph1 direction TB top1[top] --> bottom1[bottom] end subgraph subgraph2 direction TB top2[top] --> bottom2[bottom] end %% ^ These subgraphs are identical, except for the links to them: %% Link *to* subgraph1: subgraph1 direction is maintained outside --> subgraph1 %% Link *within* subgraph2: %% subgraph2 inherits the direction of the top-level graph (LR) outside ---> top2
Comments
Comments can be entered within a flow diagram, which will be ignored by the parser. Comments need to be on their own line, and must be prefaced with %%
(double percent signs). Any text after the start of the comment to the next newline will be treated as a comment, including any flow syntax
flowchart LR %% this is a comment A -- text --> B{node} A -- text --> B -- text2 --> C
Styling and classes
Styling a node
It is possible to apply specific styles such as a thicker border or a different background color to a node.
flowchart LR id1(Start)-->id2(Stop) style id1 fill:#f9f,stroke:#333,stroke-width:4px style id2 fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
Classes
More convenient than defining the style every time is to define a class of styles and attach this class to the nodes that should have a different look.
A class definition looks like the example below:
Also, it is possible to define style to multiple classes in one statement:
Attachment of a class to a node is done as per below:
It is also possible to attach a class to a list of nodes in one statement:
A shorter form of adding a class is to attach the classname to the node using the :::
operator as per below:
flowchart LR A:::someclass --> B classDef someclass fill:#f96
This form can be used when declaring multiple links between nodes:
flowchart LR A:::foo & B:::bar --> C:::foobar classDef foo stroke:#f00 classDef bar stroke:#0f0 classDef foobar stroke:#00f