[go: up one dir, main page]

DEV Community

ruthmoog
ruthmoog

Posted on

Code Graphs with Mermaid

I was recently working on graph algorithms, and it gave me the opportunity to explore Mermaid, which is a tool for adding graphs and charts to your markdown.

DEV.to doesn't currently have support for Mermaid, however you can use Mermaid.live to generate charts as .png or .svg to use wherever you want. GitHub does support Mermaid, so you can immediately level-up your README.md's with dependency graphs and sequence diagrams very easily!

Making a sequence diagram

documentation for sequence diagrams

Let's start with an example of a sequence diagram for a HTTP request.



sequenceDiagram
    User->>+Client: Enters a URL...
    Client->>+Server: Sends a HTTP GET request
    Server-->>-Client: Returns a HTTP response
    Client-->>-User: Senses are filled with delight


Enter fullscreen mode Exit fullscreen mode

If I write this code in my README, this will be rendered by Mermaid like this:
HTTP request sequence diagram

Displaying data in a Pie Chart

documentation for pie charts

Who doesn't want to display the makeup of bee species in the UK in the form of a pie chart?!

Here's my code:



pie title Classification of Bee Species Groups in the United Kingdom
    "Colletidae" : 2000
    "Andrenidae" : 2700
    "Halictidae" : 3500
    "Melittidae" : 160
    "Megachilidae" : 3000
    "Apidae" : 6000


Enter fullscreen mode Exit fullscreen mode

and the resulting chart:
Pie chart showing the classification of bee species groups in the United Kingdom

MVC pattern with state diagram

documentation for state diagrams



stateDiagram-v2
    View --> Model
    Model --> View
    Model --> Controller
    Controller --> Model


Enter fullscreen mode Exit fullscreen mode

MVC pattern diagram

Describe a process with a flow chat

documentation for flow charts

I cannot say how many iterations of the peer review process I have drawn in my lifetime, but it is a lot. One more won't hurt!
Heres the code:



flowchart TD
    A[Manuscript submitted] --> B(Editorial Review)
    B -.-|Revisions| A
    B --- C([Peer Review])
    C --> D{Reviewer 1}
    C --> E{Reviewer 2}
    C --> F{Reviewer 3}
    D -.-|Revisions| A
    E -.-|Revisions| A
    F -.-|Revisions| A
    D -->|OK| G(Editorial Decision)
    E -->|OK| G(Editorial Decision)
    F -->|OK| G(Editorial Decision)
    G -->|Accept| H>PUBLISH]
    G -->|Reject| I[\fa:fa-trash-alt/]


Enter fullscreen mode Exit fullscreen mode

and the output looks like this:

Flow chart of a peer-review process

Top comments (3)

Collapse
 
didof profile image
Francesco Di Donato

Hey, nice post! Do you know if dev.to supports these ones in the markdown?

Collapse
 
ruthmoog profile image
ruthmoog

Last I checked dev.to doesn't support mermaid.

I generated the images with mermaid and saved them as .png to use in the blogpost

Collapse
 
didof profile image
Francesco Di Donato

Got it! Thanks.