Home > Software Engineering > Making Pie (Charts) out of Mermaid.js

Making Pie (Charts) out of Mermaid.js

Quick and Effective Generation of Pie Charts with Mermaid.js and Markdown

by

I love the myriad of types of diagrams that Mermaid.js can generate from sequence diagrams to user journey maps to class diagrams. However, I have to admit that I was somewhat surprised to discover that in addition to these specialized charts, Mermaid.js can also generate the most basic of all charts: the pie chart.

In this short article I'll show you the basic syntax for representing a pie chart in Mermaid.js.

First of all, Mermaid.js works in a variety of settings from GitHub markdown to Polyglot Notebooks and various add-ins for tools like Jira and Wordpress. I go into the process for integrating Mermaid.js a bit more in my article on entity relationship diagrams if you are curious about the overall context.

With Mermaid, you can make a simple pie chart with the simple pie keyword, followed optionally by a title and then individual data points formatted as "Data Point": value.

Here's a simple Mermaid.js Pie Chart illustrating the leading causes of developer tears from 65 respondents:

pie 
    title Leading Causes of Developer Tears
    "JavaScript": 42
    "DNS": 8
    "Leaving a VM on": 15

That simple markdown is all you need to render a pie chart using Mermaid.js.

There are a couple of things here I want to highlight before I close this article:

  1. The values do not need to add up to 100. Mermaid.js figures out the total percentage by the total value count
  2. The values do not need to be in a particular order. Mermaid.js will organize the chart in descending order automatically

There are a few more ways of customizing pie charts in Mermaid.js so I'd encourage you to read the Mermaid.js pie chart documentation for full theming reference, but pie charts on their own are very simple and easy to add to your application.

Author

  • Matt Eland
    Microsoft MVP in AI, Author of "Refactoring with C#"

    Matt Eland is a software engineering leader and data scientist who has served as a senior engineer, software engineering manager, professional programming instructor, and has helped build enterprise-level software at a variety of organizations before distinguishing himself as a Microsoft MVP in Artificial Intelligence by using technology to accomplish ridiculous things in the name of science and teaching others. Matt makes it his job to learn new things and share them with others through articles, videos, and talks at user groups and conferences covering a wide range of topics from software architecture to programming topics to artificial intelligence and data science. Matt is a current data analytics master's student, an AI Specialist at Leading EDJE, is the author of "Refactoring with C#" and is creating a LinkedIn course and book on Computer Vision on Azure. Matt occasionally sleeps as well.

    View all posts

1 comment

Dew Drop – April 14, 2023 (#3922) – Morning Dew by Alvin Ashcraft April 14, 2023 - 6:42 am

[…] Making Pie (Charts) out of Mermaid.js (Matt Eland) […]

Reply

Leave a Reply

Related Content

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Discover more from The New Dev's Guide

Subscribe now to keep reading and get access to the full archive.

Continue reading