顺序图

顺序图-基本语法

序列图是一个交互图,它显示进程如何相互操作以及以什么顺序进行操作。

渲染序列图

sequenceDiagram
    Alice->>John: Hello John, how are you?
    John-->>Alice: Great!
sequenceDiagram
    Alice->>John: Hello John, how are you?
    John-->>Alice: Great!

句法

参加者
可以像本页第一个示例中那样隐式定义参与者。在图表源文本中按出现顺序呈现参与者或演员。有时,您可能想以不同于第一条消息出现的顺序来显示参与者。通过执行以下操作可以指定演员的出场顺序

sequenceDiagram
    participant John
    participant Alice
    Alice->>John: Hello John, how are you?
    John-->>Alice: Great!
sequenceDiagram
    participant John
    participant Alice
    Alice->>John: Hello John, how are you?
    John-->>Alice: Great!

别名

演员可以具有方便的标识符和描述性标签。

sequenceDiagram
    participant A as Alice
    participant J as John
    A->>J: Hello John, how are you?
    J->>A: Great!
sequenceDiagram
    participant A as Alice
    participant J as John
    A->>J: Hello John, how are you?
    J->>A: Great!

留言内容

消息可以是实线或虚线显示的两种。

[Actor][Arrow][Actor]:Message text
类型 描述
-> 实线无箭头
-> 虚线无箭头
->> 带箭头的实线
->> 带箭头的虚线
-X 实线,末端带有叉号(异步)
- X 虚线末端带有叉号(异步)

激活方式

可以激活和停用角色。(de)激活可以是专用的声明:

sequenceDiagram
    Alice->>John: Hello John, how are you?
    activate John
    John-->>Alice: Great!
    deactivate John
sequenceDiagram
    Alice->>John: Hello John, how are you?
    activate John
    John-->>Alice: Great!
    deactivate John

通过在消息箭头后面添加+/ -后缀,还有一种快捷方式标记:

sequenceDiagram
    Alice->>+John: Hello John, how are you?
    John-->>-Alice: Great!
sequenceDiagram
    Alice->>+John: Hello John, how are you?
    John-->>-Alice: Great!

可以为同一演员堆叠激活:

sequenceDiagram
    Alice->>+John: Hello John, how are you?
    Alice->>+John: John, can you hear me?
    John-->>-Alice: Hi Alice, I can hear you!
    John-->>-Alice: I feel great!
sequenceDiagram
Alice->>+John: Hello John, how are you?
Alice->>+John: John, can you hear me?
John-->>-Alice: Hi Alice, I can hear you!
John-->>-Alice: I feel great!

笔记

可以在顺序图中添加注释。这是通过注解[[右| 左| 结束] [演员]:笔记内容中的文字

请参阅以下示例

sequenceDiagram
    participant John
    Note right of John: Text in note
sequenceDiagram
    participant John
    Note right of John: Text in note

也可以创建跨越两个参与者的笔记:

sequenceDiagram
    Alice->John: Hello John, how are you?
    Note over Alice,John: A typical interaction
sequenceDiagram
    Alice->John: Hello John, how are you?
    Note over Alice,John: A typical interaction

循环

loop Loop text
... statements ...
end

请参阅以下示例:

sequenceDiagram
    Alice->John: Hello John, how are you?
    loop Every minute
        John-->Alice: Great!
    end
sequenceDiagram
Alice->John: Hello John, how are you?
loop Every minute
    John-->Alice: Great!
end

Alt键

可以在顺序图中表达替代路径。这是通过符号来完成的

alt Describing text
... statements ...
else
... statements ...
end

或者是否有可选的序列(如果没有其他序列)。

opt Describing text
... statements ...
end

请参阅以下示例:

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
    end
    opt Extra response
        Bob->>Alice: Thanks for asking
    end
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
    end
    opt Extra response
        Bob->>Alice: Thanks for asking
    end

背景突出显示

通过提供彩色背景矩形可以突出显示流。这是通过符号来完成的

颜色是使用rgb和rgba语法定义的。

rect rgb(0, 255, 0)
... content ...
end
Class Description
actor Style for the actor box at the top of the diagram.
text.actor Styles for text in the actor box at the top of the diagram.
actor-line The vertical line for an actor.
messageLine0 Styles for the solid message line.
messageLine1 Styles for the dotted message line.
messageText Defines styles for the text on the message arrows.
labelBox Defines styles label to left in a loop.
labelText Styles for the text in label for loops.
loopText Styles for the text in the loop box.
loopLine Defines styles for the lines in the loop box.
note Styles for the note box.
noteText Styles for the text on in the note boxes.
-------------本文结束感谢您的阅读-------------