Mermaid知识点总结6 - Sequence diagram 2
描述循环
以下语法可以在Sequence diagram中描述循环:
loop Loop text
... statements ...
end
如下实例所示:
sequenceDiagram
Alice->John: Hello John, how are you?
loop Every minute
John-->Alice: Great!
end
可选对话
可以使用alt…else…或者opt进行可选对话的描述,如下所示:
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
else is hy
Bob->>Alice: Always feeling well if you are around
end
opt Extra response
Bob->>Alice: Thanks for asking
end
多个参与者
参与者可以不只两个人,相应的某个参与者可以与多个参与者进行互动,使用par表示平行关系,如下所示:
par [Action 1]
... statements ...
and [Action 2]
... statements ...
and [Action N]
... statements ...
end
讲道理,这个教程中只给出了结果没有给原文,下面是根据结果自己写的:
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!
同样,可以嵌套进行par,下面也是根据教程中的图例自己写的:
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 we do this today?
end
end
背景强调
可以使用以下语法将相应内容区域背景填充颜色,背景颜色以rgb或者rgba进行规定:
rect rgb(0, 255, 0)
... content ...
end
rect rgba(0, 0, 255, .1)
... content ...
end
不给源码哦,难顶,下面是根据样例自己写的:
sequenceDiagram
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:Do you want to go to the game tonight?
John -->>- Alice:Yeah!See you there.
我怀疑原教程此处有语法错误:
序列号
可以对序列顺序添加序号,如下所示:
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!