Introduction to mermaid.js — A Simple Yet Powerful Diagramming and Charting Library

Gaurav Garg
4 min readSep 4, 2023

Mermaid.js is an open-source JavaScript library that allows you to generate a variety of diagrams and charts through simple text-based syntax. In this comprehensive guide, we will walk through the key features of mermaid.js and provide examples for creating flowcharts, sequence diagrams, Gantt charts, and more.

Introduction to mermaid.js — A Simple Yet Powerful Diagramming and Charting Library
Introduction to mermaid.js — A Simple Yet Powerful Diagramming and Charting Library

Overview

Mermaid was created in 2015 by Knut Sveidqvist to simplify the process of generating diagrams and charts for documentation purposes. Some key advantages of using mermaid.js include:

  • Text-based syntax — No need for clunky diagramming software. Simply write mermaid syntax in a code editor.
  • Supports many diagram types — Flowcharts, sequence diagrams, class diagrams, state diagrams, pie charts, and more.
  • Highly customizable — Tweak colors, shapes, directions, styling and more.
  • Integrates with many apps — Works with common markup languages like Markdown, various code editors, wikis, and more.
  • Open source — mermaid is freely available under the MIT license.

Getting Started

There are a few simple ways to get up and running with mermaid:

That’s it! Now let’s look at how to create some diagrams.

Flowcharts

Flowcharts are a great way to visualize processes and workflow. Here is the mermaid syntax for a simple flowchart:

graph TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]

Some key points about the flowchart syntax:

  • graph TD - Indicates a top-down flowchart. Use TB for bottom-top.
  • Rectangles for events — []
  • Diamonds for conditionals — {}
  • Arrow --> for lines

There are many customization options for flowcharts like changing direction, styling nodes, and more.

Sequence Diagrams

Sequence diagrams are useful for visualizing interactions between objects over time. Here is a simple sequence diagram example:

sequenceDiagram
User->>Computer: Enter username & password
Computer->>API: Send login request
API-->>Computer: Return authentication token
Computer->>User: Display welcome message

The syntax is straightforward:

  • Actors on left and right — User, Computer, API
  • Messages between actors — -> and -->>

Again, many customizations are possible like styling the actors and messages.

Gantt Charts

For project planning, mermaid can generate Gantt charts showing tasks, timelines and dependencies.

gantt
title Example Gantt Diagram
section Section
Task 1 :a1, 2022-01-01, 30d
Task 2 :after a1, 20d
section Another
Task 3 :2022-01-12, 12d
Task 4 : 24d

Some tips for Gantt syntax:

  • title - Chart title
  • section - Groups tasks together
  • Task - Task name, id, start date, duration
  • after - Define dependencies

Many more chart types like class diagrams, pie charts, Git graphs and ER diagrams are possible. Consult the mermaid documentation for full details.

Customizing and Theming

While the default mermaid themes are quite nice, you can customize just about everything:

  • Change colors — #hexcodes
  • Modify shapes and style — css classes
  • Apply themes — Premade or custom

This lets you tweak a diagram’s look and feel exactly as needed for your use case.

Integrations and Usage

A key benefit of mermaid is its integration with popular apps and frameworks:

  • Embed in Markdown files for Github/Gitlab wikis
  • Integrate with static site generators like Jekyll
  • Include in documentation sites powered by Sphinx, MkDocs and more
  • Embed in Confluence wiki pages
  • Render within code editors like VS Code

There are also wrappers for React, Vue, and Angular for embedding mermaid diagrams in web projects.

Advanced Features and Integrations

Mermaid.js isn’t just limited to basic diagrams. The library offers a range of advanced features and integrations, including:

  • Mermaid with OpenAI: Dive into the world of AI with tutorials like Mind Mapping with AI: An Accessible Approach for Neurodiverse Learners.
  • Mermaid in Open Source Docs: Improve your documentation with Mermaid.js diagrams. Check out the K8s.io Diagram Guide for inspiration.
  • Jupyter Integration with Mermaid-js: For Python enthusiasts, Mermaid.js offers integration with Jupyter notebooks. This allows you to display graphs directly in your Jupyter environment.

Conclusion

Mermaid.js is a versatile and powerful tool for anyone looking to visualize data in a user-friendly manner. With its intuitive syntax and wide range of features, it’s no wonder that Mermaid.js has become a favorite among developers and data enthusiasts alike.

We’ve only scratched the surface of what’s possible with mermaid here. But hopefully this gives you a taste of how easy it is to generate elegant diagrams and charts using intuitive text-based syntax. The extensive customization options combined with wide app integration make mermaid.js a fantastic choice for simplifying diagramming.

To learn more, be sure to check out the official mermaid documentation which covers all diagram types, features, usage, and examples in-depth. Happy diagramming!

If you’re ready to dive deeper, explore new perspectives, and join a community of passionate learners, I invite you to connect with me across various social media platforms.

MediumLinkedInTwitterFacebookInstagramWebsite

Did you relish this piece? If so, make that “Clap” icon dance to your clicks as if it’s the last day on Earth! Remember, each reader can tap into the applause up to 50 times!

--

--

Gaurav Garg

Entrepreneur, Thinker, Designer, Runner, SEO, Content Creator, writes on various Topics, Building something awesome ;)