使用 PlantUML 创建序列图非常简单。这种易用性主要归功于其语法的用户友好性,既直观又易记。
- 直观的语法:
首先,用户非常欣赏 PlantUML 所采用的简单直观的语法。这种经过深思熟虑的设计意味着,即使是图表创建新手也能轻松快速地掌握基础知识。
- 文本与图形的关联:
另一个显著特点是文本表示与图形输出之间非常相似。这种和谐的相关性可确保文本草稿准确地转化为图形图表,从而提供连贯、可预测的设计体验,在最终输出中不会出现令人不快的意外。
- 高效的制作过程:
文本和图形结果之间的紧密联系不仅简化了制作过程,还大大加快了制作速度。用户可从更简化的流程中获益,减少耗时的修改和调整要求。
- 起草时的可视化:
在起草文本的同时就能设想最终的图形结果,这是许多人认为非常宝贵的一项功能。它自然而然地促进了从初稿到最终呈现的顺利过渡,提高了工作效率,降低了出错的可能性。
- 易于编辑和修改:
重要的是,编辑现有图表的过程非常简便。由于图表是由文本生成的,用户会发现进行调整比使用图形工具修改图像要容易得多,也精确得多。
PlantUML 为创建和编辑序列图提供了一种简单明了、用户友好的方法,既能满足新手的需求,也能满足经验丰富的设计人员的需求。它巧妙地利用文本输入的简便性来制作具有视觉描述性和准确性的图表,从而使自己成为图表创建工具包中的必备工具。
您可以了解更多有关PlantUML 中一些常用命令的信息,以增强您的图表创建体验。
基本的例子
序列->
用于绘制两个参与者之间的信息。参与者不必明确声明。
要有一个点状的箭头,就用-->
也可以用<-
和<--
。这不会改变绘图,但可能提高可读性。注意,这只适用于顺序图,其他图的规则不同。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: Another authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: Another authentication Response
@enduml
2
3
4
5
6
7
8
9
声明参与者
如果使用关键字participant
来声明参与者,就可以对该参与者进行更多的控制。
声明的顺序将是(默认的)显示顺序。
使用这些其他的关键字来声明参与者,将改变参与者的表示形状。
actor
(角色)boundary
(边界)control
(控制)entity
(实体)database
(数据库)collections
(集合)queue
(队列)
@startuml
!include https://f.umlcn.com/c4.puml
participant Participant as Foo
actor Actor as Foo1
boundary Boundary as Foo2
control Control as Foo3
entity Entity as Foo4
database Database as Foo5
collections Collections as Foo6
queue Queue as Foo7
Foo -> Foo1 : To actor
Foo -> Foo2 : To boundary
Foo -> Foo3 : To control
Foo -> Foo4 : To entity
Foo -> Foo5 : To database
Foo -> Foo6 : To collections
Foo -> Foo7: To queue
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Participant as Foo
actor Actor as Foo1
boundary Boundary as Foo2
control Control as Foo3
entity Entity as Foo4
database Database as Foo5
collections Collections as Foo6
queue Queue as Foo7
Foo -> Foo1 : To actor
Foo -> Foo2 : To boundary
Foo -> Foo3 : To control
Foo -> Foo4 : To entity
Foo -> Foo5 : To database
Foo -> Foo6 : To collections
Foo -> Foo7: To queue
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
使用as
关键字重命名参与者。
你也可以改变 演员或参与者的背景颜色。
@startuml
!include https://f.umlcn.com/c4.puml
actor Bob #red
' The only difference between actor
'and participant is the drawing
participant Alice
participant "I have a really\nlong name" as L #99FF99
/' You can also declare:
participant L as "I have a really\nlong name" #99FF99
'/
Alice->Bob: Authentication Request
Bob->Alice: Authentication Response
Bob->L: Log transaction
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
actor Bob #red
' The only difference between actor
'and participant is the drawing
participant Alice
participant "I have a really\nlong name" as L #99FF99
/' You can also declare:
participant L as "I have a really\nlong name" #99FF99
'/
Alice->Bob: Authentication Request
Bob->Alice: Authentication Response
Bob->L: Log transaction
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
您可以使用order
关键字来定制参与者的显示顺序。
@startuml
!include https://f.umlcn.com/c4.puml
participant Last order 30
participant Middle order 20
participant First order 10
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Last order 30
participant Middle order 20
participant First order 10
@enduml
2
3
4
5
6
7
多行定义参与者
你可以对参与者使用多行定义。
@startuml
!include https://f.umlcn.com/c4.puml
participant Participant [
=Title
----
""打算""
]
participant Bob
Participant -> Bob
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Participant [
=Title
----
""打算""
]
participant Bob
Participant -> Bob
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
在参与者中使用非字母
你可以使用引号来定义参与者。 而且你可以使用as
关键字来给这些参与者一个别名。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> "Bob()" : Hello
"Bob()" -> "This is very\nlong" as Long
' You can also declare:
' "Bob()" -> Long as "This is very\nlong"
Long --> "Bob()" : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> "Bob()" : Hello
"Bob()" -> "This is very\nlong" as Long
' You can also declare:
' "Bob()" -> Long as "This is very\nlong"
Long --> "Bob()" : ok
@enduml
2
3
4
5
6
7
8
9
给自己发消息
参与者可以给自己发信息,
消息文字可以用\n
来换行。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
Alice <- Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
Alice <- Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml
2
3
4
5
6
7
8
文本对齐
箭头上的文本对齐可以用skinparam sequenceMessageAlign
,后接参数left
,right
或center
。
你也可以使用direction
或reverseDirection
来根据箭头的方向对齐文本。更多细节可参考skinparam。
@startuml
!include https://f.umlcn.com/c4.puml
skinparam sequenceMessageAlign right
Bob -> Alice : Request
Alice -> Bob : Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam sequenceMessageAlign right
Bob -> Alice : Request
Alice -> Bob : Response
@enduml
2
3
4
5
6
7
8
让响应信息显示在箭头下面
你可以使用skinparam responseMessageBelowArrow true
命令,让响应信息显示在箭头下面。
@startuml
!include https://f.umlcn.com/c4.puml
skinparam responseMessageBelowArrow true
Bob -> Alice : hello
Bob <- Alice : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam responseMessageBelowArrow true
Bob -> Alice : hello
Bob <- Alice : ok
@enduml
2
3
4
5
6
7
改变箭头样式
您可以通过以下几种方式改变箭头样式:
- 添加最后的
x
表示丢失的信息 - 使用
\
或/
而不是<
或>
只拥有箭头的底部或顶部部分 - 重复箭头头(例如
>>
或//
)头,拥有一个薄的图纸 - 使用
--
而不是-
拥有一个点状箭头 - 在箭头头添加最后的 "o"
- 使用双向的箭头
<->
@startuml
!include https://f.umlcn.com/c4.puml
Bob ->x Alice
Bob -> Alice
Bob ->> Alice
Bob -\ Alice
Bob \\- Alice
Bob //-- Alice
Bob ->o Alice
Bob o\\-- Alice
Bob <-> Alice
Bob <->o Alice
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Bob ->x Alice
Bob -> Alice
Bob ->> Alice
Bob -\ Alice
Bob \\- Alice
Bob //-- Alice
Bob ->o Alice
Bob o\\-- Alice
Bob <-> Alice
Bob <->o Alice
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
修改箭头颜色
你可以用以下记号修改箭头的颜色:
@startuml
!include https://f.umlcn.com/c4.puml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml
2
3
4
5
6
对消息序列编号
关键字 autonumber
用于自动对消息编号。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
@enduml
2
3
4
5
6
7
语句 autonumber //start//
用于指定编号的初始值,而 autonumber //start// //increment//
可以同时指定编号的初始值和每次增加的值。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
你可以在双引号内指定编号的格式。
格式是由 Java 的DecimalFormat
类实现的: (0
表示数字;#
也表示数字,但默认为0)。
你也可以用 HTML 标签来制定格式。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15 "<b>(<u>##</u>)"
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10 "<font color=red><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15 "<b>(<u>##</u>)"
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10 "<font color=red><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
你还可以用语句 autonumber stop
和 autonumber resume //increment// //format//
来表示暂停或继续使用自动编号。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 10 10 "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume "<font color=red><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume 1 "<font color=blue><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 10 10 "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume "<font color=red><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
autonumber stop
Bob -> Alice : dummy
autonumber resume 1 "<font color=blue><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
你也可以使用一个2或3位的序列,中间采用一种或几种分隔符,如.
,;
,,
,:
。例如:1.1.1
或1.1:1
。
最后一位数字会自动递增。
要增加第一个数字,请使用:autonumber inc A
。要增加第二位数字,请使用:autonumber inc B
。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 1.1.1
Alice -> Bob: Authentication request
Bob --> Alice: Response
autonumber inc A
'Now we have 2.1.1
Alice -> Bob: Another authentication request
Bob --> Alice: Response
autonumber inc B
'Now we have 2.2.1
Alice -> Bob: Another authentication request
Bob --> Alice: Response
autonumber inc A
'Now we have 3.1.1
Alice -> Bob: Another authentication request
autonumber inc B
'Now we have 3.2.1
Bob --> Alice: Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 1.1.1
Alice -> Bob: Authentication request
Bob --> Alice: Response
autonumber inc A
'Now we have 2.1.1
Alice -> Bob: Another authentication request
Bob --> Alice: Response
autonumber inc B
'Now we have 2.2.1
Alice -> Bob: Another authentication request
Bob --> Alice: Response
autonumber inc A
'Now we have 3.1.1
Alice -> Bob: Another authentication request
autonumber inc B
'Now we have 3.2.1
Bob --> Alice: Response
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
你也可以用autonumber
的值,带有%autonumber%
变量。
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 10
Alice -> Bob
note right
the <U+0025>autonumber<U+0025> works everywhere.
Here, its value is ** %autonumber% **
end note
Bob --> Alice: //This is the response %autonumber%//
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autonumber 10
Alice -> Bob
note right
the <U+0025>autonumber<U+0025> works everywhere.
Here, its value is ** %autonumber% **
end note
Bob --> Alice: //This is the response %autonumber%//
@enduml
2
3
4
5
6
7
8
9
10
11
页面标题、页眉和页脚
title
关键字用于为页面添加标题。
页面可以使用header
和footer
显示页眉和页脚。
@startuml
!include https://f.umlcn.com/c4.puml
header Page Header
footer Page %page% of %lastpage%
title Example Title
Alice -> Bob : message 1
Alice -> Bob : message 2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
header Page Header
footer Page %page% of %lastpage%
title Example Title
Alice -> Bob : message 1
Alice -> Bob : message 2
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
分割示意图
关键字 newpage
用于把一张图分割成多张。
在 newpage
之后添加文字,作为新的示意图的标题。
这样就能很方便地在 Word 中将长图分几页打印。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob : message 1
Alice -> Bob : message 2
newpage
Alice -> Bob : message 3
Alice -> Bob : message 4
newpage A title for the\nlast page
Alice -> Bob : message 5
Alice -> Bob : message 6
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob : message 1
Alice -> Bob : message 2
newpage
Alice -> Bob : message 3
Alice -> Bob : message 4
newpage A title for the\nlast page
Alice -> Bob : message 5
Alice -> Bob : message 6
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
组合消息
我们可以通过以下关键词来组合消息:
alt/else
opt
loop
par
break
critical
group
, 后面紧跟着消息内容
可以在标头(header)添加需要显示的文字(对于group
关键字,参看下一章节 '次级分组标签')。
关键词 end
用来结束分组。
注意,分组可以嵌套使用。
@startuml
Alice -> Bob: 认证请求
alt 成功情况
Bob -> Alice: 认证接受
else 某种失败情况
Bob -> Alice: 认证失败
group 我自己的标签
Alice -> Log : 开始记录攻击日志
loop 1000次
Alice -> Bob: DNS 攻击
end
Alice -> Log : 结束记录攻击日志
end
else 另一种失败
Bob -> Alice: 请重复
end
@enduml
@startuml
Alice -> Bob: 认证请求
alt 成功情况
Bob -> Alice: 认证接受
else 某种失败情况
Bob -> Alice: 认证失败
group 我自己的标签
Alice -> Log : 开始记录攻击日志
loop 1000次
Alice -> Bob: DNS 攻击
end
Alice -> Log : 结束记录攻击日志
end
else 另一种失败
Bob -> Alice: 请重复
end
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
次级分组标签
对于group
而言,在标头处的[
和]
之间可以显示次级文本或标签。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: 认证请求
Bob -> Alice: 认证失败
group 我自己的标签 [我自己的标签2]
Alice -> Log : 开始记录攻击日志
loop 1000次
Alice -> Bob: DNS攻击
end
Alice -> Log : 结束记录攻击日志
end
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: 认证请求
Bob -> Alice: 认证失败
group 我自己的标签 [我自己的标签2]
Alice -> Log : 开始记录攻击日志
loop 1000次
Alice -> Bob: DNS攻击
end
Alice -> Log : 结束记录攻击日志
end
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
注释信息
可以使用note left
或note right
关键字在信息后面加上注释。
你可以使用end note
关键字有一个多行注释。
@startuml
!include https://f.umlcn.com/c4.puml
Alice->Bob : hello
note left: this is a first note
Bob->Alice : ok
note right: this is another note
Bob->Bob : I am thinking
note left
a note
can also be defined
on several lines
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice->Bob : hello
note left: this is a first note
Bob->Alice : ok
note right: this is another note
Bob->Bob : I am thinking
note left
a note
can also be defined
on several lines
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
其他的注释信息方式
可以使用note left of
,note right of
或note over
在节点(participant)的相对位置放置注释。
还可以通过修改背景色来高亮显示注释。
以及使用关键字end note
来添加多行注释。
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob
note left of Alice #aqua
This is displayed
left of Alice.
end note
note right of Alice: This is displayed right of Alice.
note over Alice: This is displayed over Alice.
note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.
note over Bob, Alice
This is yet another
example of
a long note.
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob
note left of Alice #aqua
This is displayed
left of Alice.
end note
note right of Alice: This is displayed right of Alice.
note over Alice: This is displayed over Alice.
note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.
note over Bob, Alice
This is yet another
example of
a long note.
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
改变备注框的形状 [hnote和rnote]
你可以使用 hnote
和 rnote
这两个关键字来修改备注框的形状:
hnote
代表六边形(hexagonal)的备注框;rnote
代表正方形(rectangle)的备注框。
@startuml
!include https://f.umlcn.com/c4.puml
caller -> server : conReq
hnote over caller : 空闲
caller <- server : conConf
rnote over server
"r"是正方形
"h"是六边形
endrnote
rnote over server
多
行
文本
endrnote
hnote over caller
多
行
文本
endhnote
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
caller -> server : conReq
hnote over caller : 空闲
caller <- server : conConf
rnote over server
"r"是正方形
"h"是六边形
endrnote
rnote over server
多
行
文本
endrnote
hnote over caller
多
行
文本
endhnote
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
在多个参与者添加备注 [across]
你可以之直接在所有参与者之间添加备注,格式是:
note across: 备注描述
@startuml
!include https://f.umlcn.com/c4.puml
Alice->Bob:m1
Bob->Charlie:m2
note over Alice, Charlie: 创建跨越所有参与者的备注的旧方法:\n ""note over //FirstPart, LastPart//"".
note across: 新方法:\n""note across""
Bob->Alice
hnote across: 跨越所有参与者的备注。
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice->Bob:m1
Bob->Charlie:m2
note over Alice, Charlie: 创建跨越所有参与者的备注的旧方法:\n ""note over //FirstPart, LastPart//"".
note across: 新方法:\n""note across""
Bob->Alice
hnote across: 跨越所有参与者的备注。
@enduml
2
3
4
5
6
7
8
9
10
在同一级对齐多个备注 [/]
使用/
可以在同一级对齐多个备注:
- 没有
/
(默认情况下,备注不是对齐的。)
@startuml
!include https://f.umlcn.com/c4.puml
note over Alice : Alice的初始状态
note over Bob : Bob的初始状态
Bob -> Alice : hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
note over Alice : Alice的初始状态
note over Bob : Bob的初始状态
Bob -> Alice : hello
@enduml
2
3
4
5
6
7
- with
/
(the notes are aligned)
@startuml
!include https://f.umlcn.com/c4.puml
note over Alice : Alice的初始状态
/ note over Bob : Bob的初始状态
Bob -> Alice : hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
note over Alice : Alice的初始状态
/ note over Bob : Bob的初始状态
Bob -> Alice : hello
@enduml
2
3
4
5
6
7
Creole和HTML
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant "The **Famous** Bob" as Bob
Alice -> Bob : hello --there--
... Some ~~long delay~~ ...
Bob -> Alice : ok
note left
This is **bold**
This is //italics//
This is ""monospaced""
This is --stroked--
This is __underlined__
This is ~~waved~~
end note
Alice -> Bob : A //well formatted// message
note right of Alice
This is <back:cadetblue><size:18>displayed</size></back>
__left of__ Alice.
end note
note left of Bob
<u:red>This</u> is <color #118888>displayed</color>
**<color purple>left of</color> <s:red>Alice</strike> Bob**.
end note
note over Alice, Bob
<w:#FF33FF>This is hosted</w> by <img sourceforge.jpg>
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant "The **Famous** Bob" as Bob
Alice -> Bob : hello --there--
... Some ~~long delay~~ ...
Bob -> Alice : ok
note left
This is **bold**
This is //italics//
This is ""monospaced""
This is --stroked--
This is __underlined__
This is ~~waved~~
end note
Alice -> Bob : A //well formatted// message
note right of Alice
This is <back:cadetblue><size:18>displayed</size></back>
__left of__ Alice.
end note
note left of Bob
<u:red>This</u> is <color #118888>displayed</color>
**<color purple>left of</color> <s:red>Alice</strike> Bob**.
end note
note over Alice, Bob
<w:#FF33FF>This is hosted</w> by <img sourceforge.jpg>
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
分隔符
你可以通过使用==
关键词来将你的图表分割成多个逻辑步骤。
@startuml
!include https://f.umlcn.com/c4.puml
== 初始化 ==
Alice -> Bob: 认证请求
Bob --> Alice: 认证响应
== 重复 ==
Alice -> Bob: 认证请求
Alice <-- Bob: 认证响应
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
== 初始化 ==
Alice -> Bob: 认证请求
Bob --> Alice: 认证响应
== 重复 ==
Alice -> Bob: 认证请求
Alice <-- Bob: 认证响应
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
引用
你可以在图中通过使用ref over
关键词来实现引用
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
actor Bob
ref over Alice, Bob : init
Alice -> Bob : hello
ref over Bob
This can be on
several lines
end ref
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
actor Bob
ref over Alice, Bob : init
Alice -> Bob : hello
ref over Bob
This can be on
several lines
end ref
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
延迟
你可以使用...
来表示延迟,并且还可以给延迟添加注释。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: 认证请求
...
Bob --> Alice: 认证响应
...5分钟后...
Bob --> Alice: 再见!
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: 认证请求
...
Bob --> Alice: 认证响应
...5分钟后...
Bob --> Alice: 再见!
@enduml
2
3
4
5
6
7
8
9
10
11
文本换行
你可以通过手动在文本中添加\n
使长文本换行。
或者使用maxMessageSize
设置(此方式暂不支持中文换行):
@startuml
!include https://f.umlcn.com/c4.puml
skinparam maxMessageSize 50
participant a
participant b
a -> b :这\n一条\n是\n手动换行
a -> b :this is a very long message on several words
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam maxMessageSize 50
participant a
participant b
a -> b :这\n一条\n是\n手动换行
a -> b :this is a very long message on several words
@enduml
2
3
4
5
6
7
8
9
空间
你可以使用|||
来增加空间。
还可以使用数字指定增加的像素的数量。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: message 1
Bob --> Alice: ok
|||
Alice -> Bob: message 2
Bob --> Alice: ok
||45||
Alice -> Bob: message 3
Bob --> Alice: ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: message 1
Bob --> Alice: ok
|||
Alice -> Bob: message 2
Bob --> Alice: ok
||45||
Alice -> Bob: message 3
Bob --> Alice: ok
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
生命线的激活与撤销
关键字activate
和deactivate
用来表示参与者的生命活动。
一旦参与者被激活,它的生命线就会显示出来。
activate
和deactivate
适用于以上情形。
destroy
表示一个参与者的生命线的终结。
@startuml
!include https://f.umlcn.com/c4.puml
participant User
User -> A: DoWork
activate A
A -> B: << createRequest >>
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: RequestCreated
deactivate B
A -> User: Done
deactivate A
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant User
User -> A: DoWork
activate A
A -> B: << createRequest >>
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: RequestCreated
deactivate B
A -> User: Done
deactivate A
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
还可以使用嵌套的生命线,并且运行给生命线添加颜色。
@startuml
!include https://f.umlcn.com/c4.puml
participant User
User -> A: DoWork
activate A #FFBBBB
A -> A: Internal call
activate A #DarkSalmon
A -> B: << createRequest >>
activate B
B --> A: RequestCreated
deactivate B
deactivate A
A -> User: Done
deactivate A
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant User
User -> A: DoWork
activate A #FFBBBB
A -> A: Internal call
activate A #DarkSalmon
A -> B: << createRequest >>
activate B
B --> A: RequestCreated
deactivate B
deactivate A
A -> User: Done
deactivate A
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
也可以使用自动激活关键字(autoactivate),这需要与return
关键字配合:
@startuml
!include https://f.umlcn.com/c4.puml
autoactivate on
alice -> bob : hello
bob -> bob : self call
bill -> bob #005500 : hello from thread 2
bob -> george ** : create
return done in thread 2
return rc
bob -> george !! : delete
return success
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
autoactivate on
alice -> bob : hello
bob -> bob : self call
bill -> bob #005500 : hello from thread 2
bob -> george ** : create
return done in thread 2
return rc
bob -> george !! : delete
return success
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
返回
新命令return
可以用于生成一个带有可选文本标签的返回信息。返回的点是导致最近一次激活生命线的点。语法是简单的返回标签,其中标签(如果提供)可以是传统信息中可以接受的任何字符串。
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
activate Alice
Alice -> Alice : some action
return bye
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
activate Alice
Alice -> Alice : some action
return bye
@enduml
2
3
4
5
6
7
8
创建参与者
你可以把关键字create
放在第一次接收到消息之前,以强调本次消息实际上是在创建新的对象。
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
create Other
Alice -> Other : new
create control String
Alice -> String
note right : You can also put notes!
Alice --> Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
create Other
Alice -> Other : new
create control String
Alice -> String
note right : You can also put notes!
Alice --> Bob : ok
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
激活、撤销和创建的快捷语法。
在指定目标参与者后,可以立即使用以下语法:
++
激活目标(可选择在后面加上#color)--
撤销激活源**
创建目标实例!!
摧毁目标实例
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob ++ : hello
bob -> bob ++ : self call
bob -> bib ++ #005500 : hello
bob -> george ** : create
return done
return rc
bob -> george !! : delete
return success
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob ++ : hello
bob -> bob ++ : self call
bob -> bib ++ #005500 : hello
bob -> george ** : create
return done
return rc
bob -> george !! : delete
return success
@enduml
2
3
4
5
6
7
8
9
10
11
12
然后你就可以在一行上同时激活和撤销:
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob ++ : hello1
bob -> charlie --++ : hello2
charlie --> alice -- : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob ++ : hello1
bob -> charlie --++ : hello2
charlie --> alice -- : ok
@enduml
2
3
4
5
6
7
@startuml
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob --++ #gold: hello
bob -> alice --++ #gold: you too
alice -> bob --: step1
alice -> bob : step2
@enduml
@enduml
@startuml
@startuml
!include https://f.umlcn.com/c4.puml
alice -> bob --++ #gold: hello
bob -> alice --++ #gold: you too
alice -> bob --: step1
alice -> bob : step2
@enduml
@enduml
2
3
4
5
6
7
8
9
10
进入和发出消息
如果只想关注部分图示,你可以使用进入和发出箭头。
使用方括号[
和]
表示图示的左、右两侧。
@startuml
!include https://f.umlcn.com/c4.puml
[-> A: DoWork
activate A
A -> A: Internal call
activate A
A ->] : << createRequest >>
A<--] : RequestCreated
deactivate A
[<- A: Done
deactivate A
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
[-> A: DoWork
activate A
A -> A: Internal call
activate A
A ->] : << createRequest >>
A<--] : RequestCreated
deactivate A
[<- A: Done
deactivate A
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
还可以使用下面的语法:
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob #lightblue
Alice -> Bob
Bob -> Carol
...
[-> Bob
[o-> Bob
[o->o Bob
[x-> Bob
...
[<- Bob
[x<- Bob
...
Bob ->]
Bob ->o]
Bob o->o]
Bob ->x]
...
Bob <-]
Bob x<-]
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob #lightblue
Alice -> Bob
Bob -> Carol
...
[-> Bob
[o-> Bob
[o->o Bob
[x-> Bob
...
[<- Bob
[x<- Bob
...
Bob ->]
Bob ->o]
Bob o->o]
Bob ->x]
...
Bob <-]
Bob x<-]
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
缩短的进入信息与发出信息箭头
使用?
来显示缩短的箭头。
@startuml
!include https://f.umlcn.com/c4.puml
?-> Alice : ""?->""\n**short** to actor1
[-> Alice : ""[->""\n**from start** to actor1
[-> Bob : ""[->""\n**from start** to actor2
?-> Bob : ""?->""\n**short** to actor2
Alice ->] : ""->]""\nfrom actor1 **to end**
Alice ->? : ""->?""\n**short** from actor1
Alice -> Bob : ""->"" \nfrom actor1 to actor2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
?-> Alice : ""?->""\n**short** to actor1
[-> Alice : ""[->""\n**from start** to actor1
[-> Bob : ""[->""\n**from start** to actor2
?-> Bob : ""?->""\n**short** to actor2
Alice ->] : ""->]""\nfrom actor1 **to end**
Alice ->? : ""->?""\n**short** from actor1
Alice -> Bob : ""->"" \nfrom actor1 to actor2
@enduml
2
3
4
5
6
7
8
9
10
11
锚点和持续时间
使用teoz
在图表中添加锚点,从而指定持续时间。
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
{start} Alice -> Bob : start doing things during duration
Bob -> Max : something
Max -> Bob : something else
{end} Bob -> Alice : finish
{start} <-> {end} : some time
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
{start} Alice -> Bob : start doing things during duration
Bob -> Max : something
Max -> Bob : something else
{end} Bob -> Alice : finish
{start} <-> {end} : some time
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
You can use the -P
command-line option to specify the pragma:
``
java -jar plantuml.jar -Pteoz=true
java -jar plantuml.jar -Pteoz=true
构造类型和圈点
可以使用<<
和>>
给参与者添加构造类型。
在构造类型中,你可以使用(X,color)
格式的语法添加一个圆圈圈起来的字符。
@startuml
!include https://f.umlcn.com/c4.puml
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
2
3
4
5
6
7
8
9
10
默认使用 guillemet 字符来显示构造类型。 你可以使用外观参数 guillemet
来修改显示行为。
@startuml
!include https://f.umlcn.com/c4.puml
skinparam guillemet false
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam guillemet false
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml
2
3
4
5
6
7
8
9
10
11
@startuml
!include https://f.umlcn.com/c4.puml
participant Bob << (C,#ADD1B2) >>
participant Alice << (C,#ADD1B2) >>
Bob->Alice: First message
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Bob << (C,#ADD1B2) >>
participant Alice << (C,#ADD1B2) >>
Bob->Alice: First message
@enduml
2
3
4
5
6
7
8
9
10
Position of the stereotypes
It is possible to define stereotypes position (top
or bottom
) with the command skinparam stereotypePosition
.
Top postion (by default)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam stereotypePosition top
participant A<<st1>>
participant B<<st2>>
A --> B : stereo test
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam stereotypePosition top
participant A<<st1>>
participant B<<st2>>
A --> B : stereo test
@enduml
2
3
4
5
6
7
8
9
Bottom postion
@startuml
!include https://f.umlcn.com/c4.puml
skinparam stereotypePosition bottom
participant A<<st1>>
participant B<<st2>>
A --> B : stereo test
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam stereotypePosition bottom
participant A<<st1>>
participant B<<st2>>
A --> B : stereo test
@enduml
2
3
4
5
6
7
8
9
更多标题信息
你可以在标题中使用creole格式。
@startuml
!include https://f.umlcn.com/c4.puml
title __Simple__ **communication** example
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title __Simple__ **communication** example
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
2
3
4
5
6
7
8
9
10
在标题描述中使用\n
表示换行。
@startuml
!include https://f.umlcn.com/c4.puml
title __Simple__ communication example\non several lines
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title __Simple__ communication example\non several lines
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
2
3
4
5
6
7
8
9
10
还可以使用关键字title
和end title
定义多行标题。
@startuml
!include https://f.umlcn.com/c4.puml
title
<u>Simple</u> communication example
on <i>several</i> lines and using <font color=red>html</font>
This is hosted by <img:sourceforge.jpg>
end title
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title
<u>Simple</u> communication example
on <i>several</i> lines and using <font color=red>html</font>
This is hosted by <img:sourceforge.jpg>
end title
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
包裹参与者
可以使用box
和end box
画一个盒子将参与者包裹起来。
还可以在box
关键字之后添加标题或者背景颜色。
@startuml
!include https://f.umlcn.com/c4.puml
box "Internal Service" #LightBlue
participant Bob
participant Alice
end box
participant Other
Bob -> Alice : hello
Alice -> Other : hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
box "Internal Service" #LightBlue
participant Bob
participant Alice
end box
participant Other
Bob -> Alice : hello
Alice -> Other : hello
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
移除脚注
使用hide footbox
关键字移除脚注。
@startuml
!include https://f.umlcn.com/c4.puml
hide footbox
title Footer removed
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
hide footbox
title Footer removed
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml
2
3
4
5
6
7
8
9
10
11
外观参数(skinparam)
用skinparam改变字体和颜色。
可以在如下场景中使用:
你也可以修改其他渲染元素,如以下示例:
@startuml
!include https://f.umlcn.com/c4.puml
skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 60
skinparam sequenceParticipant underline
actor User
participant "First Class" as A
participant "Second Class" as B
participant "Last Class" as C
User -> A: DoWork
activate A
A -> B: Create Request
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: Request Created
deactivate B
A --> User: Done
deactivate A
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam sequenceArrowThickness 2
skinparam roundcorner 20
skinparam maxmessagesize 60
skinparam sequenceParticipant underline
actor User
participant "First Class" as A
participant "Second Class" as B
participant "Last Class" as C
User -> A: DoWork
activate A
A -> B: Create Request
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: Request Created
deactivate B
A --> User: Done
deactivate A
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@startuml
!include https://f.umlcn.com/c4.puml
skinparam backgroundColor #EEEBDC
skinparam handwritten true
skinparam sequence {
ArrowColor DeepSkyBlue
ActorBorderColor DeepSkyBlue
LifeLineBorderColor blue
LifeLineBackgroundColor #A9DCDF
ParticipantBorderColor DeepSkyBlue
ParticipantBackgroundColor DodgerBlue
ParticipantFontName Impact
ParticipantFontSize 17
ParticipantFontColor #A9DCDF
ActorBackgroundColor aqua
ActorFontColor DeepSkyBlue
ActorFontSize 17
ActorFontName Aapex
}
actor User
participant "First Class" as A
participant "Second Class" as B
participant "Last Class" as C
User -> A: DoWork
activate A
A -> B: Create Request
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: Request Created
deactivate B
A --> User: Done
deactivate A
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam backgroundColor #EEEBDC
skinparam handwritten true
skinparam sequence {
ArrowColor DeepSkyBlue
ActorBorderColor DeepSkyBlue
LifeLineBorderColor blue
LifeLineBackgroundColor #A9DCDF
ParticipantBorderColor DeepSkyBlue
ParticipantBackgroundColor DodgerBlue
ParticipantFontName Impact
ParticipantFontSize 17
ParticipantFontColor #A9DCDF
ActorBackgroundColor aqua
ActorFontColor DeepSkyBlue
ActorFontSize 17
ActorFontName Aapex
}
actor User
participant "First Class" as A
participant "Second Class" as B
participant "Last Class" as C
User -> A: DoWork
activate A
A -> B: Create Request
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: Request Created
deactivate B
A --> User: Done
deactivate A
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
填充区设置
可以设定填充区的参数配置。
@startuml
!include https://f.umlcn.com/c4.puml
skinparam ParticipantPadding 20
skinparam BoxPadding 10
box "Foo1"
participant Alice1
participant Alice2
end box
box "Foo2"
participant Bob1
participant Bob2
end box
Alice1 -> Bob1 : hello
Alice1 -> Out : out
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam ParticipantPadding 20
skinparam BoxPadding 10
box "Foo1"
participant Alice1
participant Alice2
end box
box "Foo2"
participant Bob1
participant Bob2
end box
Alice1 -> Bob1 : hello
Alice1 -> Out : out
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
附录:箭头类型大全
普通箭头
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : ""-> ""
a ->> b : ""->> ""
a -\ b : ""-\ ""
a -\\ b : ""-\\\\""
a -/ b : ""-/ ""
a -// b : ""-// ""
a ->x b : ""->x ""
a x-> b : ""x-> ""
a o-> b : ""o-> ""
a ->o b : ""->o ""
a o->o b : ""o->o ""
a <-> b : ""<-> ""
a o<->o b : ""o<->o""
a x<->x b : ""x<->x""
a ->>o b : ""->>o ""
a -\o b : ""-\o ""
a -\\o b : ""-\\\\o""
a -/o b : ""-/o ""
a -//o b : ""-//o ""
a x->o b : ""x->o ""
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : ""-> ""
a ->> b : ""->> ""
a -\ b : ""-\ ""
a -\\ b : ""-\\\\""
a -/ b : ""-/ ""
a -// b : ""-// ""
a ->x b : ""->x ""
a x-> b : ""x-> ""
a o-> b : ""o-> ""
a ->o b : ""->o ""
a o->o b : ""o->o ""
a <-> b : ""<-> ""
a o<->o b : ""o<->o""
a x<->x b : ""x<->x""
a ->>o b : ""->>o ""
a -\o b : ""-\o ""
a -\\o b : ""-\\\\o""
a -/o b : ""-/o ""
a -//o b : ""-//o ""
a x->o b : ""x->o ""
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
进入信息和发出信息(使用'[', ']')
进入信息(使用'[')
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
[-> b : ""[-> ""
[->> b : ""[->> ""
[-\ b : ""[-\ ""
[-\\ b : ""[-\\\\""
[-/ b : ""[-/ ""
[-// b : ""[-// ""
[->x b : ""[->x ""
[x-> b : ""[x-> ""
[o-> b : ""[o-> ""
[->o b : ""[->o ""
[o->o b : ""[o->o ""
[<-> b : ""[<-> ""
[o<->o b : ""[o<->o""
[x<->x b : ""[x<->x""
[->>o b : ""[->>o ""
[-\o b : ""[-\o ""
[-\\o b : ""[-\\\\o""
[-/o b : ""[-/o ""
[-//o b : ""[-//o ""
[x->o b : ""[x->o ""
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
[-> b : ""[-> ""
[->> b : ""[->> ""
[-\ b : ""[-\ ""
[-\\ b : ""[-\\\\""
[-/ b : ""[-/ ""
[-// b : ""[-// ""
[->x b : ""[->x ""
[x-> b : ""[x-> ""
[o-> b : ""[o-> ""
[->o b : ""[->o ""
[o->o b : ""[o->o ""
[<-> b : ""[<-> ""
[o<->o b : ""[o<->o""
[x<->x b : ""[x<->x""
[->>o b : ""[->>o ""
[-\o b : ""[-\o ""
[-\\o b : ""[-\\\\o""
[-/o b : ""[-/o ""
[-//o b : ""[-//o ""
[x->o b : ""[x->o ""
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
发出信息(使用']')
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a ->] : ""->] ""
a ->>] : ""->>] ""
a -\] : ""-\] ""
a -\\] : ""-\\\\]""
a -/] : ""-/] ""
a -//] : ""-//] ""
a ->x] : ""->x] ""
a x->] : ""x->] ""
a o->] : ""o->] ""
a ->o] : ""->o] ""
a o->o] : ""o->o] ""
a <->] : ""<->] ""
a o<->o] : ""o<->o]""
a x<->x] : ""x<->x]""
a ->>o] : ""->>o] ""
a -\o] : ""-\o] ""
a -\\o] : ""-\\\\o]""
a -/o] : ""-/o] ""
a -//o] : ""-//o] ""
a x->o] : ""x->o] ""
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a ->] : ""->] ""
a ->>] : ""->>] ""
a -\] : ""-\] ""
a -\\] : ""-\\\\]""
a -/] : ""-/] ""
a -//] : ""-//] ""
a ->x] : ""->x] ""
a x->] : ""x->] ""
a o->] : ""o->] ""
a ->o] : ""->o] ""
a o->o] : ""o->o] ""
a <->] : ""<->] ""
a o<->o] : ""o<->o]""
a x<->x] : ""x<->x]""
a ->>o] : ""->>o] ""
a -\o] : ""-\o] ""
a -\\o] : ""-\\\\o]""
a -/o] : ""-/o] ""
a -//o] : ""-//o] ""
a x->o] : ""x->o] ""
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
短进入信息和短发出信息(使用'?')
短进入信息(使用'?')
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
?-> b : ""?-> ""
?->> b : ""?->> ""
?-\ b : ""?-\ ""
?-\\ b : ""?-\\\\""
?-/ b : ""?-/ ""
?-// b : ""?-// ""
?->x b : ""?->x ""
?x-> b : ""?x-> ""
?o-> b : ""?o-> ""
?->o b : ""?->o ""
?o->o b : ""?o->o ""
?<-> b : ""?<-> ""
?o<->o b : ""?o<->o""
?x<->x b : ""?x<->x""
?->>o b : ""?->>o ""
?-\o b : ""?-\o ""
?-\\o b : ""?-\\\\o ""
?-/o b : ""?-/o ""
?-//o b : ""?-//o ""
?x->o b : ""?x->o ""
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
?-> b : ""?-> ""
?->> b : ""?->> ""
?-\ b : ""?-\ ""
?-\\ b : ""?-\\\\""
?-/ b : ""?-/ ""
?-// b : ""?-// ""
?->x b : ""?->x ""
?x-> b : ""?x-> ""
?o-> b : ""?o-> ""
?->o b : ""?->o ""
?o->o b : ""?o->o ""
?<-> b : ""?<-> ""
?o<->o b : ""?o<->o""
?x<->x b : ""?x<->x""
?->>o b : ""?->>o ""
?-\o b : ""?-\o ""
?-\\o b : ""?-\\\\o ""
?-/o b : ""?-/o ""
?-//o b : ""?-//o ""
?x->o b : ""?x->o ""
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
短发出信息(使用'?')
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
a ->? : ""->? ""
a ->>? : ""->>? ""
a -\? : ""-\? ""
a -\\? : ""-\\\\?""
a -/? : ""-/? ""
a -//? : ""-//? ""
a ->x? : ""->x? ""
a x->? : ""x->? ""
a o->? : ""o->? ""
a ->o? : ""->o? ""
a o->o? : ""o->o? ""
a <->? : ""<->? ""
a o<->o? : ""o<->o?""
a x<->x? : ""x<->x?""
a ->>o? : ""->>o? ""
a -\o? : ""-\o? ""
a -\\o? : ""-\\\\o?""
a -/o? : ""-/o? ""
a -//o? : ""-//o? ""
a x->o? : ""x->o? ""
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice as a
participant Bob as b
a -> b : //Long long label//
a ->? : ""->? ""
a ->>? : ""->>? ""
a -\? : ""-\? ""
a -\\? : ""-\\\\?""
a -/? : ""-/? ""
a -//? : ""-//? ""
a ->x? : ""->x? ""
a x->? : ""x->? ""
a o->? : ""o->? ""
a ->o? : ""->o? ""
a o->o? : ""o->o? ""
a <->? : ""<->? ""
a o<->o? : ""o<->o?""
a x<->x? : ""x<->x?""
a ->>o? : ""->>o? ""
a -\o? : ""-\o? ""
a -\\o? : ""-\\\\o?""
a -/o? : ""-/o? ""
a -//o? : ""-//o? ""
a x->o? : ""x->o? ""
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
特定外观参数
默认情况下
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
2
3
4
5
6
生命线策略
- nosolid 虚线 (默认情况)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam lifelineStrategy nosolid
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam lifelineStrategy nosolid
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
2
3
4
5
6
7
- solid 实线
在时序图中使用实线生命线:skinparam lifelineStrategy solid
@startuml
!include https://f.umlcn.com/c4.puml
skinparam lifelineStrategy solid
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam lifelineStrategy solid
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
2
3
4
5
6
7
style strictuml
为了符合严格UML的标准(线头的形状必须是三角形,而不能是箭头形),你可以使用:
skinparam style strictuml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam style strictuml
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam style strictuml
Bob -> Alice : hello
Alice -> Bob : ok
@enduml
2
3
4
5
6
7
隐藏孤立参与者
默认情况下会显示所有参与者。
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob
participant Carol
Alice -> Bob : hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
participant Alice
participant Bob
participant Carol
Alice -> Bob : hello
@enduml
2
3
4
5
6
7
8
9
可以使用hide unlinked
命令来隐藏未被链接到的参与者。
@startuml
!include https://f.umlcn.com/c4.puml
hide unlinked
participant Alice
participant Bob
participant Carol
Alice -> Bob : hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
hide unlinked
participant Alice
participant Bob
participant Carol
Alice -> Bob : hello
@enduml
2
3
4
5
6
7
8
9
10
给分组信息着色
可以给分组信息color[着色]。
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: Authentication Request
alt#Gold #LightBlue Successful case
Bob -> Alice: Authentication Accepted
else #Pink Failure
Bob -> Alice: Authentication Rejected
end
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Alice -> Bob: Authentication Request
alt#Gold #LightBlue Successful case
Bob -> Alice: Authentication Accepted
else #Pink Failure
Bob -> Alice: Authentication Rejected
end
@enduml
2
3
4
5
6
7
8
9
10
Mainframe
@startuml
!include https://f.umlcn.com/c4.puml
mainframe This is a **mainframe**
Alice->Bob : Hello
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
mainframe This is a **mainframe**
Alice->Bob : Hello
@enduml
2
3
4
5
6
Slanted or odd arrows
You can use the (nn)
option (before or after arrow) to make the arrows slanted, where nn is the number of shift pixels.
[Available only after v1.2022.6beta+]
@startuml
!include https://f.umlcn.com/c4.puml
A ->(10) B: text 10
B ->(10) A: text 10
A ->(10) B: text 10
A (10)<- B: text 10
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
A ->(10) B: text 10
B ->(10) A: text 10
A ->(10) B: text 10
A (10)<- B: text 10
@enduml
2
3
4
5
6
7
8
9
@startuml
!include https://f.umlcn.com/c4.puml
A ->(40) B++: Rq
B -->(20) A--: Rs
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
A ->(40) B++: Rq
B -->(20) A--: Rs
@enduml
2
3
4
5
6
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
A ->(50) C: Starts\nwhen 'B' sends
& B ->(25) C: \nBut B's message\n arrives before A's
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
A ->(50) C: Starts\nwhen 'B' sends
& B ->(25) C: \nBut B's message\n arrives before A's
@enduml
2
3
4
5
6
7
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
S1 ->(30) S2: msg 1\n
& S2 ->(30) S1: msg 2
note left S1: msg\nS2 to S1
& note right S2: msg\nS1 to S2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
S1 ->(30) S2: msg 1\n
& S2 ->(30) S1: msg 2
note left S1: msg\nS2 to S1
& note right S2: msg\nS1 to S2
@enduml
2
3
4
5
6
7
8
9
10
11
Parallel messages (with teoz)
You can use the &
teoz command to display parallel messages:
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
Alice -> Bob : hello
& Bob -> Charlie : hi
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma teoz true
Alice -> Bob : hello
& Bob -> Charlie : hi
@enduml
2
3
4
5
6
7