view source

Mermaid and Graphviz Diagrams: Flowchart, Sequence, ER

skill

Draw 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.

Apache-2.0 Requires: Node.js 18+ with npx and a Chromium browser (the installer adds both). Graphviz and D2 use their own binaries when installed, otherwise npx builds that write SVG only. PlantUML needs Java and its jar. macOS/Linux.

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

QuestionTypeNotes
What happens, step by step, with decisionsFlowchartOne start, labeled branches out of every decision
Who sends what to whom, in what orderSequenceDeclare participants first to fix their left-to-right order
What states can a thing be in, and what moves itStateLabel every transition with its trigger
What is stored and how tables relateEntity-relationshipState cardinality on every line
What is the system, who uses it, what does it depend onC4 system contextOne system in the middle; people and neighbouring systems; no technologies or protocols
What apps and data stores make up the systemC4 containerA container is an application or a data store; name the technology on each
What is inside one containerC4 componentOnly when it adds value; long-lived ones are better generated
Who reports to whomOrg chartTop-down hierarchy. Mermaid lists no org chart type: use a top-down flowchart, or Graphviz dot
What happens when, with durations and dependenciesGanttMark milestones; exclude weekends when durations are working days
What happened in order, without durationsTimelineGroup periods into sections
Who does which stepSwimlaneMermaid 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

FormatChoose it whenRender withWatch for
MermaidThe diagram sits in a document on GitHub or GitLab; sequence, state, ER, Gantt, timelineThe 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 DOTA graph or hierarchy where the layout engine matters: dot layered, sfdp large graphs, circo circular, twopi radialThe dot program, or its WebAssembly build through npx; no browserNo sequence, ER or Gantt types: you write only nodes and edges
D2Architecture diagrams with nested containers; sequence diagrams through a shape settingThe d2 program, or its WebAssembly build through npxLayout engines: dagre (default), ELK, TALA
PlantUMLUML kinds the others lack (use case, object, component, deployment, timing), or Gantt, WBS, mind mapjava -jar plantuml.jarNeeds 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 neutral to the CLI, or set colours yourself.

Accessible title and description

  • Mermaid: put accTitle: Short title and accDescr: What the diagram shows. (or accDescr { ... } for several lines) inside the diagram, after the type line. The render adds <title>, <desc>, aria-labelledby and aria-describedby to 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-describedby points 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

text
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 browser prints the path of an installed one: put it in browser.json as {"executablePath": "<path>"}.
  • Without the dot or d2 binary, the two npx lines render the same source; they write SVG only. PlantUML has no such route: without Java, pick another format.
  • -s 2 doubles the PNG size; the default is small for a slide.
  • d2 login.d2 writes login.svg. java -jar plantuml.jar login.txt writes 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.