Sequence Diagrams
Sequence diagrams are created by setting shape: sequence_diagram
on an object.
Rules
Unlike other tools, there is no special syntax to learn for sequence diagrams. The rules are also almost exactly the same as everywhere else in D2, with two notable differences.
Scoping
Children of sequence diagrams share the same scope throughout the sequence diagram.
For example:
Outside of a sequence diagram, there would be multiple instances of alice
and bob
,
since they have different container scopes. But when nested under shape:
sequence_diagram
, they refer to the same alice
and bob
.
Ordering
Elsewhere in D2, there is no notion of order. If you define a connection after another, there is no guarantee is will visually appear after. However, in sequence diagrams, order matters. The order in which you define everything is the order they will appear.
This includes actors. You don't have to explicitly define actors (except when they first appear in a group), but if you want to define a specific order, you should.
An actor in D2 is also known elsewhere as "participant".
Features
Sequence diagrams are D2 objects
Like every other object in D2, they can be contained, connected, relabeled, re-styled, and treated like any other object.
Spans
Spans convey a beginning and end to an interaction within a sequence diagram.
A span in D2 is also known elsewhere as a "lifespan", "activation box", and "activation bar".
You can specify a span by connecting a nested object on an actor.
Groups
Groups help you label a subset of the sequence diagram.
A group in D2 is also known elsewhere as a "fragment", "edge group", and "frame".
We saw an example of this in an earlier example when explaining scoping rules. More
formally, a group is a container within a sequence_diagram
shape which is not connected
to anything but has connections or objects inside.
Due to the unique scoping rules in sequence diagrams, when you are within a group, the
objects you reference in connections must exist at the top-level. Notice in the above
example that alice
and bob
are explicitly declared before group declarations.
Notes
Notes are declared by defining a nested object on an actor with no connections going to it.
Self-messages
Self-referential messages can be declared from an actor to the themselves.
Customization
You can style shapes and connections like any other. Here we make some messages dashed and set the shape on an actor.
Lifeline edges (those lines going from top-down) inherit the actor's stroke
and
stroke-dash
styles.