用 Mermaid 在 Markdown 里画流程图/时序图/甘特图
不想为了一张流程图去开 Visio、draw.io?Mermaid 让你用几行文本就把图“写”出来,还能进 Git 版本库、跟着文档一起走。
用 Mermaid 在 Markdown 裡畫流程圖/時序圖/甘特圖
不想為了一張流程圖去開 Visio、draw.io?Mermaid 讓你用幾行文字就把圖「寫」出來,還能進 Git 版本庫、跟著文件一起走。
Draw flowcharts / sequence diagrams / Gantt charts in Markdown with Mermaid
Don't want to open Visio or draw.io just for one flowchart? Mermaid lets you "write" a diagram in a few lines of text, and it can go into Git and travel with your docs.
一、Mermaid 是什么
Mermaid 是一个用文本生成图表的工具。它借用了代码块的写法:在 Markdown 里放一个语言标注为 mermaid 的围栏块,里面写图形描述,支持的渲染器(GitHub、Typora、Obsidian、很多文档站)就会把它渲染成图。
```mermaid
graph TD
A[开始] --> B{条件?}
B -- 是 --> C[执行]
B -- 否 --> D[结束]
```
好处是:图是文本,能 diff、能版本管理、能随文档一起复制粘贴,改起来也比拖图形快得多。
二、流程图 flowchart
最常用的就是流程图。开头写 graph 加方向(TD 上下、LR 左右),节点用 [] 矩形、{} 菱形(判断)、() 圆角,箭头用 -->。
```mermaid
flowchart LR
Start(需求) --> Draft[写草稿]
Draft --> Review{评审}
Review -- 通过 --> Pub[发布]
Review -- 打回 --> Draft
```
常用节点形状:A[矩形]、B(圆角)、C{菱形}、D([ stadium ])。连线可加文字标签:A -- 标签 --> B。
三、时序图 sequenceDiagram
画“谁先说、谁后说”的交互特别合适,比如接口调用、用户登录流程:
```mermaid
sequenceDiagram
participant U as 用户
participant A as 应用
participant S as 服务器
U->>A: 点击登录
A->>S: 发送账号密码
S-->>A: 返回 token
A-->>U: 登录成功
```
实线 ->> 表示请求,虚线 -->> 表示返回/响应;participant 给角色起别名更清爽。
四、甘特图 gantt
做项目排期时,用甘特图看任务时间线:
```mermaid
gantt
title 网站上线计划
dateFormat YYYY-MM-DD
section 设计
原型 :a1, 2026-11-01, 5d
视觉稿 :a2, after a1, 4d
section 开发
前端 :b1, after a2, 10d
联调 :b2, after b1, 5d
```
after 某任务 表示依赖关系,d 是天、w 是周。排期写法也和任务管理相关,可结合 Markdown 任务清单 一起用。
五、类图与思维导图简述
Mermaid 还能画更多:
- 类图 classDiagram:写代码设计时描述类与关系。
- 思维导图 mindmap:用层级列表快速梳理思路(较新语法,渲染器支持度不一)。
- 状态图 stateDiagram:描述状态流转,如订单状态机。
- 饼图 pie:展示占比。
提示:不同平台对 Mermaid 版本支持不一,复杂图形发布前务必在目标平台预览;本地可用 365Lab 编辑器 配合支持 Mermaid 的预览确认语法。
六、常见问题
Q:为什么我的 Mermaid 没渲染成图?
A:三种常见原因——渲染器不支持 Mermaid(老版 Typora/部分博客不支持)、语法写错(缺括号或方向关键字)、或围栏块语言写成了 markdown 而非 mermaid。先在支持的平台试最小示例。
Q:箭头上的中文标签不显示?
A:标签要用 -- 文字 --> 写法,且部分老版本对中文支持差,升级渲染器或换成英文标签再本地化。
Q:和代码块冲突吗?
A:不冲突。Mermaid 就是“语言标注为 mermaid 的代码块”,普通代码块照常显示,详见 Markdown 代码块。
Q:能导出图片吗?
A:可以。很多平台支持把渲染出的图右键保存,或用 Mermaid CLI(mmdc)批量导出 PNG/SVG,适合放进文档或 PPT(参见 用 Markdown 做 PPT)。
想用图表写项目文档,先学 Markdown 写 README;公式类表达看 Markdown 数学公式;从 Markdown 入门 系统学起更稳。梳理概念层级可配合 用 Markdown 画思维导图。
一、Mermaid 是什麼
Mermaid 是一個用文字生成圖表的工具。它借用了程式碼區塊的寫法:在 Markdown 裡放一個語言標註為 mermaid 的圍欄塊,裡面寫圖形描述,支援的渲染器(GitHub、Typora、Obsidian、很多文件站)就會把它渲染成圖。
```mermaid
graph TD
A[开始] --> B{条件?}
B -- 是 --> C[执行]
B -- 否 --> D[结束]
```
好處是:圖是文字,能 diff、能版本管理、能隨文件一起複製貼上,改起來也比拖圖形快得多。
二、流程圖 flowchart
最常用的就是流程圖。開頭寫 graph 加方向(TD 上下、LR 左右),節點用 [] 矩形、{} 菱形(判斷)、() 圓角,箭頭用 -->。
```mermaid
flowchart LR
Start(需求) --> Draft[写草稿]
Draft --> Review{评审}
Review -- 通过 --> Pub[发布]
Review -- 打回 --> Draft
```
常用節點形狀:A[矩形]、B(圓角)、C{菱形}、D([ stadium ])。連線可加文字標籤:A -- 標籤 --> B。
三、時序圖 sequenceDiagram
畫「誰先說、誰後說」的互動特別合適,比如介面呼叫、使用者登入流程:
```mermaid
sequenceDiagram
participant U as 用户
participant A as 应用
participant S as 服务器
U->>A: 点击登录
A->>S: 发送账号密码
S-->>A: 返回 token
A-->>U: 登录成功
```
實線 ->> 表示請求,虛線 -->> 表示返回/響應;participant 給角色起別名更清爽。
四、甘特圖 gantt
做專案排期時,用甘特圖看任務時間線:
```mermaid
gantt
title 网站上线计划
dateFormat YYYY-MM-DD
section 设计
原型 :a1, 2026-11-01, 5d
视觉稿 :a2, after a1, 4d
section 开发
前端 :b1, after a2, 10d
联调 :b2, after b1, 5d
```
after 某任務 表示依賴關係,d 是天、w 是週。排期寫法也和任務管理相關,可結合 Markdown 任務清單 一起用。
五、類圖與思維導圖簡述
Mermaid 還能畫更多:
- 類圖 classDiagram:寫程式碼設計時描述類與關係。
- 思維導圖 mindmap:用層級清單快速梳理思路(較新語法,渲染器支援度不一)。
- 狀態圖 stateDiagram:描述狀態流轉,如訂單狀態機。
- 圓餅圖 pie:展示佔比。
提示:不同平台對 Mermaid 版本支援不一,複雜圖形發布前務必在目標平台預覽;本地可用 365Lab 編輯器 配合支援 Mermaid 的預覽確認語法。
六、常見問題
Q:為什麼我的 Mermaid 沒渲染成圖?
A:三種常見原因——渲染器不支援 Mermaid(老版 Typora/部分部落格不支援)、語法寫錯(缺括號或方向關鍵字)、或圍欄塊語言寫成了 markdown 而非 mermaid。先在支援的平台試最小範例。
Q:箭頭上的中文標籤不顯示?
A:標籤要用 -- 文字 --> 寫法,且部分老版本對中文支援差,升級渲染器或換成英文標籤再本地化。
Q:和程式碼區塊衝突嗎?
A:不衝突。Mermaid 就是「語言標註為 mermaid 的程式碼區塊」,普通程式碼區塊照常顯示,詳見 Markdown 程式碼區塊。
Q:能匯出圖片嗎?
A:可以。很多平台支援把渲染出的圖右鍵儲存,或用 Mermaid CLI(mmdc)批次匯出 PNG/SVG,適合放進文件或 PPT(參見 用 Markdown 做 PPT)。
想用圖表寫專案文件,先學 Markdown 寫 README;公式類表達看 Markdown 數學公式;從 Markdown 入門 系統學起更穩。梳理概念層級可配合 用 Markdown 畫思維導圖。
1. What Mermaid is
Mermaid is a tool that generates diagrams from text. It borrows the code-block syntax: put a fenced block labeled with language mermaid in your Markdown, write the diagram description inside, and a supported renderer (GitHub, Typora, Obsidian, many doc sites) turns it into a picture.
```mermaid
graph TD
A[开始] --> B{条件?}
B -- 是 --> C[执行]
B -- 否 --> D[结束]
```
The upside: a diagram is text, so it can be diffed, version-controlled, and copied alongside your docs—and editing it is far faster than dragging shapes around.
2. Flowcharts
The most common case is a flowchart. Start with graph plus a direction (TD top-down, LR left-right); nodes use [] rectangles, {} diamonds (decisions), () rounded; arrows use -->.
```mermaid
flowchart LR
Start(需求) --> Draft[写草稿]
Draft --> Review{评审}
Review -- 通过 --> Pub[发布]
Review -- 打回 --> Draft
```
Common node shapes: A[rectangle], B(rounded), C{diamond}, D([ stadium ]). Add a text label on a line: A -- label --> B.
3. Sequence diagrams
Great for "who speaks first, who speaks next" interactions—API calls, a login flow:
```mermaid
sequenceDiagram
participant U as 用户
participant A as 应用
participant S as 服务器
U->>A: 点击登录
A->>S: 发送账号密码
S-->>A: 返回 token
A-->>U: 登录成功
```
Solid ->> means a request; dashed -->> means a return/response; participant gives roles clean aliases.
4. Gantt charts
For project scheduling, a Gantt chart shows the task timeline:
```mermaid
gantt
title 网站上线计划
dateFormat YYYY-MM-DD
section 设计
原型 :a1, 2026-11-01, 5d
视觉稿 :a2, after a1, 4d
section 开发
前端 :b1, after a2, 10d
联调 :b2, after b1, 5d
```
after some task marks a dependency; d is days, w is weeks. Scheduling pairs well with task management—see Markdown task lists.
5. Class & mind maps briefly
Mermaid draws more:
- Class diagram classDiagram: describe classes and their relationships when designing code.
- Mind map mindmap: a hierarchical list quickly organizes thoughts (newer syntax, renderer support varies).
- State diagram stateDiagram: describe state transitions, e.g. an order state machine.
- Pie chart pie: show proportions.
Tip: Platforms differ in Mermaid version support—always preview complex diagrams on the target platform before publishing; locally you can use the 365Lab editor with a Mermaid-capable preview to confirm the syntax.
6. FAQ
Q: Why didn't my Mermaid render into a diagram?
A: Three common causes—the renderer doesn't support Mermaid (older Typora / some blogs), a syntax error (missing parentheses or direction keyword), or the fence language was set to markdown instead of mermaid. Try a minimal example on a supported platform first.
Q: The Chinese label on my arrow doesn't show?
A: Labels need the -- text --> syntax, and some older versions handle Chinese poorly—upgrade the renderer or use an English label and localize later.
Q: Does it conflict with code blocks?
A: No conflict. Mermaid is simply "a code block whose language is mermaid"; ordinary code blocks display as usual—see Markdown code blocks.
Q: Can I export images?
A: Yes. Many platforms let you right-click and save the rendered diagram, or use the Mermaid CLI (mmdc) to batch-export PNG/SVG—handy for docs or slides (see Markdown for PPTs).
To write project docs with diagrams, learn Markdown READMEs first; for formula-style expression see Markdown math formulas; starting from Markdown basics is the steadiest path. To organize concept hierarchies, pair it with mind maps in Markdown.