LOADING
1067 字
5 分钟

使用 Mermaid 圖表在 Markdown 中的完整指南

Mermaid Example

翻譯提示 (未審閱)

此頁面由 AI 翻譯生成,可能存在不準確之處。

翻譯自: 🇬🇧 English (Deepseek)

檢視原文 回到譯文

使用 Mermaid 圖表在 Markdown 中的完整指南

本文示範如何在 Markdown 文件中使用 Mermaid 建立各種複雜圖表,包括流程圖、序列圖、甘特圖、類別圖和狀態圖。

流程圖範例

流程圖非常適合用來表示流程或演算法步驟。

graph TD A[開始] --> B{條件檢查} B -->|是| C[處理步驟 1] B -->|否| D[處理步驟 2] C --> E[子流程] D --> E subgraph E [子流程詳細] E1[子步驟 1] --> E2[子步驟 2] E2 --> E3[子步驟 3] end E --> F{另一個決策} F -->|選項 1| G[結果 1] F -->|選項 2| H[結果 2] F -->|選項 3| I[結果 3] G --> J[結束] H --> J I --> J

序列圖範例

序列圖顯示物件之間隨時間變化的互動。

sequenceDiagram participant 使用者 participant 網頁應用程式 participant 伺服器 participant 資料庫 使用者->>網頁應用程式: 提交登入請求 網頁應用程式->>伺服器: 發送驗證請求 伺服器->>資料庫: 查詢使用者憑證 資料庫-->>伺服器: 回傳使用者資料 伺服器-->>網頁應用程式: 回傳驗證結果 alt 驗證成功 網頁應用程式->>使用者: 顯示歡迎頁面 網頁應用程式->>伺服器: 請求使用者資料 伺服器->>資料庫: 取得使用者偏好 資料庫-->>伺服器: 回傳偏好設定 伺服器-->>網頁應用程式: 回傳使用者資料 網頁應用程式->>使用者: 載入個人化介面 else 驗證失敗 網頁應用程式->>使用者: 顯示錯誤訊息 網頁應用程式->>使用者: 提示重新輸入 end

甘特圖範例

甘特圖非常適合用來顯示專案排程和時間軸。

gantt title 網站開發專案時間表 dateFormat YYYY-MM-DD axisFormat %m/%d section 設計階段 需求分析 :a1, 2023-10-01, 7d UI 設計 :a2, after a1, 10d 原型製作 :a3, after a2, 5d section 開發階段 前端開發 :b1, 2023-10-20, 15d 後端開發 :b2, after a2, 18d 資料庫設計 :b3, after a1, 12d section 測試階段 單元測試 :c1, after b1, 8d 整合測試 :c2, after b2, 10d 使用者驗收測試 :c3, after c2, 7d section 部署 正式環境部署 :d1, after c3, 3d 上線 :milestone, after d1, 0d

類別圖範例

類別圖顯示系統的靜態結構,包括類別、屬性、方法及其關係。

classDiagram class 使用者 { +String 使用者名稱 +String 密碼 +String 電子郵件 +Boolean 啟用中 +login() +logout() +updateProfile() } class 文章 { +String 標題 +String 內容 +Date 發布日期 +Boolean 已發布 +publish() +edit() +delete() } class 留言 { +String 內容 +Date 留言日期 +addComment() +deleteComment() } class 分類 { +String 名稱 +String 描述 +addArticle() +removeArticle() } 使用者 "1" -- "*" 文章 : 撰寫 使用者 "1" -- "*" 留言 : 發布 文章 "1" -- "*" 留言 : 擁有 文章 "1" -- "*" 分類 : 屬於

狀態圖範例

狀態圖顯示物件在其生命週期中經歷的狀態序列。

stateDiagram-v2 [*] --> 草稿 草稿 --> 審核中 : 提交 審核中 --> 草稿 : 退回 審核中 --> 已核准 : 核准 已核准 --> 已發布 : 發布 已發布 --> 已封存 : 封存 已發布 --> 草稿 : 撤回 state 已發布 { [*] --> 啟用中 啟用中 --> 隱藏 : 暫時隱藏 隱藏 --> 啟用中 : 恢復 啟用中 --> [*] 隱藏 --> [*] } 已封存 --> [*]

圓餅圖範例

圓餅圖非常適合用來顯示比例和百分比資料。

pie title 網站流量來源分析 "搜尋引擎" : 45.6 "直接訪問" : 30.1 "社群媒體" : 15.3 "推薦連結" : 6.4 "其他來源" : 2.6

結論

Mermaid 是一個強大的工具,可用於在 Markdown 文件中建立各種類型的圖表。本文示範了如何使用流程圖、序列圖、甘特圖、類別圖、狀態圖和圓餅圖。這些圖表可以幫助您更清晰地表達複雜的概念、流程和資料結構。

要使用 Mermaid,只需在程式碼區塊中指定 mermaid 語言,並使用簡潔的文字語法來描述圖表。Mermaid 會自動將這些描述轉換為美觀的視覺圖表。

在您的下一篇技術部落格文章或專案文件中嘗試使用 Mermaid 圖表吧——它們會讓您的內容更專業且更容易理解!

Complete Guide to Markdown with Mermaid Diagrams

This article demonstrates how to create various complex diagrams using Mermaid in Markdown documents, including flowcharts, sequence diagrams, Gantt charts, class diagrams, and state diagrams.

Flowchart Example

Flowcharts are excellent for representing processes or algorithm steps.

graph TD A[Start] --> B{Condition Check} B -->|Yes| C[Process Step 1] B -->|No| D[Process Step 2] C --> E[Subprocess] D --> E subgraph E [Subprocess Details] E1[Substep 1] --> E2[Substep 2] E2 --> E3[Substep 3] end E --> F{Another Decision} F -->|Option 1| G[Result 1] F -->|Option 2| H[Result 2] F -->|Option 3| I[Result 3] G --> J[End] H --> J I --> J

Sequence Diagram Example

Sequence diagrams show interactions between objects over time.

sequenceDiagram participant User participant WebApp participant Server participant Database User->>WebApp: Submit Login Request WebApp->>Server: Send Auth Request Server->>Database: Query User Credentials Database-->>Server: Return User Data Server-->>WebApp: Return Auth Result alt Auth Successful WebApp->>User: Show Welcome Page WebApp->>Server: Request User Data Server->>Database: Get User Preferences Database-->>Server: Return Preferences Server-->>WebApp: Return User Data WebApp->>User: Load Personalized Interface else Auth Failed WebApp->>User: Show Error Message WebApp->>User: Prompt Re-entry end

Gantt Chart Example

Gantt charts are perfect for displaying project schedules and timelines.

gantt title Website Development Project Timeline dateFormat YYYY-MM-DD axisFormat %m/%d section Design Phase Requirements Analysis :a1, 2023-10-01, 7d UI Design :a2, after a1, 10d Prototype Creation :a3, after a2, 5d section Development Phase Frontend Development :b1, 2023-10-20, 15d Backend Development :b2, after a2, 18d Database Design :b3, after a1, 12d section Testing Phase Unit Testing :c1, after b1, 8d Integration Testing :c2, after b2, 10d User Acceptance Testing :c3, after c2, 7d section Deployment Production Deployment :d1, after c3, 3d Launch :milestone, after d1, 0d

Class Diagram Example

Class diagrams show the static structure of a system, including classes, attributes, methods, and their relationships.

classDiagram class User { +String username +String password +String email +Boolean active +login() +logout() +updateProfile() } class Article { +String title +String content +Date publishDate +Boolean published +publish() +edit() +delete() } class Comment { +String content +Date commentDate +addComment() +deleteComment() } class Category { +String name +String description +addArticle() +removeArticle() } User "1" -- "*" Article : writes User "1" -- "*" Comment : posts Article "1" -- "*" Comment : has Article "1" -- "*" Category : belongs to

State Diagram Example

State diagrams show the sequence of states an object goes through during its life cycle.

stateDiagram-v2 [*] --> Draft Draft --> UnderReview : submit UnderReview --> Draft : reject UnderReview --> Approved : approve Approved --> Published : publish Published --> Archived : archive Published --> Draft : retract state Published { [*] --> Active Active --> Hidden : temporarily hide Hidden --> Active : restore Active --> [*] Hidden --> [*] } Archived --> [*]

Pie Chart Example

Pie charts are ideal for displaying proportions and percentage data.

pie title Website Traffic Sources Analysis "Search Engines" : 45.6 "Direct Access" : 30.1 "Social Media" : 15.3 "Referral Links" : 6.4 "Other Sources" : 2.6

Conclusion

Mermaid is a powerful tool for creating various types of diagrams in Markdown documents. This article demonstrated how to use flowcharts, sequence diagrams, Gantt charts, class diagrams, state diagrams, and pie charts. These diagrams can help you express complex concepts, processes, and data structures more clearly.

To use Mermaid, simply specify the mermaid language in a code block and describe the diagram using concise text syntax. Mermaid will automatically convert these descriptions into beautiful visual diagrams.

Try using Mermaid diagrams in your next technical blog post or project documentation - they will make your content more professional and easier to understand!

使用 Mermaid 圖表在 Markdown 中的完整指南
/zh-TW/posts/mermaid-example/
作者
MuxBuffer
发布于
2011-11-02
许可协议
CC BY-NC-SA 4.0
AI參與指數 IIIA-0
分類 未經AI製作
等級 0
簡介 完全未使用AI

此页面可能不是最新版本。