跳转到正文

时间线图 ​

“时间线是一种图表,用于展示事件、日期或时间段的先后顺序。它通常以图形方式呈现,以表明时间的流逝,并且通常按时间顺序组织。基本时间线按时间顺序列出事件,通常以日期作为标记。时间线也可用于展示事件之间的关系,例如一个人一生中各种事件之间的关系” (Wikipedia)。

时间线示例 ​

代码:
mermaid
timeline
    title History of Social Media Platform
    2002 : LinkedIn
    2004 : Facebook
         : Google
    2005 : YouTube
    2006 : Twitter
Ctrl + 回车键|

语法 ​

创建时间线图的语法很简单。您始终以 timeline 关键字开头,让 mermaid 知道您要创建时间线图。

之后可以选择为时间线添加标题:添加一行,以关键字 title 开头,后跟标题文本。

然后添加时间线数据:始终以时间段开头,后跟一个冒号,再写事件文本。您也可以添加第二个冒号,再写另一个事件的文本。因此,每个时间段可以有一个或多个事件。

json
{time period} : {event}

或

json
{time period} : {event} : {event}

或

json
{time period} : {event}
              : {event}
              : {event}

注意:时间段和事件都是简单文本,不限于数字。

下面来看上面示例所用的语法。

代码:
mermaid
timeline
    title History of Social Media Platform
    2002 : LinkedIn
    2004 : Facebook : Google
    2005 : YouTube
    2006 : Twitter
Ctrl + 回车键|

通过这种方式,您可以使用文本大纲生成时间线图。 时间段和事件的顺序很重要,因为绘制时间线时会依据该顺序。第一个时间段位于时间线左侧,最后一个时间段位于时间线右侧。

同样,在特定时间段内,第一个事件位于顶部,最后一个事件位于底部。

按部分/时代对时间段分组 ​

您可以按部分/时代对时间段进行分组:添加一行,以关键字 section 开头,后跟部分名称。

在定义新的部分之前,后续所有时间段都会放在该部分中。

如果未定义任何部分,所有时间段都会放在默认部分中。

下面看一个按部分对时间段进行分组的示例。

代码:
mermaid
timeline
    title Timeline of Industrial Revolution
    section 17th-20th century
        Industry 1.0 : Machinery, Water power, Steam <br>power
        Industry 2.0 : Electricity, Internal combustion engine, Mass production
        Industry 3.0 : Electronics, Computers, Automation
    section 21st century
        Industry 4.0 : Internet, Robotics, Internet of Things
        Industry 5.0 : Artificial intelligence, Big data, 3D printing
Ctrl + 回车键|

如您所见,时间段被放入各个部分,各部分按定义顺序排列。

同一部分下的所有时间段和事件采用相近的配色方案,以便于看清时间段与事件之间的关系。

长时间段或事件的文本换行 ​

默认情况下,时间段和事件的文本过长时会自动换行,以避免文本被绘制到图表之外。

您也可以使用 <br> 强制换行。

下面再看一个包含长时间段和长事件的示例。

代码:
mermaid
timeline
        title England's History Timeline
        section Stone Age
          7600 BC : Britain's oldest known house was built in Orkney, Scotland
          6000 BC : Sea levels rise and Britain becomes an island.<br> The people who live here are hunter-gatherers.
        section Bronze Age
          2300 BC : People arrive from Europe and settle in Britain. <br>They bring farming and metalworking.
                  : New styles of pottery and ways of burying the dead appear.
          2200 BC : The last major building works are completed at Stonehenge.<br> People now bury their dead in stone circles.
                  : The first metal objects are made in Britain.Some other nice things happen. it is a good time to be alive.

Ctrl + 回车键|
代码:
mermaid
timeline
        title MermaidChart 2023 Timeline
        section 2023 Q1 <br> Release Personal Tier
          Bullet 1 : sub-point 1a : sub-point 1b
               : sub-point 1c
          Bullet 2 : sub-point 2a : sub-point 2b
        section 2023 Q2 <br> Release XYZ Tier
          Bullet 3 : sub-point <br> 3a : sub-point 3b
               : sub-point 3c
          Bullet 4 : sub-point 4a : sub-point 4b
Ctrl + 回车键|

方向(v11.14.0+) ​

时间线可以通过 timeline 后面的关键字改变方向。

代码:
mermaid
timeline TD
  title MermaidChart 2023 Timeline
    section 2023 Q1 <br> Release Personal Tier
      Bullet 1 : sub-point 1a : sub-point 1b
      Bullet 2 : sub-point 2a : sub-point 2b
    section 2023 Q2 <br> Release XYZ Tier
      Bullet 3 : sub-point <br> 3a : sub-point 3b
      Bullet 4 : sub-point 4a : sub-point 4b
Ctrl + 回车键|

可选方向包括:

  • LR:从左到右(默认)
  • TD:从上到下。

时间段和事件的样式 ​

如前所述,每个部分都有配色方案,该部分下的每个时间段和事件都采用相近的配色方案。

不过,如果未定义任何部分,则有两种情况:

  1. 分别为每个时间段设置样式,即每个时间段(及其对应事件)都有自己的配色方案。这是默认行为。
代码:
mermaid
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter

Ctrl + 回车键|

注意:未定义任何部分,每个时间段及其对应事件都有自己的配色方案。

  1. 使用 disableMultiColor 选项禁用 multiColor 选项。这样所有时间段和事件都会采用相同的配色方案。

您需要通过 mermaid.initialize 函数或 directives 添加此选项。

javascript
mermaid.initialize({
        theme: 'base',
        startOnLoad: true,
        logLevel: 0,
        timeline: {
          disableMulticolor: false,
        },
        ...
        ...

下面看同一个示例,其中已禁用 multiColor 选项。

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'base'
  timeline:
    disableMulticolor: true
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter

Ctrl + 回车键|

自定义配色方案 ​

您可以使用 cScale0 到 cScale11 主题变量自定义配色方案,这些变量会更改背景颜色。Mermaid 允许您为最多 12 个部分设置不同的颜色:cScale0 决定第一个部分或时间段的值,cScale1 决定第二个部分的值,依此类推。 如果部分超过 12 个,配色方案将开始重复。

如果您还想更改某个部分的前景色,可以使用与 cScaleLabel0 到 cScaleLabel11 对应的主题变量。

注意:这些主题变量的默认值取自所选主题。如果要覆盖默认值,可以通过 initialize 调用添加自定义主题变量值。

示例:

下面覆盖 cScale0 到 cScale2 变量的默认值:

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'default'
  themeVariables:
    cScale0: '#ff0000'
    cScaleLabel0: '#ffffff'
    cScale1: '#00ff00'
    cScale2: '#0000ff'
    cScaleLabel2: '#ffffff'
---
       timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest

Ctrl + 回车键|

可以看到颜色已更改为主题变量中指定的值。

主题 ​

Mermaid 提供了多种预定义主题,您可以从中挑选合适的主题。附注:您也可以覆盖现有主题的变量,从而得到自己的自定义主题。有关详情,请参阅为图表设置主题。

以下是不同的预定义主题选项:

  • redux-color(默认)
  • redux-dark-color
  • redux
  • redux-dark
  • default
  • neutral
  • dark
  • forest
  • neo
  • neo-dark
  • base

注意:您可以通过 initialize 调用或 directives 更改主题。有关详情,请参阅 directives。 下面试用其中几个主题,看看示例图表在不同主题下的效果:

Base 主题 ​

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'base'
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest
Ctrl + 回车键|

Forest 主题 ​

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'forest'
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest
Ctrl + 回车键|

Dark 主题 ​

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'dark'
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest
Ctrl + 回车键|

default 主题 ​

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'default'
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest
Ctrl + 回车键|

Neutral 主题 ​

代码:
mermaid
---
config:
  logLevel: 'debug'
  theme: 'neutral'
---
    timeline
        title History of Social Media Platform
          2002 : LinkedIn
          2004 : Facebook : Google
          2005 : YouTube
          2006 : Twitter
          2007 : Tumblr
          2008 : Instagram
          2010 : Pinterest
Ctrl + 回车键|

集成到您的库/网站 ​

时间线已包含在 mermaid 中,并在页面首次使用时按需加载,从而保持初始下载量较小。无需额外设置:

html
<script type="module">
  import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@12/dist/mermaid.esm.min.mjs';
</script>

您也可以参考实时编辑器中的实现,了解异步加载是如何完成的。

Opens in mermaid.ai