Flowcharts Syntax

Flowcharts - Basic 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.

Warning

If you are using the word β€œend” in a Flowchart node, capitalize the entire word or any of the letters (e.g., β€œEnd” or β€œEND”), or apply this workaround. Typing β€œend” in all lowercase letters will break the Flowchart.

A node (default)

id

Note

The id is what is displayed in the box.

Tip

Instead of flowchart one can also use graph.

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.

This is the text in the box

Unicode text

Use " to enclose the unicode text.

This ❀ Unicode

Markdown formatting

Use double quotes and backticks β€œ` text `” to enclose the markdown text.

This is MarkdownLine1Line 2Line 3

Direction

This statement declares the direction of the Flowchart.

This declares the flowchart is oriented from top to bottom (TD or TB).

Start
Stop

This declares the flowchart is oriented from left to right (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

This is the text in the box

A stadium-shaped node

This is the text in the box

A node in a subroutine shape

This is the text in the box

A node in a cylindrical shape

Database

A node in the form of a circle

This is the text in the circle

A node in an asymmetric shape

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)

This is the text in the box

A hexagon node

This is the text in the box

Parallelogram

This is the text in the box

Parallelogram alt

This is the text in the box

Trapezoid

Christmas

Trapezoid alt

Go shopping

Double circle

This is the text in the circle

Special characters that break syntax

It is possible to put text within quotes in order to render more troublesome characters. As in the example below:

This is the (text) in the box

Entity codes to escape characters

It is possible to escape characters using the syntax exemplified here.

A double quote:"
A dec char:β™₯

Numbers given are base 10, so # can be encoded as #35;. It is also supported to use HTML character names.

Subgraphs

subgraph title
    graph definition
end

An example below:

three
one
c2
c1
two
b2
b1
a2
a1

You can also set an explicit id for the subgraph.

one
a2
a1
c1

flowcharts

With the graphtype flowchart it is also possible to set edges to and from subgraphs as in the flowchart below.

three
one
c2
c1
two
b2
b1
a2
a1

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.

TOP
B1
f1
i1
B2
f2
i2
A
B

Markdown Strings

The β€œMarkdown Strings” feature enhances flowcharts and mind maps by offering a more versatile string type, which supports text formatting options such as bold and italics, and automatically wraps text within labels.

TwoBold edge labelThe dog in the hogThe catin the hatOneedge labelThe dog in the hogThe catin the hat

Formatting:

  • For bold text, use double asterisks (**) before and after the text.
  • For italics, use single asterisks (*) before and after the text.
  • With traditional strings, you needed to add <br> tags for text to wrap in nodes. However, markdown strings automatically wrap text when it becomes too long and allows you to start a new line by simply using a newline character instead of a <br> tag.

This feature is applicable to node labels, edge labels, and subgraph labels.

Interaction

It is possible to bind a click event to a node, the click can lead to either a javascript callback or to a link which will be opened in a new browser tab.

Note

This functionality is disabled when using securityLevel='strict' and enabled when using securityLevel='loose'.

click nodeId callback
click nodeId call callback()
  • nodeId is the id of the node
  • callback is the name of a javascript function defined on the page displaying the graph, the function will be called with the nodeId as parameter.

Examples of tooltip usage below:

<script>
  const callback = function () {
    alert('A callback was triggered');
  };
</script>

The tooltip text is surrounded in double quotes. The styles of the tooltip are set by the class .mermaidTooltip.

A
B
C
D

Success The tooltip functionality and the ability to link to urls are available from version 0.5.2.

?> Due to limitations with how Docsify handles JavaScript callback functions, an alternate working demo for the above code can be viewed at this jsfiddle.

Links are opened in the same browser tab/window by default. It is possible to change this by adding a link target to the click definition (_self, _blank, _parent and _top are supported):

A
B
C
D
E

Beginner’s tipβ€”a full example using interactive links in a html context:

<body>
  <pre class="mermaid">
    flowchart LR
        A-->B
        B-->C
        C-->D
        click A callback "Tooltip"
        click B "https://www.github.com" "This is a link"
        click C call callback() "Tooltip"
        click D href "https://www.github.com" "This is a link"
  </pre>

  <script>
    const callback = function () {
      alert('A callback was triggered');
    };
    const config = {
      startOnLoad: true,
      flowchart: { useMaxWidth: true, htmlLabels: true, curve: 'cardinal' },
      securityLevel: 'loose',
    };
    mermaid.initialize(config);
  </script>
</body>

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

text
text2
A
B
C

Styling and classes

Styling line curves

It is possible to style the type of curve used for lines between items, if the default method does not meet your needs. Available curve styles include basis, bumpX, bumpY, cardinal, catmullRom, linear, monotoneX, monotoneY, natural, step, stepAfter, and stepBefore.

In this example, a left-to-right graph uses the stepBefore curve style:

%%{ init: { 'flowchart': { 'curve': 'stepBefore' } } }%%
graph LR

For a full list of available curves, including an explanation of custom curves, refer to the Shapes documentation in the d3-shape project.

Styling a node

It is possible to apply specific styles such as a thicker border or a different background color to a node.

Start
Stop

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:

    classDef className fill:#f9f,stroke:#333,stroke-width:4px;

Also, it is possible to define style to multiple classes in one statement:

    classDef firstClassName,secondClassName font-size:12pt;

Attachment of a class to a node is done as per below:

    class nodeId1 className;

It is also possible to attach a class to a list of nodes in one statement:

    class nodeId1,nodeId2 className;

A shorter form of adding a class is to attach the classname to the node using the :::operator as per below:

A
B

Css classes

It is also possible to predefine classes in css styles that can be applied from the graph definition as in the example below:

Example style

<style>
  .cssClass > rect {
    fill: #ff0000;
    stroke: #ffff00;
    stroke-width: 4px;
  }
</style>

Example definition

A
AAABBB
D

Default class

If a class is named default it will be assigned to all classes without specific class definitions.

    classDef default fill:#f9f,stroke:#333,stroke-width:4px;

Basic support for fontawesome

It is possible to add icons from fontawesome.

The icons are accessed via the syntax fa:#icon class name#.

for peace
forbidden
A perhaps?

Mermaid is compatible with Font Awesome up to verion 5, Free icons only. Check that the icons you use are from the supported set of icons.

Configuration

Renderer

The layout of the diagram is done with the renderer. The default renderer is dagre.

Starting with Mermaid version 9.4, you can use an alternate renderer named elk. The elk renderer is better for larger and/or more complex diagrams.

The elk renderer is an experimenal feature. You can change the renderer to elk by adding this directive:

%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
Note

Note that the site needs to use mermaid version 9.4+ for this to work and have this featured enabled in the lazy-loading configuration.

Width

It is possible to adjust the width of the rendered flowchart.

This is done by defining mermaid.flowchartConfig or by the CLI to use a JSON file with the configuration. How to use the CLI is described in the mermaidCLI page. mermaid.flowchartConfig can be set to a JSON string with config parameters or the corresponding object.

mermaid.flowchartConfig = {
    width: 100%
}