```mermaid flowchart TD A[Christmas] -->|Get money| B(Go shopping) B --> C{Let me think} C -->|One| D[Laptop] C -->|Two| E[iPhone] C -->|Three| F[fa:fa-car Car] ```
Mermaid 语法
顺序图 (Sequence Diagram)
顺序图是一种交互图,它显示了流程如何相互操作以及以何种顺序操作。
Mermaid 可以渲染顺序图。
代码:
1 2 3 4
sequenceDiagram Alice->>John: Hello John, how are you? John-->>Alice: Great! Alice-)John: See you later!
sequenceDiagram participant Alice participant Bob Alice->>Bob: Hi Bob Bob->>Alice: Hi Alice
Actors
如果你想使用 actor 图标来代替矩形框,可以使用 actor 关键字来代替 participant 关键字。
1 2 3 4 5
sequenceDiagram actor Alice actor Bob Alice->>Bob: Hi Bob Bob->>Alice: Hi Alice
别名
每个参与者可以有一个标识符和一个别名。
1 2 3 4 5
sequenceDiagram participant A as Alice participant J as John A->>J: Hello John, how are you? J->>A: Great!
分组
参与者可以分组。你可以通过以下方式定义一个颜色(默认为透明):
1 2 3 4 5 6 7 8 9
box Aqua Group Description ... actors ... end box Group without description ... actors ... end box rgb(33,66,99) ... actors ... end
如果你的组名为颜色标识符,你也可以使用 transparent 关键字来定义一个透明的组:
1 2 3
box transparent Aqua ... actors ... end
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
sequenceDiagram box Purple Alice & John participant A participant J end box Another Group participant B participant C end A->>J: Hello John, how are you? J->>A: Great! A->>B: Hello Bob, how is Charly ? B->>C: Hello Charly, how are you?
消息
消息可以显示为实线或虚线两种形式。
1
[Actor][Arrow][Actor]:Message text
Mermaid 支持以下箭头:
类型
描述
->
无箭头实线
-->
无箭头虚线
->>
带箭头实线
-->>
带箭头虚线
-x
带 x 的实线
--x
带 x 的虚线
-)
带 ) 的实线 (async)
--)
带 ) 的虚线 (async)
激活
你可以使用 activate 和 deactivate 关键字来激活或停用参与者。
1 2 3 4 5
sequenceDiagram Alice->>John: Hello John, how are you? activate John John-->>Alice: Great! deactivate John
还有一种直接在消息后面添加 + 或 - 来激活或停用参与者的方法。
1 2 3
sequenceDiagram Alice->>+John: Hello John, how are you? John-->>-Alice: Great!
激活是可以堆叠的,如下所示:
1 2 3 4 5
sequenceDiagram Alice->>+John: Hello John, how are you? Alice->>+John: John, can you hear me? John-->>-Alice: Hi Alice, I can hear you! John-->>-Alice: I feel great!
说明
可以使用 Note 关键字添加说明。
1
Note [ right of | left of | over ] [Actor]: Text in note content
代码:
1 2 3
sequenceDiagram participant John Note right of John: Text in note
也可以添加跨越多个参与者的说明:
1 2 3
sequenceDiagram Alice->John: Hello John, how are you? Note over Alice,John: A typical interaction
也可以添加换行符:
1 2 3
sequenceDiagram Alice->John: Hello John, how are you? Note over Alice,John: A typical interaction<br/>But now in two lines
循环
可以在顺序图中使用 loop 关键字来定义循环。
1 2 3
loop Loop text ... statements ... end
代码:
1 2 3 4 5
sequenceDiagram Alice->John: Hello John, how are you? loop Every minute John-->Alice: Great! end
选择
可以在顺序图中使用 alt 关键字来定义选择。
1 2 3 4 5
alt Describing text ... statements ... else ... statements ... end
或者存在可选的序列:
1 2 3
opt Describing text ... statements ... end
代码:
1 2 3 4 5 6 7 8 9 10
sequenceDiagram Alice->>Bob: Hello Bob, how are you? alt is sick Bob->>Alice: Not so good :( else is well Bob->>Alice: Feeling fresh like a daisy end opt Extra response Bob->>Alice: Thanks for asking end
并行
可以在顺序图中使用 par 关键字来定义并行。
1 2 3 4 5 6 7
par [Action 1] ... statements ... and [Action 2] ... statements ... and [Action N] ... statements ... end
代码:
1 2 3 4 5 6 7 8
sequenceDiagram par Alice to Bob Alice->>Bob: Hello guys! and Alice to John Alice->>John: Hello guys! end Bob-->>Alice: Hi Alice! John-->>Alice: Hi Alice!
并行块也可以嵌套:
1 2 3 4 5 6 7 8 9 10 11
sequenceDiagram par Alice to Bob Alice->>Bob: Go help John and Alice to John Alice->>John: I want this done today par John to Charlie John->>Charlie: Can we do this today? and John to Diana John->>Diana: Can you help us today? end end
临界区
可以通过条件处理显示必须自动发生的操作。
1 2 3 4 5 6 7
critical [Action that must be performed] ... statements ... option [Circumstance A] ... statements ... option [Circumstance B] ... statements ... end
代码:
1 2 3 4 5 6 7 8
sequenceDiagram critical Establish a connection to the DB Service-->DB: connect option Network timeout Service-->Service: Log error option Credentials rejected Service-->Service: Log different error end
只有一个选择时,也可以使用临界区:
1 2 3 4
sequenceDiagram critical Establish a connection to the DB Service-->DB: connect end
临界区也可以嵌套,详情参考 par。
Break
可以在流中指示序列的停止(通常用于对异常建模)。
1 2 3
break [something happened] ... statements ... end
代码:
1 2 3 4 5 6 7
sequenceDiagram Consumer-->API: Book something API-->BookingService: Start booking process break when the booking process fails API-->Consumer: show failure end API-->BillingService: Start billing process
背景高亮
可以改变背景颜色来突出显示一些内容。
1 2 3 4 5 6
rect rgb(0, 255, 0) ... content ... end rect rgba(0, 0, 255, .1) ... content ... end
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
sequenceDiagram participant Alice participant John
rect rgb(191, 223, 255) note right of Alice: Alice calls John. Alice->>+John: Hello John, how are you? rect rgb(200, 150, 255) Alice->>+John: John, can you hear me? John-->>-Alice: Hi Alice, I can hear you! end John-->>-Alice: I feel great! end Alice ->>+ John: Did you want to go to the game tonight? John -->>- Alice: Yeah! See you there.
注释
可以在序列图中输入注释,解析器将忽略这些注释。使用 %% 标志注释的开始。
1 2 3 4
sequenceDiagram Alice->>John: Hello John, how are you? %% this is a comment John-->>Alice: Great!
转义字符的实体代码
可以使用 HTML 实体代码来转义字符。
1 2 3
sequenceDiagram A->>B: I #9829; you! B->>A: I #9829; you #infin; times more!
sequenceDiagram autonumber Alice->>John: Hello John, how are you? loop Healthcheck John->>John: Fight against hypochondria end Note right of John: Rational thoughts! John-->>Alice: Great! John->>Bob: How about you? Bob-->>John: Jolly good!
参与者菜单
参与者可以有包含到外部页面的个性化链接的弹出式菜单。例如,如果参与者表示 web 服务,则有用的链接可能包括指向服务运行状况指示板的链接、包含服务代码的 repo 或描述服务的 wiki 页面。
语法格式如:
1
link <actor>: <link-label> @ <link-url>
代码:
1 2 3 4 5 6 7 8 9 10
sequenceDiagram participant Alice participant John link Alice: Dashboard @ https://dashboard.contoso.com/alice link Alice: Wiki @ https://wiki.contoso.com/alice link John: Dashboard @ https://dashboard.contoso.com/john link John: Wiki @ https://wiki.contoso.com/john Alice->>John: Hello John, how are you? John-->>Alice: Great! Alice-)John: See you later!
sequenceDiagram participant Alice participant John links Alice: {"Dashboard": "https://dashboard.contoso.com/alice", "Wiki": "https://wiki.contoso.com/alice"} links John: {"Dashboard": "https://dashboard.contoso.com/john", "Wiki": "https://wiki.contoso.com/john"} Alice->>John: Hello John, how are you? John-->>Alice: Great! Alice-)John: See you later!