Create diagrams and visualizations using text and code
Mermaid is a JavaScript-based diagramming and charting tool that renders Markdown-inspired text definitions to create and modify diagrams dynamically. It allows you to generate diagrams, flowcharts, and visualizations using simple text syntax, similar to how Markdown works for text formatting.
You can experiment with Mermaid using the Mermaid Live Editor, which provides real-time preview of your diagrams.
Mermaid supports various diagram types. Here are some examples:
stateDiagram
[*] --> First
state First {
[*] --> Second
state Second {
[*] --> second
second --> Third
state Third {
[*] --> third
third --> [*]
}
}
}
stateDiagram
[*] --> Active
state Active {
[*] --> NumLockOff
NumLockOff --> NumLockOn : EvNumLockPressed
NumLockOn --> NumLockOff : EvNumLockPressed
--
[*] --> CapsLockOff
CapsLockOff --> CapsLockOn : EvCapsLockPressed
CapsLockOn --> CapsLockOff : EvCapsLockPressed
--
[*] --> ScrollLockOff
ScrollLockOff --> ScrollLockOn : EvCapsLockPressed
ScrollLockOn --> ScrollLockOff : EvCapsLockPressed
}
Sequence diagrams show how processes operate with one another and in what order.
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.
Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
Pie charts are useful for representing categorical data.
pie title What Voldemort doesn't have?
"FRIENDS" : 2
"FAMILY" : 3
"NOSE" : 45
Gantt charts are useful for project planning and scheduling.
gantt
dateFormat YYYY-MM-DD
title Adding GANTT diagram to mermaid
excludes weekdays 2014-01-10
section A section
Completed task :done, des1, 2014-01-06,2014-01-08
Active task :active, des2, 2014-01-09, 3d
Future task : des3, after des2, 5d
Future task2 : des4, after des3, 5d
Git graphs show git branching and merging operations.
gitGraph:
options
{
"nodeSpacing": 150,
"nodeRadius": 10
}
end
commit
branch newbranch
checkout newbranch
commit
commit
checkout master
commit
commit
merge newbranch
Flowcharts can represent workflows or processes.
graph LR
a1[OS Vendor] --> | Minor Version | a3[Pen Testing]
a3[Pen Testing] --> | Packer Build | a4[QA Test]
a1[OS Vendor] ==> | Major Version | a5[Security Updates]
a5[Security Updates] ==> | Ansible | Configuration
Configuration ==> |A9| Testing
Testing ==> | A9 | a4[QA Test]
a4[QA Test] ==> | A4 | a2[Packer]
Configuration -.-> | A72 | More
More -.-> | A4 | a4[QA Test]
a2[Packer] ==> | Azure | a6[SOE Image Repository]
style a2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5
Each diagram type in Mermaid has its own specific syntax, but they all follow a similar pattern:
- Start with the diagram type (e.g.,
graph
,sequenceDiagram
,gantt
) - Follow with the diagram-specific syntax
- Wrap in a code block with either:
- Plain code blocks:
</code> and <code>
- Mermaid code blocks:
mermaid</code> and <code>
- Plain code blocks:
- Flowcharts:
graph
orflowchart
- Sequence Diagrams:
sequenceDiagram
- Class Diagrams:
classDiagram
- State Diagrams:
stateDiagram
- Entity Relationship Diagrams:
erDiagram
- User Journey:
journey
- Gantt Charts:
gantt
- Pie Charts:
pie
- Git Graphs:
gitGraph
Mermaid can be integrated into various platforms:
- Markdown: Many Markdown processors support Mermaid
- Documentation Tools: Like Docusaurus, VuePress, and Jekyll
- Note-taking Apps: Like Obsidian and Notion
- Online Editors: Like CodiMD and HackMD
- Version Control: GitHub, GitLab, and Bitbucket support Mermaid in Markdown
- Start Simple: Begin with basic diagrams and gradually add complexity
- Use the Live Editor: Test your diagrams in the live editor before embedding them
- Check Syntax: Mermaid is sensitive to syntax; ensure proper spacing and indentation
- Customize Appearance: Use styling options to enhance your diagrams
- Responsive Design: Consider how your diagrams will appear on different screen sizes