通过代码优雅地编写图表——Mermaid语法

news2024/11/26 13:55:30

通过代码优雅地编写图表——Mermaid语法

使用代码,在你的Typora中优雅地编写图表!

先看一个示例:

gantt
dateFormat  YYYY-MM-DD
title Adding GANTT diagram to mermaid
excludes weekdays 2023-01-10

section A section
Completed task            :done,    des1, 2023-01-06,2023-01-08
Active task               :active,  des2, 2023-01-09, 3d
Future task               :         des3, after des2, 5d
Future task2              :         des4, after des3, 5d
2023-01-07 2023-01-09 2023-01-11 2023-01-13 2023-01-15 2023-01-17 2023-01-19 2023-01-21 2023-01-23 Completed task Active task Future task Future task2 A section Adding GANTT diagram to mermaid

简介

mermaid 是一种编写图表的语法,可以让你以代码的形式编写流程图、饼图、甘特图等,并在支持该语法的编辑器(如 Typora)中渲染为对应图表。

其调用方法与代码块一致:

mermaid 语法的基本结构为:关键字 + 节点 + 关系

例如我们绘制一张简单的流程图,其语法为:

---
title: flowchart 1
---
flowchart LR
    A-->B
    A-->C
    B-->D
    C-->D
---
title: flowchart 1
---
flowchart LR
    A-->B
    A-->C
    B-->D
    C-->D

流程图绘制

流程图的类型关键字为 graph XXflowchart XX

其中,XX 用于指定流程图的方向,包括:

关键字含义对应方向
TB / TDTop to Bottom / Top to Down从上到下
BTBottom to Top从下到上
RLRight to Left从右到左
LRLeft to Right从左到右

节点

流程图的节点由三部分组成:

key[value]

其中:

  • key 用于调用该节点,每一节点的 key 是唯一的;
  • value 为该节点显示在图表中显示的,不设置 value 时,将 key 作为默认的 value 显示;
  • [] 表示该节点的形状,不设置时默认为矩形;
flowchart TB
    key-A
    key-B[value-B]
    key-C(value-C)
key-A
value-A
value-A
节点的 key - value

节点的 key 是唯一的,对某一 keyvalue 进行定义后,后续可以直接调用,而不用重复定义 value

flowchart TB
	key1[value1]
	key2[value2]
	key1 --- key2[value2]
value1
value2

当一张图表中出现两次对于同一 key 的定义时,后面的定义将覆盖前面的定义:

flowchart TB
	key[v1]
	key[v2]
v2

节点形状

节点的形状共有 13 种,其关键字分别为:

关键字形状关键字形状
[]矩形>]标签形
()圆角矩形{}菱形
[[]]双层侧边矩形{{}}六边形
[()]圆柱形[//]右平行四边形
([])椭圆形[\\]左平行四边形
(())圆形[/\]梯形
((()))双层圆形[\/]倒梯形
flowchart TB
	A[A]
	B(B)
	C[[C]]
	D[(D)]
	E([E])
	F((F))
	G(((G)))
flowchart TB
	A[A]
	B(B)
	C[[C]]
	D[(D)]
	E([E])
	F((F))
	G(((G)))
flowchart TB
	A>A]
	B{B}
	C{{C}}
	D[/D/]
	E[\E\]
	F[/F\]
	G[\G/]
A
B
C
D
E
F
G

关系

节点间的关系共有 8 种,其关键字分别为:

关键字形状关键字形状
-标准实线o圆头
> <箭头x叉头
.虚线`text
=加粗实线~不可见实线

这些关键字以组合的形式呈现,不同的组合在线型、端点、长短等方面呈现出不同的效果:

flowchart TB
	A1 --- B1
	A2 --> B2
	A3 ---|text| B3
	A4 -->|text| B4
	A5 -.-> B5
	A6 -.->|text| B6
text
text
text
A1
B1
A2
B2
A3
B3
A4
B4
A5
B5
A6
B6
flowchart TB
	A7 ==> B7
	A8 ==>|text| B8
	A9 ~~~ B9
	A10 ~~~|text| B10
	A11 --o B11
	A12 --x B12
flowchart TB
	A7 ==> B7
	A8 ==>|text| B8
	A9 ~~~ B9
	A10 ~~~|text| B10
	A11 --o B11
	A12 --x B12
flowchart TB
	A13 o--o B13
	A14 x--x B14
	A15 x---x B15
	A16 ----> B16
	A17 -...- B17
	A18 ====> B18
A13
B13
A14
B14
A15
B15
A16
B16
A17
B17
A18
B18

冲突的特殊字符

我们注意到,在语法中 [] () 等字符被赋予了特殊的意义,如果我们想在 value 中正确显示这些字符,则需要使用 "" 包裹 value

flowchart TB
	key-right["[正确显示]"]
	key-wrong[[错误显示]]
[正确显示]
错误显示

示例

flowchart LR
    A([Start]) --> B{一会儿没课?}
    B -->|Yes| C[睡觉]
    C --> D[吃东西]
    D --> B
    B --->|No| E[去上课]
    E --> F([End])
Yes
No
Start
一会儿没课?
睡觉
吃东西
去上课
End

子图的绘制

可以在流程图中使用关键字 subgraph title ... end 定义子图:

flowchart LR
  subgraph TOP
    direction TB
    subgraph B1
        direction RL
        i1 -->f1
    end
    subgraph B2
        direction BT
        i2 -->f2
    end
  end
  A --> TOP --> B
  B1 --> B2
TOP
B1
f1
i1
B2
f2
i2
A
B

其他类型图表示例

可以访问 Mermaid 官方的文档查看更多的图表类型及它们的语法,这里给出部分示例:

Sequence diagram

sequenceDiagram
    participant Alice
    participant Bob
    Alice->>John: Hello John, how are you?
    loop Healthcheck
        John->>John: Fight against hypochondria
    end
    Note right of John: Rational thoughts <br/>prevail!
    John-->>Alice: Great!
    John->>Bob: How about you?
    Bob-->>John: Jolly good!
Alice Bob John Hello John, how are you? Fight against hypochondria loop [Healthcheck] Rational thoughts prevail! Great! How about you? Jolly good! Alice Bob John

Gantt diagram

gantt
dateFormat  YYYY-MM-DD
title Adding GANTT diagram to mermaid
excludes weekdays 2014-01-10

section A section
Completed task            :done,    des1, 2014-01-06,2014-01-08
Active task               :active,  des2, 2014-01-09, 3d
Future task               :         des3, after des2, 5d
Future task2               :         des4, after des3, 5d
2014-01-07 2014-01-09 2014-01-11 2014-01-13 2014-01-15 2014-01-17 2014-01-19 2014-01-21 2014-01-23 Completed task Active task Future task Future task2 A section Adding GANTT diagram to mermaid

Git graph

gitGraph
       commit
       commit
       branch develop
       commit
       commit
       commit
       checkout main
       commit
       commit

Entity Relationship Diagram - ❗ experimental

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains
    CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
CUSTOMER ORDER LINE-ITEM DELIVERY-ADDRESS places contains uses

User Journey Diagram

journey
    title My working day
    section Go to work
      Make tea: 5: Me
      Go upstairs: 3: Me
      Do work: 1: Me, Cat
    section Go home
      Go downstairs: 5: Me
      Sit down: 5: Me
Cat Me
Go to work
Go to work
Me
Make tea
Make tea
Me
Go upstairs
Go upstairs
Me Cat
Do work
Do work
Go home
Go home
Me
Go downstairs
Go downstairs
Me
Sit down
Sit down
My working day

Quadrant Chart

quadrantChart
    title Reach and engagement of campaigns
    x-axis Low Reach --> High Reach
    y-axis Low Engagement --> High Engagement
    quadrant-1 We should expand
    quadrant-2 Need to promote
    quadrant-3 Re-evaluate
    quadrant-4 May be improved
    Campaign A: [0.3, 0.6]
    Campaign B: [0.45, 0.23]
    Campaign C: [0.57, 0.69]
    Campaign D: [0.78, 0.34]
    Campaign E: [0.40, 0.34]
    Campaign F: [0.35, 0.78]
quadrantChart
    title Reach and engagement of campaigns
    x-axis Low Reach --> High Reach
    y-axis Low Engagement --> High Engagement
    quadrant-1 We should expand
    quadrant-2 Need to promote
    quadrant-3 Re-evaluate
    quadrant-4 May be improved
    Campaign A: [0.3, 0.6]
    Campaign B: [0.45, 0.23]
    Campaign C: [0.57, 0.69]
    Campaign D: [0.78, 0.34]
    Campaign E: [0.40, 0.34]
    Campaign F: [0.35, 0.78]

这个为啥渲染不出来呢


Pie chart diagrams

pie title Pets adopted by volunteers
    "Dogs" : 386
    "Cats" : 85
    "Rats" : 15
79% 17% 3% Pets adopted by volunteers Dogs Cats Rats

Mindmap

mindmap
  root((mindmap))
    Origins
      Long history
      ::icon(fa fa-book)
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectiveness<br/>and features
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid
mindmap
  root((mindmap))
    Origins
      Long history
      ::icon(fa fa-book)
      Popularisation
        British popular psychology author Tony Buzan
    Research
      On effectiveness<br/>and features
      On Automatic creation
        Uses
            Creative techniques
            Strategic planning
            Argument mapping
    Tools
      Pen and paper
      Mermaid

这个居然也没渲染出来

Timeline

timeline
    title History of Social Media Platform
    2002 : LinkedIn
    2004 : Facebook
         : Google
    2005 : Youtube
    2006 : Twitter
timeline
    title History of Social Media Platform
    2002 : LinkedIn
    2004 : Facebook
         : Google
    2005 : Youtube
    2006 : Twitter

SanKey

---
config:
  sankey:
    showValues: false
---
sankey-beta

Agricultural 'waste',Bio-conversion,124.729
Bio-conversion,Liquid,0.597
Bio-conversion,Losses,26.862
Bio-conversion,Solid,280.322
Bio-conversion,Gas,81.144
Biofuel imports,Liquid,35
Biomass imports,Solid,35
Coal imports,Coal,11.606
Coal reserves,Coal,63.965
Coal,Solid,75.571
District heating,Industry,10.639
District heating,Heating and cooling - commercial,22.505
District heating,Heating and cooling - homes,46.184
Electricity grid,Over generation / exports,104.453
Electricity grid,Heating and cooling - homes,113.726
Electricity grid,H2 conversion,27.14
Electricity grid,Industry,342.165
Electricity grid,Road transport,37.797
Electricity grid,Agriculture,4.412
Electricity grid,Heating and cooling - commercial,40.858
Electricity grid,Losses,56.691
Electricity grid,Rail transport,7.863
Electricity grid,Lighting & appliances - commercial,90.008
Electricity grid,Lighting & appliances - homes,93.494
Gas imports,Ngas,40.719
Gas reserves,Ngas,82.233
Gas,Heating and cooling - commercial,0.129
Gas,Losses,1.401
Gas,Thermal generation,151.891
Gas,Agriculture,2.096
Gas,Industry,48.58
Geothermal,Electricity grid,7.013
H2 conversion,H2,20.897
H2 conversion,Losses,6.242
H2,Road transport,20.897
Hydro,Electricity grid,6.995
Liquid,Industry,121.066
Liquid,International shipping,128.69
Liquid,Road transport,135.835
Liquid,Domestic aviation,14.458
Liquid,International aviation,206.267
Liquid,Agriculture,3.64
Liquid,National navigation,33.218
Liquid,Rail transport,4.413
Marine algae,Bio-conversion,4.375
Ngas,Gas,122.952
Nuclear,Thermal generation,839.978
Oil imports,Oil,504.287
Oil reserves,Oil,107.703
Oil,Liquid,611.99
Other waste,Solid,56.587
Other waste,Bio-conversion,77.81
Pumped heat,Heating and cooling - homes,193.026
Pumped heat,Heating and cooling - commercial,70.672
Solar PV,Electricity grid,59.901
Solar Thermal,Heating and cooling - homes,19.263
Solar,Solar Thermal,19.263
Solar,Solar PV,59.901
Solid,Agriculture,0.882
Solid,Thermal generation,400.12
Solid,Industry,46.477
Thermal generation,Electricity grid,525.531
Thermal generation,Losses,787.129
Thermal generation,District heating,79.329
Tidal,Electricity grid,9.452
UK land based bioenergy,Bio-conversion,182.01
Wave,Electricity grid,19.013
Wind,Electricity grid,289.366
---
config:
  sankey:
    showValues: false
---
sankey-beta

Agricultural 'waste',Bio-conversion,124.729
Bio-conversion,Liquid,0.597
Bio-conversion,Losses,26.862
Bio-conversion,Solid,280.322
Bio-conversion,Gas,81.144
Biofuel imports,Liquid,35
Biomass imports,Solid,35
Coal imports,Coal,11.606
Coal reserves,Coal,63.965
Coal,Solid,75.571
District heating,Industry,10.639
District heating,Heating and cooling - commercial,22.505
District heating,Heating and cooling - homes,46.184
Electricity grid,Over generation / exports,104.453
Electricity grid,Heating and cooling - homes,113.726
Electricity grid,H2 conversion,27.14
Electricity grid,Industry,342.165
Electricity grid,Road transport,37.797
Electricity grid,Agriculture,4.412
Electricity grid,Heating and cooling - commercial,40.858
Electricity grid,Losses,56.691
Electricity grid,Rail transport,7.863
Electricity grid,Lighting & appliances - commercial,90.008
Electricity grid,Lighting & appliances - homes,93.494
Gas imports,Ngas,40.719
Gas reserves,Ngas,82.233
Gas,Heating and cooling - commercial,0.129
Gas,Losses,1.401
Gas,Thermal generation,151.891
Gas,Agriculture,2.096
Gas,Industry,48.58
Geothermal,Electricity grid,7.013
H2 conversion,H2,20.897
H2 conversion,Losses,6.242
H2,Road transport,20.897
Hydro,Electricity grid,6.995
Liquid,Industry,121.066
Liquid,International shipping,128.69
Liquid,Road transport,135.835
Liquid,Domestic aviation,14.458
Liquid,International aviation,206.267
Liquid,Agriculture,3.64
Liquid,National navigation,33.218
Liquid,Rail transport,4.413
Marine algae,Bio-conversion,4.375
Ngas,Gas,122.952
Nuclear,Thermal generation,839.978
Oil imports,Oil,504.287
Oil reserves,Oil,107.703
Oil,Liquid,611.99
Other waste,Solid,56.587
Other waste,Bio-conversion,77.81
Pumped heat,Heating and cooling - homes,193.026
Pumped heat,Heating and cooling - commercial,70.672
Solar PV,Electricity grid,59.901
Solar Thermal,Heating and cooling - homes,19.263
Solar,Solar Thermal,19.263
Solar,Solar PV,59.901
Solid,Agriculture,0.882
Solid,Thermal generation,400.12
Solid,Industry,46.477
Thermal generation,Electricity grid,525.531
Thermal generation,Losses,787.129
Thermal generation,District heating,79.329
Tidal,Electricity grid,9.452
UK land based bioenergy,Bio-conversion,182.01
Wave,Electricity grid,19.013
Wind,Electricity grid,289.366

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1070674.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

快速了解SpringCloud Sleuth --链路追踪 + Zipkin--数据搜集/存储/可视化

&#x1f600;前言 本篇博文是关于SpringCloud Sleuth --链路追踪 Zipkin–数据搜集/存储/可视化的基本介绍和使用&#xff0c;希望你能够喜欢 &#x1f3e0;个人主页&#xff1a;晨犀主页 &#x1f9d1;个人简介&#xff1a;大家好&#xff0c;我是晨犀&#xff0c;希望我的文…

常见源协议介绍

开源协议&#xff08;Open Source License&#xff09;是一种法律文档&#xff0c;用于规定如何使用、修改和分发开源软件和其他开源项目的规则和条件。这些协议允许创作者或组织将其创造的代码或作品以开放源代码的形式共享给他人&#xff0c;以促进协作、创新和知识共享。常见…

HttpClient实现爬虫开发

网络爬虫是一种高效获取网络信息的方式&#xff0c;而HttpClient是一个强大而灵活的Java库&#xff0c;提供了方便的API和丰富的功能&#xff0c;使其成为开发高效且灵活的网络爬虫的理想选择。本文将分享如何利用HttpClient库进行网络爬虫开发&#xff0c;帮助您更好地理解并实…

NSA 和 CISA 联合揭露当下十大网络安全错误配置

10月5日&#xff0c;美国国家安全局 (NSA) 和网络安全与基础设施安全局 (CISA) 公布了十大目前最常见的网络安全错误配置&#xff0c;这些错误由红蓝团队在大型组织网络中发现。 根据发布的联合报告&#xff0c;团队评估了国防部 (DoD)、联邦民事行政部门 (FCEB)、州和地方政府…

解决程序员百分百问题pip终极杀手

文章目录 pip中国版镜像源永久版中国镜像源pip添加多个中国版镜像源无敌版pip中的小细节 pip中国版镜像源 Python中使用pip安装包&#xff0c;常用的国内镜像有&#xff1a; 例如&#xff1a; Python中使用pip安装包&#xff0c;常用的国内镜像有&#xff1a;清华大学镜像&am…

Mac 点击桌面 出现黑边框 解决

1、桌面黑框效果 2、解决&#xff1a;设置为 仅在台前调度中

电脑散热——液金散热

目录 1.简介 2.传统硅脂与液金导热区别 3.特点 4.优点 5.为什么液金技术名声不太好 6.使用方法 1.简介 凡是对于电脑基础硬件有所了解的人&#xff0c;都知道硅脂是如今高性能电脑设备中必不可少的东西。芯片表面和散热器接触面&#xff0c;虽然肉眼看上去是非常光滑的金属…

vue实现拖拽排序

在业务中列表拖拽排序是比较常见的需求&#xff0c;常见的JS拖拽库有Sortable.js&#xff0c;Vue.Draggable等&#xff0c;大多数同学遇到这种需求也是更多的求助于这些JS库&#xff0c;其实&#xff0c;使用HTML原生的拖放事件来实现拖拽排序并不复杂&#xff0c;结合Vue的tra…

【运维笔记】Docker 安装Kibana-7.4.0(在线Docker版)

一、准备工作&#xff1a; Centos 7.5 安装 Docker-24.0.6 详细步骤&#xff08;避坑版&#xff09;&#xff1a; https://blog.csdn.net/seesun2012/article/details/133674191注意1&#xff1a;本文的命令使用的是 root 用户登录执行&#xff0c;不是 root 的话所有命令前面…

山西电力市场日前价格预测【2023-10-09】

日前价格预测 预测说明&#xff1a; 如上图所示&#xff0c;预测明日&#xff08;2023-10-09&#xff09;山西电力市场全天平均日前电价为575.84元/MWh。其中&#xff0c;最高日前电价为1500.00元/MWh&#xff0c;预计出现在17: 30-20: 00。最低日前电价为218.27元/MWh&#x…

go的面向对象学习

文章目录 面向对象编程(上)1.问题与解决思路2.结构体1》Golang语言面向对象编程说明2》结构体与结构体变量(实例/对象)的关系的示意图3》入门案例(using struct to solve the problem of cat growing) 3.结构体的具体应用4.创建结构体变量和访问结构体字段5.struct类型的内存分…

Snowflake:一家由数据驱动的生成式人工智能公司

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 总结&#xff1a; &#xff08;1&#xff09;从长远来看&#xff0c;生成式人工智能只会进一步增加数据的重要性。 &#xff08;2&#xff09;尽管宏观环境面临挑战&#xff0c;但Snowflake(SNOW)仍然保持着强劲的增长率。 …

Map,Set和哈希表的使用

目录 两种模型 Map的使用 Map接口方法的使用 注意事项 Set的使用 哈希表 冲突 如何避免冲突 在我们日常生活中,会进行一些查找操作,比如根据姓名查询考试成绩,根据姓名查询联系方式等在查找是进行一些插入和删除操作,即动态查找. 而Map和Set是一种适合动态查找的集合容…

springboot整合thymeleaf模板引擎

1.什么是thyeleaf模板引擎 Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 内容的模板引擎。 是新一代 Java 模板引擎&#xff0c;它支持 HTML 原型&#xff0c;其文件后缀为“.html”&#xff0c;因此它可以直接被浏览器打开&#xff0c;此时浏览器会忽略未定义的 Thymeleaf 标签…

C语言:字符字符串

目录 字符 字符串 输出各字符串 输出各类型长度——strlen函数返回字符串长度&#xff0c;需指定头文件 字符 a;char ch z;// char ch "z"; // err 字符类型只能是单引号// char ch zh; // err 字符类型只能是单个字符 字符串 这种由双引号&#xff08;Doub…

SpringBoot+Vue3外卖项目构思

SpringBoot的学习&#xff1a; SpringBoot的学习_明里灰的博客-CSDN博客 实现功能 前台 用户注册&#xff0c;邮箱登录&#xff0c;地址管理&#xff0c;历史订单&#xff0c;菜品规格&#xff0c;购物车&#xff0c;下单&#xff0c;菜品浏览&#xff0c;评价&#xff0c;…

软考程序员考试大纲(2023)

文章目录 前言一、考试说明1.考试目标2.考试要求3&#xff0e;考试科目设置 二、考试范围考试科目1&#xff1a;计算机与软件工程基本知识1&#xff0e;计算机科学基础2&#xff0e;计算机系统基础知识3&#xff0e;系统开发和运行知识4&#xff0e;网络与信息安全基础知识5&am…

你的librosa和scikit-learn打架了吗?

被这个问题困扰好久&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 我的原来版本librosa0.7.1 和 scikit-learn1.3.1 一直拆了按&#xff0c;按…

好物周刊#19:开源指北

https://github.com/cunyu1943/JavaPark https://yuque.com/cunyu1943 村雨遥的好物周刊&#xff0c;记录每周看到的有价值的信息&#xff0c;主要针对计算机领域&#xff0c;每周五发布。 一、项目 1. Vditor 一款浏览器端的 Markdown 编辑器&#xff0c;支持所见即所得、…

Android---Class 对象在执行引擎中的初始化过程

一个 class 文件被加载到内存中的步骤如下图所示&#xff1a; 装载 装载是指 Java 虚拟机查找 .class 文件并生成字节流&#xff0c;然后根据字节流创建 java.lang.Class 对象的过程。 1. ClassLoader 通过一个类的全限定名&#xff08;包名类名&#xff09;来查找 .class 文件…