写 PlantUML 时有一些「每次都要查文档才能想起来」的小技巧。这篇按小功能维度整理 30 条,让你在写图时少走弯路。
1. 节点换行
1 2 3 4 5 6
| @startuml class "Class\nName" as c1 class Class<< Long>> as c2 note right of c1 : 多行注释\n也能换行 @enduml
|
\n 在名字和注释里都生效。
2. 用 unicode 字符
1 2 3 4 5 6 7 8
| @startuml note right ✓ Yes ✗ No → Next ⏵ Play end note @enduml
|
emoji / 字符在标签里直接写。
3. 箭头方向左右翻转
1 2 3 4 5 6
| @startuml A -right-> B : 默认向右 A -left-> C : 向左 A -up-> D : 向上 A -down-> E : 向下 @enduml
|
-right-> / -left-> / -up-> / -down-> 控制箭头方向。
4. 自定义箭头颜色
1 2 3 4 5
| @startuml A -[#A31F34]-> B : 红色 A -[#2F4858]-> C : 深蓝 A -[#20A464]-> D : 绿色 @enduml
|
-[#hex]-> 给箭头染色。
5. 箭头粗细
1 2 3 4 5
| @startuml A -> B : 默认 A ->[thickness=2] C : 粗线 A ->[thickness=4] D : 更粗 @enduml
|
6. 标签带样式
1 2 3 4 5
| @startuml A -[#A31F34,bold]-> B : 红色加粗 A -[#2F4858,dashed]-> C : 蓝虚线 A -[bold,dashed]-> D : 加粗虚线 @enduml
|
bold / dashed / dotted 修饰箭头样式。
7. 节点颜色
1 2 3 4 5 6 7 8
| @startuml class A #A31F34 class B #FAFAFA/A31F34 : 背景/边框 class C #FAFAFA { Field1 Field2 } @enduml
|
#background 或 #background/border 直接赋色。
8. 节点分组
1 2 3 4 5 6 7 8 9 10 11 12
| @startuml package "前端" { class A class B } package "后端" { class C class D } A --> C B --> D @enduml
|
package 自动布局。
9. 注释 note
1 2 3 4 5 6 7 8 9 10 11 12
| @startuml
A note left: 左侧的注释 note right: 右侧的注释 note top: 顶部 note bottom: 底部
A .. B : 连接 note over A, B : 跨 A、B 的注释
@enduml
|
note over A, B 是放在 AB 之间的水平注释。
10. 注释块(多行)
1 2 3 4 5 6 7 8 9 10 11
| @startuml
class A
note left of A 这是多行注释 第二行 第三行 end note
@enduml
|
note ... end note 内多行。
11. 控制全局元素颜色
1 2 3 4 5 6 7 8
| @startuml skinparam class { BackgroundColor #FAFAFA BorderColor #2F4858 FontColor #1F2328 ArrowColor #2F4858 } @enduml
|
skinparam 影响整张图的风格。
12. 隐藏 stereotype 圆角图标
1 2 3 4 5 6
| @startuml hide circle class A class B A --> B @enduml
|
hide circle 隐藏关系线上的小圆圈(默认 UML 是有圆圈表示接口实现的)。
13. 在类图里放 emoji
1 2 3 4 5
| @startuml class "🚀 Rocket" as r class "🐛 Bug" as b r --> b : 发布 @enduml
|
emoji 直接放在类名里。
14. 隐藏字段或方法
1 2 3 4 5 6 7 8 9 10
| @startuml class User { +name: String +email: String {field} -id: Long -- {method} +login() {method} +logout() } @enduml
|
{field} / {method} 分组,让分类显示更清晰。
15. UML 关键字识别
1 2 3 4 5 6 7
| @startuml class Foo <<(M, #A31F34) Magic>> class Bar <<service>> abstract class Abs interface Inf
@enduml
|
<<type>> 自定义 stereotype。
16. 类的「template」版
1 2 3 4 5
| @startuml class Foo<T>
note right : 泛型类\nFoo<T> @enduml
|
<> 表达泛型类。
17. 数组 / List / Map 类型
1 2 3 4 5 6 7
| @startuml class User { +roles: List<String> +scores: Map<String, Integer> +data: byte[] } @enduml
|
泛型类型直接写在冒号后。
18. 注解
1 2 3 4 5 6 7
| @startuml class Foo
note top of Foo : 注释 note top of Foo #FAFAFA : 浅色注释
@enduml
|
注释支持颜色。
19. 时间线方向
1 2 3 4 5 6 7 8 9
| @startuml hide footbox title 时间线方向
a -> b : right b --> c : right c <-- d : left
@enduml
|
hide footbox 隐藏底部时间标。
20. 时序图 Box 分组
1 2 3 4 5 6 7 8 9 10 11 12
| @startuml box "客户端" #FAFAFA participant Browser end box box "服务端" #FFFFFF participant API participant DB end box
Browser -> API API -> DB @enduml
|
box 给 lifeline 划组,可加颜色。
21. 拆分 / 缩放
1 2 3 4 5
| @startuml scale 2 class A class B @enduml
|
scale 2 整图放大 2 倍。
22. 嵌套子图
1 2 3 4 5 6 7 8 9 10
| @startuml
class Order
state New { * -> Created Created -> Confirmed }
@enduml
|
状态图的子图;在 PlantUML 中通过 state X { ... } 表达。
23. 接口虚线箭头
1 2 3 4 5 6 7 8
| @startuml
interface ILoggable class Service
Service ..|> ILoggable
@enduml
|
实现用 ..|>(虚线 + 空心三角)。
24. 字符转义
1 2 3 4
| @startuml class "Class\#1" as c1 note "含 # \\ 字符" as n @enduml
|
\\ 转义反斜杠;某些 # 也可能要转义。
25. 网格背景
1 2 3 4 5 6 7 8 9 10
| @startuml skinparam { Shadowing false RoundCorner 0 DefaultFontName "Menlo" }
class A A -> B @enduml
|
调出「架构图」风格的 grid 背景需要样式:
1 2 3 4 5 6 7 8 9 10
| skinparam { Shadowing false RoundCorner 0 DefaultFontName "Menlo" BackgroundColor #FAFAFA }
class A A ..> B : dotted @enduml
|
26. 直角连线
1 2 3 4 5 6
| @startuml skinparam linetype ortho A -> B B -> C C -> D @enduml
|
linetype ortho 强制连线走直角(不斜)。
27. 文字换行手动
1 2 3
| @startuml class "非常\n长的\n类名" as c1 @enduml
|
\n 转义换行。
28. 隐藏 stereotype
1 2 3 4 5 6
| @startuml hide stereotype class A <<service>> class B <<controller>> A -> B @enduml
|
29. 类图自动布局方向
1 2 3 4 5 6 7 8
| @startuml left to right direction
class A class B class C A --> B --> C @enduml
|
top to bottom direction / left to right direction 控制整体布局方向。
1 2 3 4 5
| @startuml hide footer hide << * >> class A @enduml
|
只控制输出的细节部分渲染。
评审 checklist
一句话总结
PlantUML 官方语法很多,30 个最常用的小技巧能让日常写图效率翻倍 —— 这些是评审里经常被忽略的细节。