社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

Github 78K star!使用文本语法轻松绘制专业图表,程序员必备!

编程技术进阶 • 2 周前 • 35 次点击  

还在为画图表头疼吗?一边改代码,一边还要更新文档和图表,简直让人抓狂!别担心,Mermaid来救场了!

这个神奇的工具让你只需写几行类似Markdown的文本,就能生成专业漂亮的流程图、时序图、甘特图等。

不用再纠结于拖拽图形元素,不用再为修改一个小细节重新设计整张图。写代码的你,写文档的你,做演示的你,都能用Mermaid省下大把时间。

图片

Mermaid是一个基于JavaScript的图表绘制工具,它允许用户使用类似Markdown的文本语法来创建和修改各种图表。通过简单的文本定义,Mermaid能够生成流程图、时序图、甘特图、类图、状态图等多种可视化图表,极大地简化了图表创建过程。

图片

核心特点

  • 简单易用:使用类Markdown语法,无需专业绘图工具
  • 快速创建:几行代码即可生成专业图表
  • 易于维护:文本定义使图表修改变得简单高效
  • 多种图表类型:支持流程图、时序图、甘特图、类图、状态图等
  • 与开发流程集成:可作为生产脚本的一部分
  • 跨平台兼容:支持多种环境和集成方式

使用方式

1、在线编辑器

最简单的方式是使用Mermaid Live Editor,它允许您实时编辑和预览图表,无需安装任何软件。

体验地址:https://mermaid.live/edit

图片

2、Markdown中使用

许多支持Markdown的平台已集成Mermaid支持,只需在代码块中指定语言为"mermaid"

flowchart LR
A[开始] --> B{是否继续?}
B -->|是| C[执行操作]
B -->|否| D[结束]
C --> B

3、在网页中集成

要在HTML页面中集成Mermaid,只需引入Mermaid的JavaScript库并添加带有"mermaid"类的




"mermaid">
flowchart LR
    A[开始] --> B{是否继续?}
    B -->|是| C[执行操作]
    B -->|否| D[结束]
    C --> B

使用案例

1、流程图

流程图用于表示工作流程或过程。Mermaid的流程图语法简洁明了

flowchart LR
A[Hard] -->|Text| B(Round)
B --> C{Decision}
C -->|One| D[Result 1]
C -->|Two| E[Result 2]
图片

2、时序图

时序图展示对象之间的交互和消息传递顺序,特别适合描述系统组件间的通信

sequenceDiagram
Alice->>John: Hello John, how are you?
loop HealthCheck
    John->>John: Fight against hypochondria
end
Note right of John: Rational thoughts!
John-->>Alice: Great!
John->>Bob: How about you?
Bob-->>John: Jolly good!
图片

3、甘特图

甘特图用于项目管理,显示项目、任务和里程碑的时间线

gantt
    section Section
    Completed :done,    des1, 2014-01-06,2014-01-08
    Active        :active,  des2, 2014-01-07, 3d
    Parallel 1   :         des3, after des1, 1d
    Parallel 2   :         des4, after des1, 1d
    Parallel 3   :         des5, after des3, 1d
    Parallel 4   :         des6, after des4, 1d
图片

4、类图

类图用于描述系统的静态结构,展示类、接口及其关系

classDiagram
Class01
<> Class01
Class09 --> C2 : Where am I?
Class09 --* C3
Class09 --|> Class07
Class07 : equals()
Class07 : Object[] elementData
Class01 : size()
Class01 : int chimp
Class01 : int gorilla
class Class10 {
  <>
  int id
  size()
}
图片

5、状态图

状态图描述对象在其生命周期中的不同状态及转换

stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*]
图片

6、饼图

饼图用于展示数据的比例关系

pie
"Dogs" : 386
"Cats" : 85
"Rats" : 15
图片

7、C4图

C4Context
title System Context diagram for Internet Banking System

Person(customerA, "Banking Customer A""A customer of the bank, with personal bank accounts.")
Person(customerB, "Banking Customer B")
Person_Ext(customerC, "Banking Customer C")
System(SystemAA, "Internet Banking System""Allows customers to view information about their bank accounts, and make payments.")

Person(customerD, "Banking Customer D""A customer of the bank,
with personal bank accounts."
)

Enterprise_Boundary(b1, "BankBoundary") {

  SystemDb_Ext(SystemE, "Mainframe Banking System""Stores all of the core banking information about customers, accounts, transactions, etc.")

  System_Boundary(b2, "BankBoundary2") {
    System(SystemA, "Banking System A")
    System(SystemB, "Banking System B""A system of the bank, with personal bank accounts.")
  }

  System_Ext(SystemC, "E-mail system""The internal Microsoft Exchange e-mail system.")
  SystemDb(SystemD, "Banking System D Database""A system of the bank, with personal bank accounts.")

  Boundary(b3, "BankBoundary3""boundary") {
    SystemQueue(SystemF, "Banking System F Queue""A system of the bank, with personal bank accounts.")
    SystemQueue_Ext(SystemG, "Banking System G Queue""A system of the bank, with personal bank accounts.")
  }
}

BiRel(customerA, SystemAA, "Uses")
BiRel(SystemAA, SystemE, "Uses")
Rel(SystemAA, SystemC, "Sends e-mails""SMTP")
Rel(SystemC, customerA, "Sends e-mails to")
图片

最后

Mermaid为开发者、产品经理、文档编写者提供了一种简单而强大的工具,通过代码创建专业图表。不仅节省了图表创建和维护的时间,还使图表与代码一样容易版本控制和协作。

Github仓库:https://github.com/mermaid-js/mermaid


AI时代,我们还为大家准备了Deepseek资料库,长按扫码发送 312 即可获取。

图片

来都来了,点个在看再走吧~~~



图片

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/181106
 
35 次点击