DEV Community

Johnny Turco
Johnny Turco

Posted on

1

How to the Use React Minimal Pie Chart Library

react-minimal-pie-chart is a tiny, lightweight pie charting React library. After reviewing the landscape, react-minimal-pie-chart is the smallest and simplest way to create pie (or donut) charts for your React app. The following is a quick introduction to get you up and running.

First, install the library:



npm install react-minimal-pie-chart


Enter fullscreen mode Exit fullscreen mode

And import it into your component:



import { PieChart } from 'react-minimal-pie-chart';


Enter fullscreen mode Exit fullscreen mode

Now you can use the <PieChart /> component.

There are quite a few options you can use to customize your pie charts. The most basic and important property is the data property, which takes an array of objects. A basic chart will have the following keys: title, value, and color. For example, your data attribute might look like this:



data={[
    {
      title: "Test 1",
      value: 50,
      color: "#000000"
    },
    {
      title: "Test 2",
      value: 50,
      color: "#ffffff"
    }
]}


Enter fullscreen mode Exit fullscreen mode

To get your labels to show up, you'll need to add the following property: label={({ dataEntry }) => dataEntry.title}.

And to style your labels, you set the labelStyle property to an object of SVG CSS properties and values. For example:



labelStyle={{
    fill: "white",
    fontSize: "5px",
    fontFamily: "Helvetica Neue,sans-serif",
    textShadow: "1px 1px 5px #000"
    }}


Enter fullscreen mode Exit fullscreen mode

You can also position your labels with labelPosition, which takes a number 0-100 (for a percent from center).

Here is a complete example of a basic pie chart component:



<PieChart
    label={({ dataEntry }) => dataEntry.title}
    labelStyle={
        {
            fill: "white",
            fontSize: "5px",
            fontFamily: "Helvetica Neue,sans-serif",
            textShadow: "1px 1px 5px #000"
        }
    }
    labelPosition={75}
    data={
        [
            {
              title: "Test 1",
              value: 50,
              color: "#000000"
            },
            {
              title: "Test 2",
              value: 50,
              color: "#ffffff"
            }
        ]
    }
/>


Enter fullscreen mode Exit fullscreen mode

Which will give you a pie chart that looks like this:
Image of pie chart example

If you want to add more, you can animate your pie chart on component render and even add mouse interactions. Check out the documentation here.

Image of Stellar post

๐Ÿš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging โ†’

๐Ÿ‘‹ Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someoneโ€™s dayโ€”drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay