主题
用例图 (12.0.0+)
🌐 Use case diagrams (12.0.0+)
用例图显示了参与者如何与系统及其用例进行交互。用 usecase-beta 关键字开始绘制图表。每个语句都单独放在一行上。
🌐 Use case diagrams show how actors interact with a system and its use cases. Start a diagram with the usecase-beta keyword. Put each statement on its own physical line.
在 UML 文本中,用例是两个词。Mermaid 的精确语法关键字是单个标记 usecase-beta;这个页面在其他地方都使用两个词的形式。
🌐 In UML prose, use case is two words. Mermaid's exact syntax keyword is the single token usecase-beta; this page uses the two-word form everywhere else.
代码:
mermaid
使用 direction 搭配 TD、TB、BT、LR 或 RL 来选择布局方向。
🌐 Use direction with TD, TB, BT, LR, or RL to choose the layout direction.
默认主题、外观和布局(v12.0.0+)
🌐 Default theme, look and layout (v12.0.0+)
用例图默认使用 redux-color 主题和 neo 外观,并且由 ELK 而不是 Dagre 布局。不是每种图表类型都是这样——具体请看 每图表默认设置 的列表,以及 Mermaid 决定的顺序。
🌐 Use case diagrams use the redux-color theme and the neo look by default, and are laid out by ELK rather than Dagre. Not every diagram type does — see Per-diagram defaults for the list and for the order in which Mermaid decides.
同一个图,画法两种:
🌐 The same diagram, drawn both ways:
使用默认设置
🌐 With the defaults
代码:
mermaid
之前的样子
🌐 The previous appearance
两者只是默认设置,所以你自己设置的任何东西都会生效。在图表的前言中命名之前的主题和样式会像以前 Mermaid 那样显示出来:
🌐 Both are only defaults, so anything you set yourself wins. Naming the previous theme and look in a diagram's front matter draws it the way Mermaid did before:
代码:
mermaid
把同样的三个键传给 mermaid.initialize(),页面上的每个图表都会生效,而把主题和外观限定到一种图表类型 —— mermaid.initialize({ layout: 'dagre', usecase: { theme: 'default', look: 'classic' } }) —— 则只对这种类型生效。layout 是顶层选项,所以它对每个图表都适用。
🌐 Passing the same three keys to mermaid.initialize() does it for every diagram on the page, and scoping the theme and look to one diagram type — mermaid.initialize({ layout: 'dagre', usecase: { theme: 'default', look: 'classic' } }) — does it for that type alone. layout is a top-level option, so it applies to every diagram.
参与者和用例
🌐 Actors and use cases
演员和用例标识符匹配 [A-Za-z0-9_]+,所以它们可以以数字开头——1、1mg 和 3rd 都是有效的。标识符在整个图中都是唯一的,并且在演员、用例、边界、JSON 节点和显式边缘之间共享。
🌐 Actor and use case identifiers match [A-Za-z0-9_]+, so they may start with a digit — 1, 1mg, and 3rd are all valid. Identifiers are diagram-wide and are shared by actors, use cases, boundaries, JSON nodes, and explicit edges.
一个裸演员使用它的标识作为其标签。演员也可以有一个明确的标识符和显示标签。用例使用圆括号表示椭圆,方括号表示矩形。
🌐 A bare actor uses its identifier as its label. An actor can also have an explicit identifier and display label. Use cases use parentheses for an ellipse and square brackets for a rectangle.
代码:
mermaid
Login("Sign in") 有稳定标识符 Login 和显示标签 Sign in。像 "Reset password" 这样的引用声明会通过将每个非单词字符替换为 _ 来获得确定性标识符。当其他语句需要可读的、稳定的引用时,请使用明确的标识符。
关系端点可以在其声明之前出现。最终的声明顺序不会影响解析的类型、形状或标签。没有显式声明的端点会成为省略用例。参与者从不通过它们在关系中的位置来推断,需要在图中的某个地方有 actor 声明。
🌐 A relationship endpoint can appear before its declaration. Final declaration order does not affect the resolved kind, shape, or label. An endpoint that has no explicit declaration becomes an ellipse use case. Actors are never inferred from their position in a relationship and require an actor declaration somewhere in the diagram.
演员变体
🌐 Actor variants
演员支持四种变体。普通的杆状演员是默认的。将 type 设置为 hollow 或 awesome,即可使用其他内置变体。将 icon 设置为图标名称,即可使用图标演员。
🌐 Actors support four variants. The normal stick actor is the default. Set type to hollow or awesome for the other built-in variants. Set icon to an icon name for an icon actor.
代码:
mermaid
图标组件使用 Mermaid 的图标注册表。在使用该图标包中的图标名称前先注册所需的图标包。如果图标不可用,Mermaid 会渲染标准的未知图标替代,而不会让图表出错。
🌐 Icon actors use Mermaid's icon registry. Register the required icon pack before using an icon name from that pack. If an icon is not available, Mermaid renders the standard unknown-icon fallback instead of failing the diagram.
演员元数据只接受 type、icon 和 business。图标不能与非正常的 type 或 business: true 结合使用。
🌐 Actor metadata accepts only type, icon, and business. An icon cannot be combined with a non-normal type or with business: true.
商业元素与刻板印象
🌐 Business elements and stereotypes
将 business: true 设置为给普通或空心演员或椭圆形用例添加常规业务斜线。矩形用例、超棒演员和图标演员不能作为业务元素。
🌐 Set business: true to add the conventional business slash to a normal or hollow actor or to an ellipse use case. Rectangular use cases, awesome actors, and icon actors cannot be business elements.
一个演员或用例可以有一个可见的刻板印象。在元数据之后、::: 类后缀之前放置 <<...>>。刻板印象保持其大小写,并显示在主标签上方。
代码:
mermaid
刻板印象提供可见的语义,但不会创建 CSS 类或自动刻板印象选择器。使用类或直接样式来控制它们的外观。
🌐 Stereotypes provide visible semantics but do not create CSS classes or automatic stereotype selectors. Use classes or direct styles to control their appearance.
标签
🌐 Labels
标签可以是括号或方括号内的不加引号的文本、单行纯文本字符串,或者是 Mermaid Markdown 字符串。相同形式也适用于参与者标签、用例标签、边界标题、关系标签和备注。
🌐 Labels can be unquoted text inside parentheses or brackets, a single-line plain string, or a Mermaid Markdown string. The same forms apply to actor labels, use case labels, boundary titles, relationship labels, and notes.
未加引号的标签会直接按照源文本来处理,所以它可以包含数字、Markdown 标记、标点符号和非 ASCII 字符。它必须保持在一行内,并且开头和结尾的空格会被去掉。只有语法已经保留的东西例外:分隔符 (、)、[、]、{ 和 },引号字符 " 和 ',以及序列 --、-->、<--、--o、--x、--|>、..>、:::、@{ 和 <<。单独的 -、.、:、@、< 或 > 都是普通文本。如果标签中必须出现保留字符,就要加引号。
🌐 An unquoted label is taken literally from the source, so it can hold digits, Markdown markers, punctuation, and non-ASCII characters. It has to stay on one physical line, and leading and trailing spaces are dropped. Only what the syntax already reserves stays out: the delimiters (, ), [, ], {, and }, the quote characters " and ', and the sequences --, -->, <--, --o, --x, --|>, ..>, :::, @{, and <<. A lone -, ., :, @, <, or > is ordinary text. Quote the label when a reserved character has to appear in it.
代码:
mermaid
普通字符串仍然是纯文本。普通字符串中的 Markdown 标记会被直接显示。Mermaid Markdown 字符串使用一对外层双引号和一对内层反引号。Markdown 字符串可以包含实际的换行符。
🌐 Plain strings remain plain text. Markdown markers in a plain string are displayed literally. Mermaid Markdown strings use an outer pair of double quotes and an inner pair of backticks. Markdown strings can contain physical newlines.
代码:
mermaid
当标签中必须出现保留字符时,请使用 Mermaid 实体代码。常见的代码包括 #quot;、#39;、#40;、#41;、#91;、#93; 和 #96;。普通字符串不会处理反斜杠转义。例如,"First\nSecond" 会显示反斜杠和 n;如果想要多行内容,可以在 Markdown 字符串中使用实际换行。
🌐 Use Mermaid entity codes when a reserved character must appear in a label. Common codes include #quot;, #39;, #40;, #41;, #91;, #93;, and #96;. Plain strings do not process backslash escapes. For example, "First\nSecond" displays the backslash and n; use a physical newline inside a Markdown string for multiline content.
行和注释
🌐 Lines and comments
每个语句占据一行,除非它包含 Markdown 字符串、多行元数据块、JSON 对象、辅助功能描述块或系统边界块。最后一个语句不需要结尾换行。
🌐 Each statement occupies one physical line unless it contains a Markdown string, multiline metadata block, JSON object, accessibility description block, or system boundary block. The final statement does not need a trailing newline.
在可选的缩进之后,%% 开始整行注释。空行和注释在公共 AST 中保持它们在源代码中的顺序。
🌐 After optional indentation, %% starts a whole-line comment. Blank lines and comments retain their source order in the public AST.
代码:
mermaid
%% 在字符串中是标签数据。// 和 # 不是注释。分号不是语句分隔符。同一物理行中完整语句之后的任何标记都是错误。
系统边界
🌐 System boundaries
systemBoundary 块用于分组参与者和用例声明。给边界一个简短的标识符,然后是可选的显示标题,再接着是可选的元数据和类。后缀按 systemBoundary <id><title>@{ … }:::<classes> 顺序排列,与参与者和用例使用的顺序相同。
代码:
mermaid
标题接受与用例相同的标签形式——未加引号的文本、普通字符串或 Markdown 字符串——可以放在 […] 或 (…) 中。与用例不同的是,括号形式不会选择形状:边界几何仅来自 type 元数据。
🌐 Titles accept the same label forms as use cases — unquoted text, a plain string, or a Markdown string — inside either […] or (…). Unlike a use case, the bracket form does not select a shape: boundary geometry comes from the type metadata only.
一个用引号标题声明但没有标识符的边界会获得一个确定性的标识符。下面,标题 Payment service 生成了标识符 Payment_service,然后单独的元数据声明就针对它了。
🌐 A boundary declared with a quoted title and no identifier gets a deterministic one instead. Below, the title Payment service produces the identifier Payment_service, which the separate metadata statement then targets.
代码:
mermaid
默认的边界类型是 rect。设置 type: package,可以在内联或后续的元数据语句中渲染包标题标签。后续语句会覆盖针对同一个键的内联元数据。边界 ID 是全图的,而它们的标题则是显示标签。
🌐 The default boundary type is rect. Set type: package, inline or in a later metadata statement, to render a package title tab. A later statement overrides inline metadata for the same key. Boundary IDs are diagram-wide, while their titles are display labels.
边界只有一层。它们的内容只能包含参与者和用例声明、空行,以及 %% 注释。关系、备注、JSON 节点、方向说明、类和样式说明、边界元数据以及嵌套边界仍然位于顶层。一个参与者或用例最多只能属于一个边界。被包含的元素可以被顶层的关系、备注、类和样式引用。
🌐 Boundaries are one level deep. Their contents can contain only actor and use case declarations, blank lines, and %% comments. Relationships, notes, JSON nodes, direction statements, class and style statements, boundary metadata, and nested boundaries remain at the top level. An actor or use case can belong to at most one boundary. Contained elements can be referenced by top-level relationships, notes, classes, and styles.
人际关系
🌐 Relationships
关联
🌐 Associations
支持七种实心关联操作符。标记的方向和类型会被保留。
🌐 Seven solid association operators are supported. The marker direction and type are preserved.
代码:
mermaid
稳定的关联可以有一个标签。包含 include 或 extend 的标签仍然是普通关联。
🌐 Solid associations can have a label. A label containing include or extend is still an ordinary association.
代码:
mermaid
JSON 节点只能使用点、反点或无标记的实线关联。圆形、叉形、包含、扩展和泛化关系不能连接到 JSON 节点。
🌐 JSON nodes can use only point, reversed-point, or markerless solid associations. Circle, cross, include, extend, and generalization relationships cannot connect to JSON nodes.
包含、扩展和泛化
🌐 Include, extend, and generalization
在UML关系语义中使用明确的操作符。
🌐 Use explicit operators for UML relationship semantics.
代码:
mermaid
对于 include,源用例包含目标用例。对于 extend,源用例扩展目标基本用例。泛化是指从特化的参与者或用例指向通用的参与者或用例。Include 和 extend 需要用例端点。泛化需要两个参与者或两个用例。
🌐 For include, the source use case includes the target use case. For extend, the source use case extends the target base use case. Generalization points from the specialized actor or use case to the general actor or use case. Include and extend require use case endpoints. Generalization requires two actors or two use cases.
边缘 ID、样式、动画和长度
🌐 Edge IDs, styles, animation, and length
在操作符前立即放置一个显式边 ID,后跟 @。显式边 ID 共享整个图表的 ID 命名空间,并且可以被 class、style 和元数据语句引用。
🌐 Place an explicit edge ID followed by @ immediately before an operator. Explicit edge IDs share the diagram-wide ID namespace and can be targeted by class, style, and metadata statements.
代码:
mermaid
匿名边会获得内部 ID,但无法通过 ID 进行样式设置或配置。使用 animate: true 设置默认速度,使用 animation: fast 或 animation: slow 选择速度并启用动画,使用 animate: false 关闭动画。
🌐 Anonymous edges receive internal IDs but cannot be styled or configured by ID. Use animate: true for the default speed, animation: fast or animation: slow to select a speed and enable animation, and animate: false to disable it.
额外的短横线会在点、反向点和无标记的实体关系上要求更长的最小布局长度。两条短横线形式表示 minlen = 1;三条短横线表示 minlen = 2,每增加一条短横线就多加一。比如,A --> B、A ---> B 和 A ----> B 依次要求更长的边。在有标签的关系中,把额外的短横线放在标签的右侧,如 A -- "longer" ----> B。圆形、叉形、包含、扩展和泛化操作符长度固定,不接受额外短横线。
🌐 Extra dashes request greater minimum layout length on point, reversed-point, and markerless solid associations. The two-dash form has minlen = 1; three dashes give minlen = 2, and each additional dash adds one. For example, A --> B, A ---> B, and A ----> B request progressively longer edges. In a labelled association, put the extra dashes on the right side of the label, as in A -- "longer" ----> B. Circle, cross, include, extend, and generalization operators have fixed length and reject extra dashes.
注意
🌐 Notes
一个注释附加到一个参与者或用例上。目标可以在注释之后声明。注释使用共享的注释形状,即主题注释颜色的普通矩形,并通过一条点状、无标记的线连接到它的目标。
🌐 A note attaches to one actor or use case. The target can be declared after the note. A note uses the shared note shape, a plain rectangle in the theme note colors, and is connected to its target by a dotted, markerless line.
注释是图中的一个普通节点。布局算法会把它和参与者及用例一起排列,所以注释相对于其目标没有固定的位置:它的位置会随着图的方向和其他布局元素而变化,添加一个注释可能会移动其他元素。
🌐 A note is an ordinary node in the graph. The layout algorithm places it together with the actors and use cases, so a note has no fixed side relative to its target: its position follows the diagram direction and the rest of the layout, and adding a note can move the other elements.
代码:
mermaid
注意不能针对 JSON 节点、边界、边或其他注意。注意没有位置关键字、别名、直接链接、独立形式或多个附件。
🌐 Notes cannot target JSON nodes, boundaries, edges, or other notes. Notes do not have placement keywords, aliases, direct links, standalone forms, or multiple attachments.
JSON 表格
🌐 JSON tables
一个顶层的 JSON 声明会创建一个表格,其标题就是它的标识符。它的主体必须是一个严格的 JSON 对象。
🌐 A top-level JSON declaration creates a table whose title is its identifier. Its body must be a strict JSON object.
代码:
mermaid
对象属性按源顺序出现,包括类似整数的键。数组保持索引顺序。嵌套的叶子节点使用像 address.city 和 items[0].name 这样的路径。空对象和数组占一行。如果一个 JSON 对象重复了某个属性,以最后一个值为准,但保留该属性的第一个源位置。字符串显示时不加 JSON 引号;数字、布尔值和 null 使用 JSON 拼写。
🌐 Object properties appear in source order, including integer-like keys. Arrays keep index order. Nested leaves use paths such as address.city and items[0].name. Empty objects and arrays get one row. If a JSON object repeats a property, the last value wins while the property's first source position is retained. Strings render without JSON quote marks; numbers, booleans, and null use JSON spelling.
JSON 声明必须保持在顶层。数组和标量值不能作为根。JSON 字符串和表格单元格使用共享的文本清理路径。
🌐 JSON declarations must stay at the top level. Arrays and scalar values are not valid roots. JSON strings and table cells use the shared text sanitization path.
样式
🌐 Styling
演员、用例、边界、JSON 节点和显式边都支持 Mermaid 类和直接样式。使用 classDef 来定义一个或多个类,class 来分配它们,style 用于直接声明,或者在一个演员、用例、边界或 JSON 声明上使用 :::。
🌐 Actors, use cases, boundaries, JSON nodes, and explicit edges support Mermaid classes and direct styles. Use classDef to define one or more classes, class to assign them, style for direct declarations, or ::: on an actor, use case, boundary, or JSON declaration.
代码:
mermaid
CSS 声明是用逗号分隔的 property:value 项。值里要用字面逗号请用 \,。分号是不允许的。样式优先顺序是主题变量、default 类、按分配顺序的命名类,然后是直接的 style 声明。同一层里每个属性以后面的声明为准。
🌐 CSS declarations are comma-separated property:value items. Use \, for a literal comma in a value. Semicolons are rejected. Style precedence is theme variables, the default class, named classes in assignment order, then direct style declarations. Later declarations win per property within the same layer.
背景来自活动主题,所以硬编码的 fill 与你编写的主题绑定。fill 在边界上会绘制它的主体,而在 package 边界上会绘制它的标题标签,同时标题文字保持主题的标题颜色。因此,浅色的 fill 在深色主题中会隐藏标题,就像在流程图子图中一样。当设置 fill 时,最好配合 color 声明使用,就像 classDef system fill:#f8f8ff,color:#333 那样。
🌐 Backgrounds come from the active theme, so a hardcoded fill is tied to the theme you wrote it for. A fill on a boundary paints its body and, on a package boundary, its title tab, while the title text keeps the theme's title color. A light fill therefore hides the title in a dark theme, exactly as it does on a flowchart subgraph. Pair the fill with a color declaration when you set one, as in classDef system fill:#f8f8ff,color:#333.
演员元数据是有类型的,并不是样式映射。fillColor、strokeColor、strokeWidth 以及任意的演员元数据键都是错误的。请改用 classDef、class 或 style。
🌐 Actor metadata is typed and is not a style map. fillColor, strokeColor, strokeWidth, and arbitrary actor metadata keys are errors. Use classDef, class, or style instead.
颜色
🌐 Colors
颜色是根据元素的类型决定的,而不是元素声明的顺序。参与者使用一种颜色,用例使用另一种颜色,系统边界使用第三种颜色,所以元素的颜色说明了它的类型,在你编辑图表时不会改变。在文档中间添加一个用例不会影响其他元素的状态,这样可以保持差异、文档截图和视觉基准的稳定性。
🌐 Colour is keyed to the kind of element, not to the order elements are declared. Actors take one colour, use cases another, and system boundaries a third, so the colour of an element says what it is and does not change when you edit the diagram around it. Adding a use case in the middle of a document leaves every other element exactly as it was, which keeps diffs, documentation screenshots, and visual baselines stable.
每个角色都会读取一对主题变量。一个未设置任何变量的主题会像以前一样渲染,并且你可以通过 themeVariables 覆盖其中的任何一个。
🌐 Each role reads a pair of theme variables. A theme that sets none of them renders as it always did, and you can override any of them through themeVariables.
| 主题变量 | 适用于 |
|---|---|
usecaseActorBkg | 参与者图标填充 |
usecaseActorBorder | 参与者图标描边 |
usecaseBkg | 用例主体填充 |
usecaseBorder | 用例主体描边 |
usecaseBoundaryBkg | 系统边界填充(未编号时) |
usecaseBoundaryBorder | 系统边界描边(未编号时) |
usecaseIncludeLine | include 关系描边 |
usecaseExtendLine | extend 关系描边 |
include 和 extend 都是虚线的,在小尺寸下很难区分,所以颜色主题也给它们分配了不同的色调。
系统边界是上述规则的例外:它们是用编号而不是共享一种颜色来表示的。文档中的第一个边界使用主题调色板的第一种颜色,第二个边界使用第二种颜色,依此类推,就像流程图子图编号一样。对于容器来说,这个编号有意义——它表示某个元素属于哪个组——只要边界的顺序不变,这个编号就会保持不变,所以在任何地方添加参与者或用例都不会影响它。对于没有调色板可供编号的主题,usecaseBoundaryBkg 和 usecaseBoundaryBorder 就作为备用。
🌐 System boundaries are the exception to the rule above: they are numbered rather than given one shared colour. The first boundary in the document takes the first colour of the theme's palette, the second takes the second, and so on, exactly as flowchart subgraphs are numbered. For a container the number means something — it says which group an element belongs to — and it stays put as long as the order of the boundaries does, so adding an actor or a use case anywhere leaves it alone. usecaseBoundaryBkg and usecaseBoundaryBorder are the fallback for themes that carry no palette to number with.
代码:
mermaid
每元素颜色旋转
🌐 Per-element colour rotation
将 colorScheme: rotate 设置为将编号从边界扩展到参与者和用例,因此每个元素都会从主题的类别调色板中获取自己的位置,就像实体关系图和类图的着色方式一样。参与者和用例共享一个循环,参与者先编号,用例随后按声明顺序编号。因此按照顺序写的 actor A、usecase U、actor B 会被编号为 A、B、U,而不是 A、U、B。这样可以为每个元素增加多样性,但代价是前面提到的稳定性:插入一个元素会移动其同类所有后续元素的颜色,插入参与者也会影响用例的颜色。边界无论如何都保持自己的编号。只有配色主题(redux-color 和 redux-dark-color)带有调色板,因此在其他主题中,这两个设置渲染出来是相同的。
🌐 Set colorScheme: rotate to extend the numbering from the boundaries to the actors and use cases as well, so every element takes its own slot from the theme's categorical palette, the way entity relationship and class diagrams are coloured. Actors and use cases share one cycle, with the actors numbered first and the use cases after them, each in declaration order. An actor A, usecase U, actor B written in that order is therefore numbered A, B, U rather than A, U, B. This buys per-element variety at the cost of the stability described above: inserting an element shifts the colour of every later element of its own kind, and inserting an actor shifts the use cases too. Boundaries keep their own numbering either way. Only the colour themes (redux-color and redux-dark-color) carry a palette, so on any other theme the two settings render identically.
代码:
mermaid
classDef 和 style 会覆盖两种方案,所以每个元素的语义颜色无论哪种方案激活都可以使用。详情见 Styling。
配置
🌐 Configuration
用例图接受这些图表配置键:
🌐 Use case diagrams accept these diagram configuration keys:
| 键 | 默认值 | 用途 |
|---|---|---|
actorFontSize | 14 | 参与者标签字体大小 |
actorFontFamily | "Open Sans", sans-serif | 参与者标签字体类型 |
actorFontWeight | normal | 参与者标签字体粗细 |
usecaseFontSize | 12 | 用例标签字体大小 |
usecaseFontFamily | "Open Sans", sans-serif | 用例标签字体类型 |
usecaseFontWeight | normal | 用例标签字体粗细 |
nodeSpacing | 50 | 同一层节点之间的间距 |
rankSpacing | 50 | 布局层级之间的间距 |
diagramPadding | 20 | 图表周围的填充 |
colorScheme | role | 图表如何从主题获取颜色 |
useMaxWidth | true | SVG 是否随着可用宽度缩放 |
代码:
mermaid
类和直接样式会覆盖主题和图表字体配置中它们所设置的属性。有关全局初始化和 frontmatter 配置,请参见 Mermaid 配置。
🌐 Classes and direct styles override theme and diagram font configuration for the properties they set. See Mermaid configuration for global initialization and frontmatter configuration.
可访问性
🌐 Accessibility
使用 accTitle 和 accDescr 给图表提供一个可访问的标题和描述。accDescr 在 : 之后接受单行值或多行块。
🌐 Use accTitle and accDescr to give the diagram an accessible title and description. accDescr accepts a single-line value after : or a multiline block.
代码:
mermaid
渲染的参与者、用例、边界、备注、JSON 表格和关系都包含语义可访问的名称。这些名称中包含了参与者变体、业务角色、刻板印象和关系类型。有关图表标题和描述语法,请参见 可访问性。
🌐 Rendered actors, use cases, boundaries, notes, JSON tables, and relationships include semantic accessible names. Actor variants, business roles, stereotypes, and relationship types are included in those names. See Accessibility for the diagram title and description syntax.
完整的例子
🌐 Complete example
特性可以在一个图中组合。这个例子使用了可访问性元数据、角色变体、包边界、刻板印象、类、JSON 表格、一条注释、一条标记的长关联、包含关系、动画边和直接样式边。
🌐 Features can be combined in one diagram. This example uses accessibility metadata, actor variants, a package boundary, stereotypes, classes, a JSON table, a note, a labelled long association, an include relationship, an animated edge, and a directly styled edge.
代码:
mermaid
从 PlantUML 迁移
🌐 Migrating from PlantUML
在 PlantUML 用例图渲染为 Mermaid 之前,需要进行这些更改:
🌐 A PlantUML use case diagram needs these changes before it renders as Mermaid:
- 每行只放一个语句。同一行放第二个语句会导致解析错误。
- 未声明的关系端点会变成椭圆形的用例。在文档的某处用
actor ID声明每个参与者,因为源位置并不意味着是一个参与者。 - 包含
include或extend的关联标签仍然是关联。对于 UML 的 include 和 extend 语义,使用..> : include或..> : extend。 - 将演员
fillColor、strokeColor、strokeWidth和其他任意元数据替换为classDef、class或style。 - 替换这些语法之外且会引发解析错误的构造:分隔符和带标题的分隔符、
as别名、冒号操作符、独立的package和rectangle块、skinparam、<style>块、allowmixing、左右上下的链接提示、注释位置关键字、独立注释、多目标注释,以及newpage。改用classDef、class、style和:::来美化图表,具体见 样式 说明。 - 重写反斜杠转义和普通的
\n多行文本。它们会被解析,所以不会报错。参见 解析但意味着别的东西的构造。
能够解析但意思不同的结构
🌐 Constructs that parse but mean something else
不支持的语法会引发解析错误,但有两个例外。这两种情况不会报错,而且渲染效果与 PlantUML 不同:
🌐 Unsupported syntax raises a parse error, with two exceptions. These parse without an error and render differently from PlantUML:
A("Line1\nLine2")会把反斜杠和n当作单行文本的字面内容。要换行的话,可以使用包含实际换行符的 Markdown 字符串。A("Paren \( x")会把反斜杠当做普通文本保留,因为反斜杠从不转义它后面的字符。可以直接把字符写在引号或 Markdown 字符串里,或者用像#40;和#41;这样的实体代码。
代码:
mermaid