作者:黄永刚

mermaid简介

当撰写文档的时候,对于流程图的生成大多使用Visio等繁重的工具,没有一种轻便的工具能够画图从而简化文档的编写,就像markdown那样。

mermaid解决这个痛点,这是一个类似markdown语法的脚本语言,通过JavaScript实现图表的生成。

先来看个例子:

1.流程图(flowchart)

graph LR;
  A-->B;
  A-->C;
  B-->D;
  C-->D;

生成的图表如下所示:



2. 时序图(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!

生成的图表如下所示:

3.甘特图(gantt diagram)

gantt
   dateFormat YYYY-MM-DD
   title Adding GANTT diagram functionality to mermaid
   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
   section Critical tasks
   Completed task in the critical line :crit, done, 2014-01-06,24h
   Implement parser and json      :crit, done, after des1, 2d
   Create tests for parser       :crit, active, 3d
   Future task in critical line     :crit, 5d
   Create tests for renderer      :2d
   Add to ,mermaid           :1d

生成的表如下:


下游项目

Mermaid 是由Knut Sveidqbist发起旨在轻便化的文档撰写。所有开发者:开发者列表

Graph

graph LR
A --> B



这是申明一个由左到右,水平向右的图。\

可能方向有:

- TB - top bottom

- BT - bottom top

- RL - right left

- LR - left right

- TD - same as TB


节点与形状

默认节点

graph LR

id1



注意:’id’显示在节点内部。

文本节点

graph LR
id[This is the text in the box];
圆角节点

graph LR
id(This is the text in the box);
圆节点(The form of a circle)

graph LR
id((This is the text in the circle));
非对称节点(asymetric shape)

graph LR
id>This is the text in the box]
菱形节点(rhombus)

graph LR
id{This is the text in the box}

连接线

节点间的连接线有多种形状,而且可以在连接线中加入标签:

箭头形连接

graph LR;
A-->B;
开放行连接

graph LR
A --- B
标签连接

graph LR
A -- This is the label text --- B;
箭头标签连接

A–>|text|B\

或者\

A– text –>B

graph LR
A-- text -->B
虚线(dotted link,点连线)

-.->

graph LR
A-.->B

-.-.

graph LR
A-.-.B
标签虚线

-.text.->

graph LR
A-.text.->B

粗实线

==>

graph LR
A==>B

===

graph LR
A===B

标签粗线

=\=text\==>

graph LR
A==text==>B

=\=text\===

graph LR
A==text===B


特殊的语法

使用引号可以抑制一些特殊的字符的使用,可以避免一些不必要的麻烦。

graph LR\

d1[“This is the (text) in the box”]

graph LR
d1["This is the (text) in the box"]

html字符的转义字符

转义字符的使用语法:

流程图定义如下:

graph LR\

A[“A double quote:#quot;”] –> B[“A dec char:#9829;”]

渲染后的图如下:

graph LR
A["A double quote:#quot;"]-->B["A dec char:#9829;"]

子图(Subgraphs)

subgraph title\

graph definition\

end

示例:

graph TB
subgraph one
a1 --> a2
en
subgraph two
b2 --> b2
end
subgraph three
c1 --> c2
end
c1 --> a2

结果:

基础fontawesome支持

如果想加入来自frontawesome的图表字体,需要像frontawesome网站上那样引用的那样。\

详情请点击:fontawdsome

引用的语法为:++fa:#icon class name#++

graph TD
B["fa:fa-twitter for peace"]
B-->C[fa:fa-ban forbidden]
B-->D(fa:fa-spinner);
B-->E(A fa:fa-camerra-retro perhaps?);

渲染图如下:

graph TD
B["fa:fa-twitter for peace"]
B-->C[fa:fa-ban forbidden]
B-->D(fa:fa-spinner);
B-->E(A fa:fa-camera-retro perhaps?);

以上reference:

1.mermaid docs


第二部分—图表(graph)


定义连接线的样式
graph LR
id1(Start)-->id2(Stop)
style id1 fill:#f9f,stroke:#333,stroke-width:4px;
style id2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray:5,5;

渲染结果:

graph LR
id1(Start)-->id2(Stop)
style id1 fill:#f9f,stroke:#333,stroke-width:4px;
style id2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray:5,5;

备注:这些样式参考CSS样式。

样式类

为了方便样式的使用,可以定义类来使用样式

类的定义示例:

classDef className fill:#f9f,stroke:#333,stroke-width:4px;

对节点使用样式类:

class nodeId className;

同时对多个节点使用相同的样式类:

class nodeId1,nodeId2 className;

可以在CSS中提前定义样式类,应用在图表的定义中。

graph LR
A-->B[AAABBB];
B-->D;
class A cssClass;

默认样式类:\

当没有指定样式的时候,默认采用。

classDef default fill:#f9f,stroke:#333,stroke-width:4px;

示例:

graph LR
classDef default fill:#f90,stroke:#555,stroke-width:4px;
id1(Start)-->id2(Stop)

结果:

graph LR
classDef default fill:#f90,stroke:#555,stroke-width:4px;
id1(Start)-->id2(Stop)

序列图(sequence diagram)1

序列图

示例:

sequenceDiagram
  Alice->>John: Hello John, how are you ?
  John-->>Alice: Great!
  Alice--->>John: Huang,you are better .
  John-->>Alice: yeah, Just not bad.
sequenceDiagram
  Alice->>John: Hello John, how are you ?
  John-->>Alice: Great!
  Alice->>John: Hung,you are better .
  John-->>Alice: yeah, Just not bad.



观察上面的图,如果想让John出现在前面,如何控制,mermaid通过设定参与者(participants)的顺序控制二者的顺序。上面的图可以做如下修改:

sequenceDiagram\

  participant John\

  participant Alice\

  Alice->>John:Hello John,how are you?\

  John–>>Alice:Great!

sequenceDiagram
  participant John
  participant Alice
  Alice-xJohn:Hello John,how are you?
  John-->>Alice:Great!



消息的语法:

  实线或者虚线的使用:

[Actor][Arrow][Actor]:Message text\

Arrow的六种样式:

  • ->
  • –>
  • ->>
  • –>>
  • -x
  • –x

示例:

sequenceDiagram
Alice->John: Hello John, how are you ?
John-->Alice:Great!
Alice->>John: dont borther me !
John-->>Alice:Great!
Alice-xJohn: wait!
John--xAlice: Ok!

便签

给序列图增加便签:\

具体规则:\

[right of | left of | over][Actor]:Text\

示例:

sequenceDiagram
  participant John
  Note left of John: Text in note

结果:

跨越两个Actor的便签:

sequenceDiagram
  Alice->John:Hello John, how are you?
  Note over Alice,John:A typical interaction
sequenceDiagram
Alice->>John:Hello John, how are you?
Note over Alice,John:A typical interaction

循环Loops

在序列图中,也可以使用循环,具体规则如下:

loop Loop text
... statements...
end

示例:

sequenceDiagram
  Alice->>John: Hello!
  loop Reply every minute
    John->>Alice:Great!
  end

渲染结果:

选择ALT

在序列图中选择的表达。规则如下:

alt Describing text
...statements...
else
...statements...
end

或者使用opt(推荐在没有else的情况下使用)

opt Describing text
...statements...
end

示例:

sequenceDiagram
  Alice->>Bob: Hello Bob, how are you?
  alt is sick
    Bob->>Alice:not so good :(
  else is well
    Bob->>Alice:Feeling fresh like a daisy:)
  end
  opt Extra response
    Bob->>Alice:Thanks for asking
  end

渲染结果如下:


甘特图(gantt)2

甘特图是一类条形图,由Karol Adamiechi在1896年提出, 而在1910年Henry Gantt也独立的提出了此种图形表示。通常用在对项目终端元素和总结元素的开始及完成时间进行的描述。

示例:

gantt
dateFormat YYYY-MM-DD section S1
T1: 2014-01-01, 9d section S2
T2: 2014-01-11, 9d section S3
T3: 2014-01-02, 9d
gantt
dateFormat YYYY-MM-DD
section S1
T1: 2014-01-01, 9d
section S2
T2: 2014-01-11, 9d
section S3
T3: 2014-01-02, 9d

先来看一个大的例子:

    gantt
dateFormat YYYY-MM-DD
title Adding GANTT diagram functionality to mermaid 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 section Critical tasks
Completed task in the critical line :crit, done, 2014-01-06,24h
Implement parser and jison :crit, done, after des1, 2d
Create tests for parser :crit, active, 3d
Future task in critical line :crit, 5d
Create tests for renderer :2d
Add to mermaid :1d section Documentation
Describe gantt syntax :active, a1, after des1, 3d
Add gantt diagram to demo page :after a1 , 20h
Add another diagram to demo page :doc1, after a1 , 48h section Last section
Describe gantt syntax :after doc1, 3d
Add gantt diagram to demo page : 20h
Add another diagram to demo page : 48h

获得的图渲染后如下:

header 1 header 2
title 标题
dateFormat 日期格式
section 模块
Completed 已经完成
Active 当前正在进行
Future 后续待处理
crit 关键阶段
日期缺失 默认从上一项完成后

关于日期的格式可以参考:

- string-format

- Time-Formatting

Demo

graph TB
sq[Square shape] --> ci((Circle shape)) subgraph A subgraph
di{Diamond with line break} -.-> ro(Rounded)
di==>ro2(Rounded square shape)
end e --> od3>Really long text with linebreak<br>in an Odd shape] cyr[Cyrillic]-->cyr2((Circle shape Начало)); classDef green fill:#9f6,stroke:#333,stroke-width:2px;
classDef orange fill:#f96,stroke:#333,stroke-width:4px;
class sq,e green
class di orange

reference

mermaid docs


本文原创首发于公众号:老王和他的IT界朋友们

微信扫描关注微信号:(原创投稿有惊喜!!!)


  1. 序列图的样式的定制需要在可以渲染CSS的地方才可使用,具体可以查阅参考。
  2. 甘特图的样式的定制需要在可以渲染CSS的地方才可使用,具体可以查阅参考。

markdown绘图插件----mermaid简介的更多相关文章

  1. 异常强大的Markdown编辑插件-Markdown Preview Enhanced

    最近使用Markdown写作,了解到以下这些Markdown写作工具 MaHua 在线markdown编辑器 百度搜索Markdown时,它排在非常靠前的位置 马克飞象- 专为印象笔记打造的Markd ...

  2. jQuery Form 表单提交插件----Form 简介,官方文档,官方下载地址

     一.jQuery Form简介 jQuery Form插件是一个优秀的Ajax表单插件,可以非常容易地.无侵入地升级HTML表单以支持Ajax.jQuery Form有两个核心方法 -- ajaxF ...

  3. jqPlot,一个 jQuery这个 JavaScript 框架的绘图插件

    因为项目中需要做报表的功能,于是学习了如何使用jqplot这个绘图插件 结合ajax技术,动态交互后台数据 前前后后花了三四天的时间. 感觉它会出来的想说还可以. 我的后台模板是bootstrap,在 ...

  4. Python3 matplotlib的绘图函数subplot()简介

    Python3 matplotlib的绘图函数subplot()简介 一.简介 matplotlib下, 一个 Figure 对象可以包含多个子图(Axes), 可以使用 subplot() 快速绘制 ...

  5. wPaint在线绘图插件

    wPaint在线绘图插件 一.总结 一句话总结: 1.搜画图插件的时候关键词应该搜什么? jquery画图插件 js画图插件 jquery绘图插件 这些 二.在线绘图插件--wPaint 的实际应用 ...

  6. Markdown教程<2> mermaid图形绘制(1)

    Markdown教程<2> mermaid图形绘制(1) 博客园中的markdown编辑器同时支持mermaid图表引擎与tex公式引擎,可以使用mermaid直接画出流程图,时序图,甘特 ...

  7. ThreeJS系列2_effect插件集简介( 3d, vr等 )

    ThreeJS系列2_effect插件集简介( 3d, vr等 ) ThreeJS 官方案例中有一些 js库 可以替代 render 将场景中的物质变换为其他效果的物质 目录 ThreeJS系列2_e ...

  8. Markdown中使用mermaid画流程图

    Markown语法简单,用来写文档是个不错的选择. 但是Markdown 语法并不直接支持画图,当然方法还是有的. 本人用的Markdown编辑器为vscode,在里面直接安装merdaid插件即可使 ...

  9. markdown 绘图利器之granphviz

    目录 概述 graphviz 脚本语法结构 图 方向,尺寸,间距 节点 shape 属性 多边形 record-based 的形状 用户定制 label 属性 基本用法 HTML用法 style 属性 ...

随机推荐

  1. pwd

    显示当前在文件系统里的确切位置 注: 系统根目录是/ 用户主目录是/home/用户名 对于root用户,用户主目录为/root

  2. [AtCoder arc090F]Number of Digits

    Description 题库链接 记 \(d\) 在十进制下的位数为 \(f(d)\) .给出询问 \(S\) ,求有多少对 \((l,r)\) 使得 \[\sum_{i=l}^r f(i)=S\] ...

  3. [SDOI2010]古代猪文

    题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边海的那边的某片 ...

  4. 为了异常安全(swap,share_ptr)——Effecive C++

    互斥锁: 假设我们要在多线程中实现背景图片的控制: class PrettyMenu{ public: -- void changeBackground(std::istream& imgSr ...

  5. 51 nod 1406 与查询

    1406 与查询 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   有n个整数.输出他之中和x相与之后结果为x的有多少个.x从0 ...

  6. 【20170920校内模拟赛】小Z爱学习

    所有题目开启-O2优化,开大栈空间,评测机效率为4亿左右. T1 小 Z 学数学(math) Description ​ 要说小 Z 最不擅长的学科,那一定就是数学了.这不,他最近正在学习加法运算.老 ...

  7. 【USACO08NOV】奶牛混合起来Mixed Up Cows

    题目描述 约翰有 N 头奶牛,第 i 头奶牛的编号是 S i ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...

  8. 【USACO】干草金字塔

    题目描述 贝西要用干草包堆出一座金字塔.干草包会从传送带上陆续运来,依次出现 N 包,每包干草可 以看做是一个二维平面上的一个长方形,第 i 包干草的宽度是 W i ,长度统一为 1. 金字塔的修建有 ...

  9. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  10. 新版Eclipse打开jsp、js等为文本编辑,没有JSP Editor插件问题

    刚从官网下载安装的Eclipse Java Oxygen.2但是打开的jsp文件尽然默认文本编辑器打开,就js文件也是一样,纳闷! 网上搜索一番,原来缺少web开发相关工具, 下面给插件安装方法: 1 ...