How to Create Pie Charts in ReactJS

share link

by Abdul Rawoof A R dot icon Updated: Jul 25, 2023

technology logo
technology logo

Solution Kit Solution Kit  

To create pie charts in a React application, you can use the react-highcharts library, which provides a React wrapper for the Highcharts library. Pie charts are a common way to present a brief composition summary or comparison. They continue to be a popular option for tiny datasets, even though they can be more challenging to read than column charts. 


Highcharts is a popular library for creating interactive charts and graphs on the web. To create pie charts in a React application, you can use the react-highcharts library, which provides a React wrapper for the Highcharts library. There are several different charts available on Highcharts. Examples include bar charts, pie charts, line charts, spline charts, area charts, and so forth. Pies in highcharts can also be hollow, known as a donut chart. Additionally, this pie features an inside chart, creating a hierarchical picture. Pie chart with clickable individual slices that display more specific information. 


Here is an example of how you can include a pie chart with ReactJS in your application: 

Fig 1: Preview of the output that you will get on running this code from your IDE.

Code

In this solution we're using React library.

import React from "react";
import { render } from "react-dom";
// Import Highcharts
import Highcharts from "highcharts/highstock";
import variablePie from "highcharts/modules/variable-pie.js";

variablePie(Highcharts);

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const options = this.highchartsOptions();
    this.chart = Highcharts.chart("pie-chart-profile", options);
  }
  highchartsOptions() {
    const options = {
      chart: {
        // renderTo: 'container',
        type: "variablepie",
        margin: [0, 0, 0, 0],
        // marginLeft: -100,
        events: {
          load: function() {
            this.renderer
              .circle(
                this.chartWidth / 2,
                this.plotHeight / 2 + this.plotTop,
                this.plotHeight / 4
              )
              .attr({
                fill: "rgba(0,0,0,0)",
                stroke: "#2ec277",
                left: -100,
                "stroke-width": 1
              })
              .add();
          }
        }
      },
      colors: ["#2ec277", "#2db799", "#b7e886", "#6d5494", "#0077b4"],

      title: {
        text: null
      },

      legend: {
        align: "right",
        verticalAlign: "top",
        layout: "vertical",
        x: 20,
        y: 100,
        itemMarginTop: 5,
        itemMarginBottom: 5,
        itemStyle: {
          font: "17pt Trebuchet MS, Verdana, sans-serif",
          color: "#333333"
        }
      },
      plotOptions: {
        series: {
          stacking: "normal",
          dataLabels: {
            enabled: false
          },
          showInLegend: true,
          size: 185
        }
      },

      series: [
        {
          minPointSize: 10,
          innerSize: "27%",
          zMin: 0,
          name: "Job Match",
          data: [
            {
              name: "Job Title Match  99%",
              y: 100,
              z: 99
            },
            {
              name: "Industry Match 98%",
              y: 100,
              z: 98
            }
          ]
        }
      ]
    };
    return options;
  }

  render() {
    return <div className="chart-toggle-display" id="pie-chart-profile" />;
  }
}

render(<App />, document.getElementById("root"));

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Install npm install --save highcharts
  5. Open the folder in IDE.
  6. Copy the code using "copy" button above and paste it in index.js file(remove the earlier code from index.js).
  7. Open the terminal from IDE.
  8. npm start to run the file.


Also you can refer this url 'DEMO' for getting the above output.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for "Pie Charts in Reactjs" in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in Visual Studio Code 1.73.1.
  2. The solution is tested on node v18.12.1 and npm v8.19.2.
  3. React version-18.2.0.
  4. Highcharts version-16.0.2.


Using this solution, we are able to view pie charts in Reactjs with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to view pie charts in Reactjs.

Dependent Libraries

reactby facebook

JavaScript doticonstar image 209050 doticonVersion:v18.2.0doticon
License: Permissive (MIT)

The library for web and native user interfaces

Support
    Quality
      Security
        License
          Reuse

            reactby facebook

            JavaScript doticon star image 209050 doticonVersion:v18.2.0doticon License: Permissive (MIT)

            The library for web and native user interfaces
            Support
              Quality
                Security
                  License
                    Reuse
                      JavaScript doticonstar image 1250 doticonVersion:v16.0.2doticon
                      License: Permissive (MIT)

                      React wrapper for Highcharts library

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                react-highchartsby kirjs

                                JavaScript doticon star image 1250 doticonVersion:v16.0.2doticon License: Permissive (MIT)

                                React wrapper for Highcharts library
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          You can also search for any dependency on kandi like 'react' and 'highcharts'.

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page.

                                          See similar Kits and Libraries