--- sort: 3 title: Flowchart --- # Flowcharts - Basic Syntax ## Graph This statement declares the direction of the Flowchart. This declares the graph is oriented from top to bottom (`TD` or `TB`). ```mermaid-example graph TD Start --> Stop ``` This declares the graph is oriented from left to right (`LR`). ```mermaid-example graph LR Start --> Stop ``` ## Flowchart Orientation 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 ## Flowcharts This renders a flowchart that allows for features such as: more arrow types, multi directional arrows, and linking to and from subgraphs. Apart from the graph type, the syntax is the same. This is currently experimental but when the beta period is over, both the graph and flowchart keywords will render in the new way. This means it is ok to start beta testing flowcharts. > **Important note** Do not type the word "end" as a Flowchart node. Capitalize all or any one the letters to keep the flowchart from breaking, i.e, "End" or "END". Or you can apply this [workaround](https://github.com/mermaid-js/mermaid/issues/1444#issuecomment-639528897).** ## Nodes & shapes ### A node (default) ```mermaid-example graph LR id ``` > **Note** The id is what is displayed in the box. ### 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. ```mermaid-example graph LR id1[This is the text in the box] ``` ## Node Shapes ### A node with round edges ```mermaid-example graph LR id1(This is the text in the box) ``` ### A stadium-shaped node ```mermaid-example graph LR id1([This is the text in the box]) ``` ### A node in a subroutine shape ```mermaid-example graph LR id1[[This is the text in the box]] ``` ### A node in a cylindrical shape ```mermaid-example graph LR id1[(Database)] ``` ### A node in the form of a circle ```mermaid-example graph LR id1((This is the text in the circle)) ``` ### A node in an asymmetric shape ```mermaid-example graph 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) ```mermaid-example graph LR id1{This is the text in the box} ``` ### A hexagon node ```mermaid-example graph LR id1{{This is the text in the box}} ``` ### Parallelogram ```mermaid-example graph TD id1[/This is the text in the box/] ``` ### Parallelogram alt ```mermaid-example graph TD id1[\This is the text in the box\] ``` ### Trapezoid ```mermaid-example graph TD A[/Christmas\] ``` ### Trapezoid alt ```mermaid-example graph TD B[\Go shopping/] ``` ## 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 ```mermaid-example graph LR A-->B ``` ### An open link ```mermaid-example graph LR A --- B ``` ### Text on links ```mermaid-example graph LR A-- This is the text! ---B ``` or ```mermaid-example graph LR A---|This is the text|B ``` ### A link with arrow head and text ```mermaid-example graph LR A-->|text|B ``` or ```mermaid-example graph LR A-- text -->B ``` ### Dotted link ```mermaid-example graph LR; A-.->B; ``` ### Dotted link with text ```mermaid-example graph LR A-. text .-> B ``` ### Thick link ```mermaid-example graph LR A ==> B ``` ### Thick link with text ```mermaid-example graph LR A == text ==> B ``` ### Chaining of links It is possible to declare many links in the same line as per below: ```mermaid-example graph LR A -- text --> B -- text2 --> C ``` It is also possible to declare multiple nodes links in the same line as per below: ```mermaid-example graph LR a --> b & c--> d ``` You can then describe dependencies in a very expressive way. Like the one-liner below: ```mermaid-example graph TB A & B--> C & D ``` If you describe the same diagram using the the basic syntax, it will take four lines. A word of warning, one could go overboard with this making the graph 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. ```mmd graph TB A --> C A --> D B --> C B --> D ``` ### Beta: New arrow types When using flowchart instead of graph there are new types of arrows supported as per below: ```mermaid-example flowchart LR A --o B B --x C ``` ### Beta: Multi directional arrows When using flowchart instead of graph there is the possibility to use multidirectional arrows. ```mermaid-example 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: ```mermaid-example graph TD A[Start] --> B{Is it?}; B -->|Yes| C[OK]; C --> D[Rethink]; D --> B; B ---->|No| E[End]; ``` > **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: ```mermaid-example graph 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 | `-.->` | `-..->` | `-...->` | ## 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: ```mermaid-example graph LR id1["This is the (text) in the box"] ``` ### Entity codes to escape characters It is possible to escape characters using the syntax exemplified here. ```mermaid-example graph LR A["A double quote:#quot;"] -->B["A dec char:#9829;"] ``` ## Subgraphs ``` subgraph title graph definition end ``` An example below: ```mermaid-example graph 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. ```mermaid-example graph TB c1-->a2 subgraph ide1 [one] a1-->a2 end ``` ## Beta: flowcharts With the graphtype flowcharts it is also possible to set edges to and from subgraphs as in the graph below. ```mermaid-example 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 ``` ## 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: ```html ``` The tooltip text is surrounded in double quotes. The styles of the tooltip are set by the class .mermaidTooltip. ```mermaid-example graph LR; A-->B; B-->C; C-->D; click A callback "Tooltip for a callback" click B "http://www.github.com" "This is a tooltip for a link" click A call callback() "Tooltip for a callback" click B href "http://www.github.com" "This is a tooltip for a link" ``` > **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](https://jsfiddle.net/s37cjoau/3/). 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): ```mermaid-example graph LR; A-->B; B-->C; C-->D; D-->E; click A "http://www.github.com" _blank click B "http://www.github.com" "Open this in a new tab" _blank click C href "http://www.github.com" _blank click D href "http://www.github.com" "Open this in a new tab" _blank ``` Beginners tip, a full example using interactive links in a html context: ```html