2021-01-02 15:55:31 +01:00
---
template: overrides/main.html
---
# Diagrams
Diagrams help to communicate complex relationships and interconnections between
different technical components, and are a great addition to project
2021-10-03 20:28:52 +02:00
documentation. Material for MkDocs integrates with [Mermaid.js], a very
2021-01-02 15:55:31 +01:00
popular and flexible solution for drawing diagrams.
2021-10-03 20:28:52 +02:00
[Mermaid.js]: https://mermaid-js.github.io/mermaid/
2021-01-02 15:55:31 +01:00
## Configuration
2021-10-03 20:28:52 +02:00
[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } ·
2021-01-02 15:55:31 +01:00
:octicons-beaker-24: Experimental ·
2021-10-03 20:28:52 +02:00
[:octicons-tag-24: insiders-1.15.0 ... present][Insiders]
2021-01-02 15:55:31 +01:00
2021-10-03 20:28:52 +02:00
This configuration enables native support for [Mermaid.js] diagrams. Material
for MkDocs will automatically initialize the JavaScript runtime when a page
includes a `mermaid` code block:
2021-01-02 15:55:31 +01:00
``` yaml
markdown_extensions:
2021-01-02 16:43:46 +01:00
- pymdownx.superfences:
2021-01-02 15:55:31 +01:00
custom_fences:
- name: mermaid
2021-07-18 17:50:45 +02:00
class: mermaid
2021-01-05 18:50:08 +01:00
format: !!python/name:pymdownx.superfences.fence_code_format
2021-01-02 15:55:31 +01:00
```
2021-10-03 20:28:52 +02:00
No further configuration is necessary. Advantages over custom integration:
2021-01-02 15:55:31 +01:00
2021-10-03 20:28:52 +02:00
- [x] Works with [instant loading] without any additional effort
2021-01-02 15:55:31 +01:00
- [x] Diagrams automatically use fonts and colors defined in `mkdocs.yml` [^1]
2021-10-03 20:28:52 +02:00
- [x] Fonts and colors can be customized with [additional stylesheets]
2021-01-02 15:55:31 +01:00
- [x] Support for both, light and dark color schemes
2021-10-03 20:28:52 +02:00
_While it's also possible to integrate [Mermaid.js] using existing
2021-02-01 10:11:48 +01:00
third-party plugins[^2], the new native integration is recommended as it
2021-01-02 15:55:31 +01:00
ensures interoperability with all Material for MkDocs features._
[^1]:
2021-10-03 20:28:52 +02:00
While all [Mermaid.js] features should work out-of-the-box, Material for
2021-07-18 17:50:45 +02:00
MkDocs will currently adjust the fonts and colors for flow charts, sequence
diagrams, class diagams, state diagrams and entity relationship diagrams.
2021-01-02 15:55:31 +01:00
[^2]:
2021-10-03 20:28:52 +02:00
If you don't want to use the native integration, [mkdocs-mermaid2-plugin]
2021-01-02 15:55:31 +01:00
might be a good alternative. However, note that this plugin cannot be used
2021-10-03 20:28:52 +02:00
in conjunction with the [mkdocs-minify-plugin] and doesn't adapt to
2021-01-24 12:34:03 +01:00
dark mode.
2021-01-02 15:55:31 +01:00
2021-10-03 20:28:52 +02:00
[Insiders]: ../insiders/index.md
[instant loading]: ../setup/setting-up-navigation.md#instant-loading
[additional stylesheets]: ../customization.md#additional-css
[mkdocs-mermaid2-plugin]: https://github.com/fralau/mkdocs-mermaid2-plugin
[mkdocs-minify-plugin]: https://github.com/byrnereese/mkdocs-minify-plugin
2021-01-02 15:55:31 +01:00
## Usage
2021-10-03 20:28:52 +02:00
Mermaid diagrams are written as code blocks with the help of the [SuperFences]extension. They must be enclosed with two separate lines containing three
backticks.
2021-07-18 17:50:45 +02:00
2021-10-03 20:28:52 +02:00
[code blocks]: code-blocks.md
[SuperFences]: ../setup/extensions/python-markdown-extensions.md#superfences
2021-07-18 17:50:45 +02:00
### Using flowcharts
2021-10-03 20:28:52 +02:00
[Flowcharts] are diagrams that represent workflows or processes. The steps
2021-07-18 17:50:45 +02:00
are rendered as nodes of various kinds and are connected by edges, describing
the necessary order of steps.
2021-01-02 15:55:31 +01:00
_Example_:
```` markdown
``` mermaid
graph LR
A[Start] --> B{Error?};
B -->|Yes| C[Hmm...];
C --> D[Debug];
D --> B;
B ---->|No| E[Yay!];
```
````
_Result_:
2021-06-15 11:03:15 +02:00
``` mermaid
graph LR
A[Start] --> B{Error?};
B -->|Yes| C[Hmm...];
C --> D[Debug];
D --> B;
B ---->|No| E[Yay!];
```
2021-01-02 15:55:31 +01:00
2021-10-03 20:28:52 +02:00
[Flowcharts]: https://mermaid-js.github.io/mermaid/#/flowchart
2021-01-02 15:55:31 +01:00
2021-07-18 17:50:45 +02:00
### Using sequence diagrams
2021-10-03 20:28:52 +02:00
[Sequence diagrams] describe a specific scenario as sequential interactions
2021-07-18 17:50:45 +02:00
between multiple objects or actors, including the messages that are exchanged
between those actors.
_Example_:
```` markdown
``` mermaid
sequenceDiagram
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
```
````
_Result_:
``` mermaid
sequenceDiagram
Alice->>John: Hello John, how are you?
loop Healthcheck
John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
```
2021-10-03 20:28:52 +02:00
[Sequence diagrams]: https://mermaid-js.github.io/mermaid/#/sequenceDiagram
2021-07-18 17:50:45 +02:00
### Using state diagrams
2021-10-03 20:28:52 +02:00
[State diagrams] are a great tool to describe the behavior of a system,
2021-07-18 17:50:45 +02:00
decomposing it into a finite number of states, and transitions between those
states.
_Example_:
```` markdown
``` mermaid
stateDiagram-v2
[*] --> Active
state Active {
[*] --> NumLockOff
NumLockOff --> NumLockOn : EvNumLockPressed
NumLockOn --> NumLockOff : EvNumLockPressed
--
[*] --> CapsLockOff
CapsLockOff --> CapsLockOn : EvCapsLockPressed
CapsLockOn --> CapsLockOff : EvCapsLockPressed
--
[*] --> ScrollLockOff
ScrollLockOff --> ScrollLockOn : EvScrollLockPressed
ScrollLockOn --> ScrollLockOff : EvScrollLockPressed
}
```
````
_Result_:
``` mermaid
stateDiagram-v2
[*] --> Active
state Active {
[*] --> NumLockOff
NumLockOff --> NumLockOn : EvNumLockPressed
NumLockOn --> NumLockOff : EvNumLockPressed
--
[*] --> CapsLockOff
CapsLockOff --> CapsLockOn : EvCapsLockPressed
CapsLockOn --> CapsLockOff : EvCapsLockPressed
--
[*] --> ScrollLockOff
ScrollLockOff --> ScrollLockOn : EvScrollLockPressed
ScrollLockOn --> ScrollLockOff : EvScrollLockPressed
}
```
2021-10-03 20:28:52 +02:00
[State diagrams]: https://mermaid-js.github.io/mermaid/#/stateDiagram
2021-07-18 17:50:45 +02:00
### Using class diagrams
2021-10-03 20:28:52 +02:00
[Class diagrams] are central to object oriented programing, describing the
2021-07-18 17:50:45 +02:00
structure of a system by modelling entities as classes and relationships between
them.
_Example_:
```` markdown
``` mermaid
classDiagram
Person < |-- Student
Person < |-- Professor
Person : +String name
Person : +String phoneNumber
Person : +String emailAddress
Person: +purchaseParkingPass()
Address "1" < -- " 0 . . 1 " Person:lives at
class Student{
+int studentNumber
+int averageMark
+isEligibleToEnrol()
+getSeminarsTaken()
}
class Professor{
+int salary
}
class Address{
+String street
+String city
+String state
+int postalCode
+String country
-validate()
+outputAsLabel()
}
```
````
_Result_:
``` mermaid
classDiagram
Person < |-- Student
Person < |-- Professor
Person : +String name
Person : +String phoneNumber
Person : +String emailAddress
Person: +purchaseParkingPass()
Address "1" < -- " 0 . . 1 " Person:lives at
class Student{
+int studentNumber
+int averageMark
+isEligibleToEnrol()
+getSeminarsTaken()
}
class Professor{
+int salary
}
class Address{
+String street
+String city
+String state
+int postalCode
+String country
-validate()
+outputAsLabel()
}
```
2021-10-03 20:28:52 +02:00
[Class diagrams]: https://mermaid-js.github.io/mermaid/#/classDiagram
2021-07-18 17:50:45 +02:00
### Using entity-relationship diagrams
2021-10-03 20:28:52 +02:00
An [entity-relationship diagram] is composed of entity types and specifies
2021-07-18 17:50:45 +02:00
relationships that exist between entities. It describes inter-related things in
a specific domain of knowledge.
_Example_:
```` markdown
``` mermaid
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
```
````
_Result_:
``` mermaid
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
```
2021-10-03 20:28:52 +02:00
[entity-relationship diagram]: https://mermaid-js.github.io/mermaid/#/entityRelationshipDiagram