Mermaid and Graphviz Diagrams: Flowchart, Sequence, ER
skillDraw flowcharts, sequence, state, ER, C4 and Gantt diagrams as Mermaid, Graphviz or D2 text, render SVG or PNG and check it. Use for diagram requests.
agents that use this skill
instructions
Overview
A diagram answers one question about a process, a system or a plan. Here the text source is the master copy and the image is its output. Use this skill to pick the diagram type and the text format, write the source, render it to SVG or PNG, and check the picture. The deliverable is the source file and the rendered image side by side, with a title, a key where the notation needs one, and a text alternative. The image was opened and looked at before delivery.
Mental model
- A diagram has one reader and one question. The C4 model makes the same point: its diagram levels "tell different stories to different audiences", and the system context and container diagrams are enough for most teams.
- You control structure: which things, which links, which grouping, which direction. A layout engine places them. Fighting the engine for coordinates wastes time; changing the structure or the direction fixes most ugly layouts.
- A model cannot see its own output. A diagram that parses can still overlap, clip or tangle, so rendering and looking is part of writing it.
- Colour is the last layer. A diagram that reads in grey survives printing and colour blindness.
Rules
Pick the type by the question
| Question | Type | Notes |
|---|---|---|
| What happens, step by step, with decisions | Flowchart | One start, labeled branches out of every decision |
| Who sends what to whom, in what order | Sequence | Declare participants first to fix their left-to-right order |
| What states can a thing be in, and what moves it | State | Label every transition with its trigger |
| What is stored and how tables relate | Entity-relationship | State cardinality on every line |
| What is the system, who uses it, what does it depend on | C4 system context | One system in the middle; people and neighbouring systems; no technologies or protocols |
| What apps and data stores make up the system | C4 container | A container is an application or a data store; name the technology on each |
| What is inside one container | C4 component | Only when it adds value; long-lived ones are better generated |
| Who reports to whom | Org chart | Top-down hierarchy. Mermaid lists no org chart type: use a top-down flowchart, or Graphviz dot |
| What happens when, with durations and dependencies | Gantt | Mark milestones; exclude weekends when durations are working days |
| What happened in order, without durations | Timeline | Group periods into sections |
| Who does which step | Swimlane | Mermaid has a swimlane type in beta; a flowchart with one subgraph per lane works everywhere |
Do not use a diagram for values to compare or trend over time: that is a chart.
Pick the format
| Format | Choose it when | Render with | Watch for |
|---|---|---|---|
| Mermaid | The diagram sits in a document on GitHub or GitLab; sequence, state, ER, Gantt, timeline | The Mermaid CLI (needs a Chromium browser) | A misspelled keyword breaks the diagram; a lowercase end in a label breaks flowcharts and sequence diagrams, so quote or capitalize it. C4 has no automatic layout and no legends; the architecture type is beta |
| Graphviz DOT | A graph or hierarchy where the layout engine matters: dot layered, sfdp large graphs, circo circular, twopi radial | The dot program, or its WebAssembly build through npx; no browser | No sequence, ER or Gantt types: you write only nodes and edges |
| D2 | Architecture diagrams with nested containers; sequence diagrams through a shape setting | The d2 program, or its WebAssembly build through npx | Layout engines: dagre (default), ELK, TALA |
| PlantUML | UML kinds the others lack (use case, object, component, deployment, timing), or Gantt, WBS, mind map | java -jar plantuml.jar | Needs Java and the jar, so use it only where both are installed; some diagrams also need Graphviz |
Default to Mermaid when the host renders it, Graphviz for graphs, and the format the team already uses when one exists.
Hosts: GitHub renders a fenced mermaid block in Markdown. GitLab renders Mermaid and, where enabled, PlantUML and Kroki blocks (PlantUML is on at GitLab.com; an administrator enables it on self-managed). Check any other host's documentation before relying on it, and keep a rendered SVG for places that do not render source.
One question per diagram
- Title every diagram with its type and scope ("Container diagram for the billing system").
- Split when two readers need different detail or when the title needs "and". Go from broad to narrow: context, then container, then one component diagram if it earns its place.
- Keep to the question. A node that does not help answer it goes.
- Collapse a busy group into one node and give its detail a diagram of its own.
Direction, labels and key
- Set the direction on purpose: left to right for steps read like a sentence, top to bottom for hierarchies. Mermaid takes TB, TD, BT, LR, RL after
flowchart. - Label every edge, except a plain next-step arrow in a flowchart. A label says what flows or what happens and matches the arrow's direction: "reads orders, SQL over TLS", not "uses". Each line is one-way.
- Between containers, state the protocol or technology on the link.
- Give each element a type and a short description of its responsibility; give containers and components their technology.
- Add a key that explains shapes, colours, border styles, line types and arrowheads. Spell out or explain acronyms.
- Use one shape per kind of thing, the same way in every diagram of the set.
Colour
- Do not rely on colour alone: a shape, a line style or a label carries the same meaning. Dashed outline plus the word "fails" beats red alone.
- Use only as many colours as show a real difference, and keep each colour's meaning across the set.
- Parts needed to understand the diagram need at least 3:1 contrast against their neighbours and the background; text needs 4.5:1.
- Mermaid's default theme colours parts of a diagram without meaning. Pass
-t neutralto the CLI, or set colours yourself.
Accessible title and description
- Mermaid: put
accTitle: Short titleandaccDescr: What the diagram shows.(oraccDescr {...}for several lines) inside the diagram, after the type line. The render adds<title>,<desc>,aria-labelledbyandaria-describedbyto the SVG. Converting a Markdown file with the CLI turns them into each image's title and alt text. - Other formats: Graphviz SVG gives each node and edge a
<title>, which is not a description of the whole. Put the text alternative where the image is embedded: a short alt text naming the diagram, and a long description in the page that gives the structure in words or a table, in reading order. A<figure>with a<figcaption>ties them together;aria-describedbypoints at a text-only description. - The description says what the diagram shows and its main path or finding. It does not repeat the title.
Render and check
html-render browser
npx -y -p @mermaid-js/mermaid-cli mmdc -p browser.json -i login.mmd -o login.svg
npx -y -p @mermaid-js/mermaid-cli mmdc -p browser.json -t neutral -i login.mmd -o login.png -s 2
dot -Tsvg login.dot -o login.svg
dot -Tpng -Gdpi=144 login.dot -o login.png
npx -y @hpcc-js/wasm-graphviz-cli -T svg login.dot > login.svg
d2 login.d2
npx -y @d2lang/d2 login.d2 login.svg
java -jar plantuml.jar login.txt- The Mermaid CLI needs a Chromium and does not fetch one when run through npx.
html-render browserprints the path of an installed one: put it inbrowser.jsonas{"executablePath": "<path>"}. - Without the
dotord2binary, the two npx lines render the same source; they write SVG only. PlantUML has no such route: without Java, pick another format. -s 2doubles the PNG size; the default is small for a slide.d2 login.d2writeslogin.svg.java -jar plantuml.jar login.txtwrites a PNG.- SVG for documents and anything people zoom; PNG for chat and slides.
Then open the image and look: overlapping or clipped labels, edges that cross for no reason, an arrow direction that fights the reading direction, a missing key, text too small at the size where it will be shown. Fix the source, not the image, and render again. For Mermaid, confirm the SVG carries the title and description: grep -o '<title[^>]*>[^<]*' login.svg.
Keep the source next to the image with the same base name (login.mmd, login.svg), and put the render command in a comment at the top of the source. Commit both so the image can be regenerated.
When the user cannot be asked, state which question and which reader you assumed.
Examples
One diagram for everything. Bad: a flowchart of forty nodes titled "System" that mixes sign-up, billing and servers. Good: a system context diagram with the user, the payment provider and the system as one box, and a separate container diagram for billing, each titled with its type and scope.
Generic edge labels. Bad: API -->|uses| DB. Good: API -->|reads and writes orders, SQL over TLS| DB.
Colour as the only cue. Bad: failure paths red, success paths green, nothing else. Good: failure paths red with a dashed line and the label "fails", and a key that lists both cues.
Checklist
- One question and one reader; the title states type and scope.
- The type fits the question; the format fits the host and the toolchain.
- Direction is set; edges are labeled; a key explains the notation.
- Meaning does not depend on colour alone; contrast is 3:1 for parts and 4.5:1 for text.
- Title and description are in the source (Mermaid) or beside the image (other formats).
- Rendered to SVG or PNG and opened; no overlap, clipping or tangles.
- Source and image sit together with the render command recorded.
- A render tool that was missing is named in the hand-off.
Composition / References
Scope limits: charts of data values, social graphics and carousels, dashboards, logos, and drawing in an interactive tool such as a whiteboard app are separate jobs. UML modelling theory is out of scope.
- C4 model, diagrams and notation: https://c4model.com/diagrams , https://c4model.com/diagrams/system-context , https://c4model.com/diagrams/container , https://c4model.com/diagrams/component , https://c4model.com/diagrams/notation
- Mermaid: https://mermaid.js.org/intro/syntax-reference.html , https://mermaid.js.org/config/accessibility.html , https://mermaid.js.org/syntax/flowchart.html , https://mermaid.js.org/syntax/sequenceDiagram.html , https://mermaid.js.org/syntax/stateDiagram.html , https://mermaid.js.org/syntax/entityRelationshipDiagram.html , https://mermaid.js.org/syntax/gantt.html , https://mermaid.js.org/syntax/timeline.html , https://mermaid.js.org/syntax/c4.html , https://mermaid.js.org/syntax/architecture.html
- Mermaid CLI: https://github.com/mermaid-js/mermaid-cli
- Graphviz and D2 through npx: https://www.npmjs.com/package/@hpcc-js/wasm-graphviz-cli , https://www.npmjs.com/package/@d2lang/d2
- Graphviz: https://graphviz.org/doc/info/command.html , https://graphviz.org/docs/layouts/ , https://graphviz.org/docs/outputs/svg/
- D2: https://d2lang.com/tour/install , https://d2lang.com/tour/layouts/ , https://d2lang.com/tour/exports/ , https://d2lang.com/tour/shapes/ , https://d2lang.com/tour/sequence-diagrams/
- PlantUML: https://plantuml.com/starting , https://plantuml.com/
- GitHub diagrams: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-diagrams ; GitLab Markdown: https://docs.gitlab.com/user/markdown/
- UK Government Analysis Function, colours in charts: https://analysisfunction.civilservice.gov.uk/policy-store/data-visualisation-colours-in-charts/
- W3C, complex images: https://www.w3.org/WAI/tutorials/images/complex/