PlantUML定时图
-
- 1、声明参与者
- 2、二进制及时钟信号
- 3、增加标示
- 4、相对时间
- 5、锚点
- 6、参与者
- 7、设置缩放
- 8、初始状态
- 9、模糊状态
- 10、隐藏状态
- 11、隐藏时间轴
- 12、使用日期
- [13、Change Date Format](#13、Change Date Format)
- [14、Manage time axis labels](#14、Manage time axis labels)
-
- [14.1、Label on each tick (by default)](#14.1、Label on each tick (by default))
- [14.2、Manual label (only when the state changes)](#14.2、Manual label (only when the state changes))
- 15、添加约束
-
- [15.1、= 设置高亮](#15.1、= 设置高亮)
- 16、添加注释
- 17、添加文本
- 18、完整样例
- 19、数据表示样例
- 20、颜色
- 21、使用(全局)样式
- 22、对特定行着色
UML 中的时序图是一种特定类型的交互图,可视化系统的时序约束。它侧重于事件的时间顺序,展示不
同对象如何随着时间的推移相互影响。定时图在实时系统和嵌入式系统中特别有用,可用于了解对象在
整个给定时间段内的行为。
1、声明参与者
使用以下关键字声明参与者, 可根据需要选择显示样式。

通过 @ 标注, 和 is 动词定义状态.
js
@startuml
robust "Web 浏览器" as WB
concise "Web 用户" as WU
@0
WU is 空闲
WB is 空闲
@100
WU is 等待中
WB is 处理中
@300
WB is 等待中
@enduml

js
@startuml
clock "Clock_0" as C0 with period 50
clock "Clock_1" as C1 with period 50 pulse 15 offset 10
binary "Binary" as B
concise "Concise" as C
robust "Robust" as R
analog "Analog" as A
@0
C is Idle
R is Idle
A is 0
@100
B is high
C is Waiting
R is Processing
A is 3
@300
R is Waiting
A is 1
@enduml

2、二进制及时钟信号
使用以下关键字可以绘制二进制及时钟信号:
- binary
- clock
js
@startuml
clock clk with period 1
binary "Enable" as EN
@0
EN is low
@5
EN is high
@10
EN is low
@enduml

3、增加标示
使用下述的语法,增加对某一时刻信号变动的描述。
js
@startuml
robust "Web 浏览器" as WB
concise "Web 用户" as WU
@0
WU is 空闲
WB is 空闲
@100
WU -> WB : URL
WU is 等待中
WB is 处理中
@300
WB is 等待中
@enduml

4、相对时间
可以使用 @+ 和 @- 符号表示相对时间.
js
@startuml
robust "DNS Resolver" as DNS
robust "Web Browser" as WB
concise "Web User" as WU
@0
WU is Idle
WB is Idle
DNS is Idle
@+100
WU -> WB : URL
WU is Waiting
WB is Processing
@+200
WB is Waiting
WB -> DNS@+50 : Resolve URL
@+100
DNS is Processing
@+300
DNS is Idle
@enduml

5、锚点
在绝对时间上使用绝对时间或相对时间,你可以通过使用 as 关键字和以: 开始的名称来定义一个时间作为锚点。
@XX as :<anchor point name>
js
@startuml
clock clk with period 1
binary "enable" as EN
concise "dataBus" as db
@0 as :start
@5 as :en_high
@10 as :en_low
@:en_high-2 as :en_highMinus2
@:start
EN is low
db is "0x0000"
@:en_high
EN is high
@:en_low
EN is low
@:en_highMinus2
db is "0xf23a"
@:en_high+6
db is "0x0000"
@enduml

6、参与者
按时间顺序来描述状态变化可能会有些麻烦,不如将每个参与者的变化放在一起。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
@WB
0 is idle
+200 is Proc.
+100 is Waiting
@WU
0 is Waiting
+500 is ok
@enduml

7、设置缩放
还可以设置缩放比例。
js
@startuml
concise "Web User" as WU
scale 100 as 50 pixels
@WU
0 is Waiting
+500 is ok
@enduml

当使用绝对时间或者日期时,1 单位为 1 秒。
js
@startuml
concise "季节" as S
'这里将30天缩放到50像素
scale 2592000 as 50 pixels
@2000/11/01
S is "冬"
@2001/02/01
S is "春"
@2001/05/01
S is "夏"
@2001/08/01
S is "秋"
@enduml

8、初始状态
可以定义初始状态。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
WB is Initializing
WU is Absent
@WB
0 is idle
+200 is Processing
+100 is Waiting
@WU
0 is Waiting
+500 is ok
@enduml

9、模糊状态
一个信号在某个时段可能是模糊的,有多个可能的状态区间。
9.1、模糊或者未定义鲁棒状态信号
js
@startuml
robust "Signal1" as S1
robust "Signal2" as S2
S1 has 0,1,2,hello
S2 has 0,1,2
@0
S1 is 0
S2 is 0
@100
S1 is {0,1} #SlateGrey
S2 is {0,1}
@200
S1 is 1
S2 is 0
@300
S1 is hello
S2 is {0,2}
@enduml

9.2、模糊或者未定义二进制信号
js
@startuml
clock "Clock" as C with period 2
binary "Enable" as EN
@0
EN is low
@1
EN is high
@3
EN is low
@5
EN is {low,high}
@10
EN is low
@enduml

10、隐藏状态
可以隐藏某个时段的状态。
js
@startuml
concise "Web User" as WU
@0
WU is {-}
@100
WU is A1
@200
WU is {-}
@300
WU is {hidden}
@400
WU is A3
@500
WU is {-}
@enduml

js
@startuml
scale 1 as 50 pixels
concise state0
concise substate1
robust bit2
bit2 has HIGH,LOW
@state0
0 is 18_start
6 is s_dPause
8 is 10_data
14 is {hidden}
@substate1
0 is sSeq
4 is sPause
6 is {hidden}
8 is dSeq
12 is dPause
14 is {hidden}
@bit2
0 is HIGH
2 is LOW
4 is {hidden}
8 is HIGH
10 is LOW
12 is {hidden}
@enduml

11、隐藏时间轴
可以隐藏时间轴。
js
@startuml
hide time-axis
concise "Web User" as WU
WU is Absent
@WU
0 is Waiting
+500 is ok
@enduml

12、使用日期
时间轴除时间以外也可以改用日期表示。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
@2019/07/02
WU is Idle
WB is Idle
@2019/07/04
WU is Waiting : some note
WB is Processing : some other note
@2019/07/05
WB is Waiting
@enduml

js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
@1:15:00
WU is Idle
WB is Idle
@1:16:30
WU is Waiting : some note
WB is Processing : some other note
@1:17:30
WB is Waiting
@enduml

13、Change Date Format
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
use date format "YY-MM-dd"
@2019/07/02
WU is Idle
WB is Idle
@2019/07/04
WU is Waiting : some note
WB is Processing : some other note
@2019/07/05
WB is Waiting
@enduml

14、Manage time axis labels
14.1、Label on each tick (by default)
js
@startuml
scale 31536000 as 40 pixels
use date format "yy-MM"
concise "OpenGL Desktop" as OD
@1992/01/01
OD is {hidden}
@1992/06/30
OD is 1.0
@1997/03/04
OD is 1.1
@1998/03/16
OD is 1.2
@2001/08/14
OD is 1.3
@2004/09/07
OD is 3.0
@2008/08/01
OD is 3.0
@2017/07/31
OD is 4.6
@enduml

14.2、Manual label (only when the state changes)
js
@startuml
scale 31536000 as 40 pixels
manual time-axis
use date format "yy-MM"
concise "OpenGL Desktop" as OD
@1992/01/01
OD is {hidden}
@1992/06/30
OD is 1.0
@1997/03/04
OD is 1.1
@1998/03/16
OD is 1.2
@2001/08/14
OD is 1.3
@2004/09/07
OD is 3.0
@2008/08/01
OD is 3.0
@2017/07/31
OD is 4.6
@enduml

15、添加约束
可以在图上标示时间约束。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
WB is Initializing
WU is Absent
@WB
0 is idle
+200 is Processing
+100 is Waiting
WB@0 <-> @50 : {50 ms lag}
@WU
0 is Waiting
+500 is ok
@200 <-> @+150 : {150 ms}
@enduml

15.1、= 设置高亮
可以给图表中的某一时段设置高亮。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
@0
WU is Idle
WB is Idle
@100
WU -> WB : URL
WU is Waiting #LightCyan;line:Aqua
@200
WB is Proc.
@300
WU -> WB@350 : URL2
WB is Waiting
@+200
WU is ok
@+200
WB is Idle
highlight 200 to 450 #Gold;line:DimGrey : This is my caption
@enduml

16、添加注释
可以使用 note top of 或 note bottom of 关键字在某一时刻或参与者的上方或下方添加注释 (只可以在 concise 类型的参与者中使用)。
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
@0
WU is Idle
WB is Idle
@100
WU is Waiting
WB is Processing
note top of WU : first note\non several\nlines
note bottom of WU : second note\non several\nlines
@300
WB is Waiting
@enduml

17、添加文本
可以选择添加标题、页眉、页脚、图例和说明。
js
@startuml
Title Some title
header: Some header
footer: Some footer
legend
Some legend
end legend
caption Some caption
robust "Web Browser" as WB
concise "Web User" as WU
@0
WU is Idle
WB is Idle
@100
WU is Waiting
WB is Processing
@300
WB is Waiting
@enduml

18、完整样例
js
@startuml
concise "Client" as Client
concise "Server" as Server
concise "Response freshness" as Cache
Server is idle
Client is idle
@Client
0 is send
Client -> Server@+25 : GET
+25 is await
+75 is recv
+25 is idle
+25 is send
Client -> Server@+25 : GET\nIf-Modified-Since: 150
+25 is await
+50 is recv
+25 is idle
@100 <-> @275 : no need to re-request from server
@Server
25 is recv
+25 is work
+25 is send
Server -> Client@+25 : 200 OK\nExpires: 275
+25 is idle
+75 is recv
+25 is send
Server -> Client@+25 : 304 Not Modified
+25 is idle
@Cache
75 is fresh
+200 is stale
@enduml

19、数据表示样例
js
@startuml
scale 5 as 150 pixels
clock clk with period 1
binary "enable" as en
binary "R/W" as rw
binary "data Valid" as dv
concise "dataBus" as db
concise "address bus" as addr
@6 as :write_beg
@10 as :write_end
@15 as :read_beg
@19 as :read_end
@0
en is low
db is "0x0"
addr is "0x03f"
rw is low
dv is 0
@:write_beg-3
en is high
@:write_beg-2
db is "0xDEADBEEF"
@:write_beg-1
dv is 1
@:write_beg
rw is high
@:write_end
rw is low
dv is low
@:write_end+1
rw is low
db is "0x0"
addr is "0x23"
@12
dv is high
@13
db is "0xFFFF"
@20
en is low
dv is low
@21
db is "0x0"
highlight :write_beg to :write_end #Gold:Write
highlight :read_beg to :read_end #lightBlue:Read
db@:write_beg-1 <-> @:write_end : setup time
db@:write_beg-1 -> addr@:write_end+1 : hold
@enduml

20、颜色
可以为图表添加颜色。
js
@startuml
concise "LR" as LR
concise "ST" as ST
LR is AtPlace #palegreen
ST is AtLoad #gray
@LR
0 is Lowering
100 is Lowered #pink
350 is Releasing
@ST
200 is Moving
@enduml

21、使用(全局)样式
21.1、无样式(默认样式)
js
@startuml
robust "Web Browser" as WB
concise "Web User" as WU
WB is Initializing
WU is Absent
@WB
0 is idle
+200 is Processing
+100 is Waiting
WB@0 <-> @50 : {50 ms lag}
@WU
0 is Waiting
+500 is ok
@200 <-> @+150 : {150 ms}
@enduml

21.2、设置样式
可以使用样式来改变元素的渲染
js
@startuml
<style>
timingDiagram {
document {
BackGroundColor SandyBrown
}
constraintArrow {
LineStyle 2-1
LineThickness 3
LineColor Blue
}
}
</style>
robust "Web Browser" as WB
concise "Web User" as WU
WB is Initializing
WU is Absent
@WB
0 is idle
+200 is Processing
+100 is Waiting
WB@0 <-> @50 : {50 ms lag}
@WU
0 is Waiting
+500 is ok
@200 <-> @+150 : {150 ms}
@enduml

22、对特定行着色
可以使用 <style> 标记和模板将行属性命名
js
@startuml
<style>
timingDiagram {
.red {
LineColor red
}
.blue {
LineColor blue
LineThickness 5
}
}
</style>
clock clk with period 1
binary "Input Signal 1" as IS1
binary "Input Signal 2" as IS2 <<blue>>
binary "Output Signal 1" as OS1 <<red>>
@0
IS1 is low
IS2 is high
OS1 is low
@2
OS1 is high
@4
OS1 is low
@5
IS1 is high
OS1 is high
@6
IS2 is low
@10
IS1 is low
OS1 is low
@enduml
