← Back to Editor All Tutorials
中文

How to Draw Flowcharts in Mermaid

Flowcharts visualize process steps and decision paths, making them ideal for user flows, approval processes, or algorithm descriptions. Mermaid uses either graph or flowchart keywords for flow diagrams.

Why Use Flowcharts?

Flowcharts excel at clearly expressing process steps and decision paths. Compared to plain text descriptions, flowcharts can:

Ideal Use Cases:

Practical Use Cases

User Flow Example

User login flow is the most common flowchart application, clearly showing validation, error handling, and success redirect branches:

graph TD
    A([Start]) --> B[User enters credentials]
    B --> C{Account exists?}
    C -->|No| D[Show error]
    D --> B
    C -->|Yes| E{Password valid?}
    E -->|No| F[Increment fail count]
    F --> G{Fails ≥ 3?}
    G -->|Yes| H[Lock account 30 min]
    G -->|No| B
    E -->|Yes| I[Generate Session Token]
    I --> J[Redirect to dashboard]
    J --> K([End])

在 MermZen 中试试 →

Business Approval Example

Leave approval flow demonstrating multi-level approval logic:

graph TD
    A([Start]) --> B[Submit leave request]
    B --> C{Days ≤ 3?}
    C -->|Yes| D[Direct supervisor approves]
    C -->|No| E[Department manager approves]
    D --> F{Approved?}
    E --> F
    F -->|Yes| G[Update attendance system]
    F -->|No| H[Notify rejection]
    G --> I([End])
    H --> I

在 MermZen 中试试 →

Algorithm Flow Example

Binary search algorithm flowchart:

graph TD
    A([Start]) --> B[Initialize left=0, right=n-1]
    B --> C{left ≤ right?}
    C -->|No| D[Return -1 not found]
    C -->|Yes| E[Calculate mid = left + right / 2]
    E --> F{arr[mid] == target?}
    F -->|Yes| G[Return mid]
    F -->|No| H{arr[mid] < target?}
    H -->|Yes| I[left = mid + 1]
    H -->|No| J[right = mid - 1]
    I --> C
    J --> C
    D --> K([End])
    G --> K

在 MermZen 中试试 →

Declaring a Chart

graph TD

在 MermZen 中试试 →

Direction parameters:

Parameter Meaning
TD / TB Top to bottom
LR Left to right
BT Bottom to top
RL Right to left

Node Shapes

graph TD
    A[Rectangle]
    B(Rounded rect)
    C{Diamond}
    D((Circle))
    E([Stadium / pill])
    F[[Subroutine]]
    G[(Database)]

在 MermZen 中试试 →

Syntax Shape Usage
A[text] Rectangle Step / action
A(text) Rounded rectangle Subprocess
A{text} Diamond Decision / condition
A((text)) Circle Connector / junction
A([text]) Stadium Start / end

Node Shape Selection Guide

Choosing the right node shape makes flowcharts more readable:

Shape Syntax Best For
Rectangle [text] Regular steps, actions, processes Most common, represents operations
Diamond {text} Decision conditions, branching Must have at least two exits (yes/no)
Circle ((text)) Connectors, junction points For cross-page links or flow merging
Stadium ([text]) Start/end nodes Beginning and end of flow

Selection Tips:

Connector Types

graph LR
    A --> B
    A --- C
    A -.-> D
    A ==> E
    A --label--> F
    A -->|label| G

在 MermZen 中试试 →

Syntax Meaning
A --> B Solid arrow
A --- B Solid line, no arrow
A -.-> B Dashed arrow
A ==> B Bold arrow (emphasis)
A --text--> B Labeled arrow

Subgraphs

graph TD
    subgraph Frontend
        A[UI Layer] --> B[Validation]
    end
    subgraph Backend
        C[API] --> D[Database]
    end
    B --> C

Best Practices

1. Limit Decision Nodes Per Layer

Keep decision nodes to no more than 2 per layer to avoid overly complex flowcharts:

graph TD
    A[Start] --> B{Condition 1?}
    B -->|Yes| C{Condition 2?}
    B -->|No| D[Process A]
    C -->|Yes| E[Process B]
    C -->|No| F[Process C]

If more decisions are needed, consider splitting into multiple sub-flows or using subgraphs.

2. Always Label Branch Exits

Every decision node branch should have a clear label:

graph LR
    A{Login success?} -->|Yes| B[Go to home]
    A -->|No| C[Show error]

Recommended labels: Yes/No, Success/Fail, Approved/Rejected, Valid/Invalid

3. Use Subgraphs to Group Related Nodes

Group related nodes with subgraph for better readability:

graph TD
    subgraph Client
        A[User Request] --> B[Authentication]
    end
    subgraph Server
        C[API Gateway] --> D[Business Logic]
        D --> E[Database]
    end
    B --> C

在 MermZen 中试试 →

4. Maintain Consistent Flow Direction

Avoid mixing multiple directions in the same diagram.

Comparison with Other Diagrams

Feature Flowchart Sequence Diagram State Diagram
Core Focus Process steps + decisions Object interaction order State transitions
Best For Business flows, algorithms API calls, system interactions State machines, lifecycles
Decision Branches ✅ Primary focus ❌ Not emphasized ✅ Conditional transitions
Time Sequence ⚠️ Optional ✅ Core element ⚠️ Implicit
Participants ⚠️ Optional ✅ Required ❌ Single object

Selection Guide:

Full Example: User Login Flow

graph TD
    A([Start]) --> B[User enters credentials]
    B --> C{Account exists?}
    C -->|No| D[Show error]
    D --> B
    C -->|Yes| E{Password valid?}
    E -->|No| F[Increment fail count]
    F --> G{Fails ≥ 3?}
    G -->|Yes| H[Lock account 30 min]
    G -->|No| B
    E -->|Yes| I[Generate Session Token]
    I --> J[Redirect to dashboard]
    J --> K([End])

在 MermZen 中试试 →


To try the above code in MermZen, click Open Editor and paste the code there.