AgentSkillsCN

mermaid-diagrams

生成Mermaid图表(流程图、时序图、类图、状态图、ER图、C4图、思维导图、Git图)。当需要绘制图表、可视化流程、记录架构设计,或以直观方式阐释系统时,可随时调用此功能。触发关键词包括:“diagram”、“flowchart”、“sequence diagram”、“class diagram”、“state diagram”、“ER diagram”、“C4”、“mindmap”、“gitgraph”、“visualize”、“draw”。

SKILL.md
--- frontmatter
name: mermaid-diagrams
description: Generate Mermaid diagrams (flowcharts, sequence, class, state, ER, C4, mindmaps, gitgraph). Use when asked to create diagrams, visualize processes, document architecture, or explain systems visually. Triggers include "diagram", "flowchart", "sequence diagram", "class diagram", "state diagram", "ER diagram", "C4", "mindmap", "gitgraph", "visualize", "draw".

Mermaid Diagrams

Generate diagrams using Mermaid syntax. All diagrams render in markdown code blocks with mermaid language identifier.

Decision Guide

Use CaseDiagram Type
Process flow, decision treesFlowchart
API calls, system interactions over timeSequence
OOP structure, domain modelsClass
State machines, workflowsState
Database schemas, data relationshipsER
Software architecture (C4 model)C4
Brainstorming, hierarchical organizationMindmap
Git branching, commit historyGitGraph

Quick Reference

Flowchart

mermaid
flowchart TD
    A[Start] --> B{Decision}
    B -->|Yes| C[Action 1]
    B -->|No| D[Action 2]
    C --> E[End]
    D --> E

Sequence

mermaid
sequenceDiagram
    participant U as User
    participant S as Server
    U->>S: Request
    S-->>U: Response

Class

mermaid
classDiagram
    Animal <|-- Dog
    Animal : +String name
    Animal : +makeSound()
    Dog : +fetch()

State

mermaid
stateDiagram-v2
    [*] --> Idle
    Idle --> Processing: start
    Processing --> Done: complete
    Done --> [*]

ER

mermaid
erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains

C4 Context

mermaid
C4Context
    Person(user, "User", "End user")
    System(app, "Application", "Main system")
    Rel(user, app, "Uses")

Mindmap

mermaid
mindmap
  root((Topic))
    Branch A
      Leaf 1
      Leaf 2
    Branch B

GitGraph

mermaid
gitgraph
    commit
    branch feature
    commit
    checkout main
    merge feature

Flowchart Reference

Directions: TD (top-down), LR (left-right), BT (bottom-top), RL (right-left)

Node Shapes

ShapeSyntaxExample
Rectangle[text]A[Process]
Rounded(text)A(Start)
Stadium([text])A([Terminal])
Diamond{text}A{Decision}
Circle((text))A((Event))
Hexagon{{text}}A{{Prepare}}
Cylinder[(text)]A[(Database)]
Subroutine[[text]]A[[Subprocess]]
Parallelogram[/text/]A[/Input/]
Trapezoid[\text\]A[\Manual Op\]

Link Types

TypeSyntaxDescription
Arrow-->Solid with arrowhead
Open---Solid, no arrowhead
Dotted arrow-.->Dotted with arrowhead
Thick arrow==>Thick with arrowhead
With text--|text|>Label on link
Multi-length---->Longer link (more ranks)

Subgraphs

mermaid
flowchart TD
    subgraph Backend["Backend Services"]
        direction LR
        API --> DB[(Database)]
    end
    Client --> API

Styling

mermaid
flowchart TD
    A[Node]:::highlight
    classDef highlight fill:#ff9,stroke:#333
    style A stroke-width:2px

Sequence Diagram Reference

Arrow Types

SyntaxStyle
->>Solid with arrowhead
-->>Dotted with arrowhead
-xSolid with cross
-)Async (open arrow)
<<->>Bidirectional

Participants

mermaid
sequenceDiagram
    actor User
    participant API as API Server
    participant DB as Database

Activations

mermaid
sequenceDiagram
    User->>+API: Request
    API->>+DB: Query
    DB-->>-API: Result
    API-->>-User: Response

Control Flow

mermaid
sequenceDiagram
    alt Success
        A->>B: OK
    else Failure
        A->>B: Error
    end

    loop Every 5s
        A->>B: Ping
    end

    opt Optional
        A->>B: Maybe
    end

Notes

mermaid
sequenceDiagram
    Note right of A: Single note
    Note over A,B: Spanning note

Grouping with Boxes

mermaid
sequenceDiagram
    box Blue Frontend
        participant U as User
        participant W as Web
    end
    box Green Backend
        participant A as API
    end

Class Diagram Reference

Visibility Modifiers

SymbolMeaning
+Public
-Private
#Protected
~Package

Relationships

TypeSyntaxMeaning
Inheritance<|--Extends
Composition*--Strong owns
Aggregationo--Weak owns
Association-->Uses
Dependency..>Depends on
Realization..|>Implements

Full Example

mermaid
classDiagram
    class Animal {
        <<abstract>>
        +String name
        +int age
        +makeSound()* void
    }
    class Dog {
        +String breed
        +fetch() void
    }
    Animal <|-- Dog
    Dog "1" --> "*" Toy : plays with

Generics

mermaid
classDiagram
    class List~T~ {
        +add(T item)
        +T get(int index)
    }

Annotations

  • <<interface>> - Interface
  • <<abstract>> - Abstract class
  • <<service>> - Service class
  • <<enumeration>> - Enum

State Diagram Reference

Basic Syntax

mermaid
stateDiagram-v2
    [*] --> State1
    State1 --> State2: trigger
    State2 --> [*]

Composite States

mermaid
stateDiagram-v2
    state Active {
        [*] --> Running
        Running --> Paused: pause
        Paused --> Running: resume
    }
    [*] --> Active
    Active --> [*]: stop

Fork and Join

mermaid
stateDiagram-v2
    state fork <<fork>>
    state join <<join>>
    [*] --> fork
    fork --> Task1
    fork --> Task2
    Task1 --> join
    Task2 --> join
    join --> [*]

Choice

mermaid
stateDiagram-v2
    state check <<choice>>
    [*] --> check
    check --> Success: valid
    check --> Error: invalid

Notes

mermaid
stateDiagram-v2
    State1
    note right of State1
        Description here
    end note

ER Diagram Reference

Cardinality

LeftRightMeaning
|oo|Zero or one
||||Exactly one
}oo{Zero or more
}||{One or more

Relationship Types

  • -- Identifying (solid line, child depends on parent)
  • .. Non-identifying (dashed line, independent entities)

Attributes

mermaid
erDiagram
    USER {
        int id PK
        string email UK
        string name
        datetime created_at
    }
    POST {
        int id PK
        int user_id FK
        string title
        text content
    }
    USER ||--o{ POST : writes

C4 Diagram Reference

C4 provides 4 levels of abstraction for architecture documentation.

Diagram Types

  • C4Context - System context (level 1)
  • C4Container - Container diagram (level 2)
  • C4Component - Component diagram (level 3)
  • C4Dynamic - Interaction sequences
  • C4Deployment - Infrastructure layout

Elements

ElementSyntaxUse
PersonPerson(alias, label, desc)Users
Person_ExtPerson_Ext(...)External users
SystemSystem(alias, label, desc)Software systems
System_ExtSystem_Ext(...)External systems
SystemDbSystemDb(...)Database systems
ContainerContainer(alias, label, tech, desc)Applications
ContainerDbContainerDb(...)Container databases
ComponentComponent(alias, label, tech, desc)Internal parts

Boundaries

mermaid
C4Container
    System_Boundary(app, "Application") {
        Container(web, "Web App", "React")
        Container(api, "API", "Node.js")
        ContainerDb(db, "Database", "PostgreSQL")
    }
    Rel(web, api, "HTTP/JSON")
    Rel(api, db, "SQL")

Relationships

  • Rel(from, to, label) - Standard relationship
  • Rel_U/D/L/R(...) - Directional hints
  • BiRel(from, to, label) - Bidirectional

Complete C4 Context Example

mermaid
C4Context
    Person(user, "Customer", "Uses the system")
    System(app, "E-Commerce", "Online store")
    System_Ext(payment, "Payment Gateway", "Processes payments")
    System_Ext(shipping, "Shipping API", "Handles delivery")

    Rel(user, app, "Browses, orders")
    Rel(app, payment, "Processes payments")
    Rel(app, shipping, "Ships orders")

Mindmap Reference

Uses indentation for hierarchy. Root node required.

Node Shapes

mermaid
mindmap
    root((Central Topic))
        [Square]
        (Rounded)
        ))Cloud((
        {{Hexagon}}

With Icons

mermaid
mindmap
    root((Project))
        Tasks ::icon(fa fa-tasks)
        Team ::icon(fa fa-users)

Styling

mermaid
mindmap
    root
        Important:::urgent
        Normal

GitGraph Reference

Basic Operations

mermaid
gitgraph
    commit id: "initial"
    commit id: "feature"
    branch develop
    commit
    checkout main
    merge develop tag: "v1.0"

Commit Types

  • type: NORMAL - Default
  • type: HIGHLIGHT - Emphasized
  • type: REVERSE - Reverted

Full Example

mermaid
gitgraph
    commit id: "init"
    branch feature
    commit id: "add-login"
    commit id: "add-logout" type: HIGHLIGHT
    checkout main
    commit id: "hotfix" type: REVERSE
    merge feature tag: "v1.0"

Best Practices

DO

  • Keep diagrams focused (one concept per diagram)
  • Use clear, descriptive labels
  • Add direction hints (TD, LR) explicitly
  • Use subgraphs/boundaries for grouping
  • Include legends for complex diagrams

DON'T

  • Overcrowd with too many nodes (>15-20)
  • Use cryptic single-letter IDs without labels
  • Mix multiple concerns in one diagram
  • Rely on auto-layout for complex diagrams

Layout Tips

  • Flowchart: LR for processes, TD for hierarchies
  • Sequence: Order participants by interaction frequency
  • Class: Group related classes with namespaces
  • C4: Start with Context, drill down to Container/Component
  • ER: Place central entities in the middle

Common Fixes

ProblemSolution
Nodes overlapReduce node count, use subgraphs
Links cross confusinglyReorder nodes, change direction
Text truncatedUse aliases: A[Long Name] as short
Diagram too wideSwitch LR to TD