The Wayback Machine - https://web.archive.org/web/20240810035655/https://www.geeksforgeeks.org/d3-js-pie-function/
Open In App

D3.js pie() Function

Last Updated : 01 Sep, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The d3.pie() is used to construct a pie generator that has its default settings. This pie generator takes an array of data and then returns an array of objects that contains details about each arc angle.

Syntax:

d3.pie();

Parameters: This function does not accept any parameters.

Return Values: This function returns a pie generator.

Below given are a few examples of the function given above.

Example 1:

HTML




<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport"
          content="width=device-width, 
                  initial-scale=1.0"/> 
      
    <!--Fetching from CDN of D3.js -->
    <script src= 
        "https://d3js.org/d3.v6.min.js"> 
    </script>
</head>
  
<body> 
    <div style="width:300px; height:300px;">
        <center>
            <h1 style="color:green">
                GeeksforGeeks
            </h1> 
  
            <h2>
                d3.pie()
            </h2> 
        </center>
  
        <svg width="300" height="300">
        </svg>
  
    </div>
    <script> 
        var data = [1.1,2.2,4.46,2.12,1.36,5.002445,4.1242];
  
        // Selecting SVG using d3.select()
        var svg = d3.select("svg");
  
        let g = svg.append("g")
               .attr("transform", "translate(150,120)");
          
        // Creating Pie generator
        var pie = d3.pie();
  
        // Creating arc
        var arc = d3.arc()
                    .innerRadius(0)
                    .outerRadius(100);
  
        // Grouping different arcs
        var arcs = g.selectAll("arc")
                    .data(pie(data))
                    .enter()
                    .append("g");
  
        // Appending path 
        arcs.append("path")
            .attr("fill", (data, i)=>{
                let value=data.data;
                return d3.schemeSet3[i];
            })
            .attr("d", arc);
    </script> 
</body> 
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport"
          content="width=device-width, 
                  initial-scale=1.0"/> 
  
    <!--Fetching from CDN of D3.js -->
    <script src= 
        "https://d3js.org/d3.v6.min.js"> 
    </script>
</head>
  
<body> 
    <div style="width:300px; height:300px;">
        <center>
            <h1 style="color:green">
                GeeksforGeeks
            </h1> 
  
            <h2>
                d3.pie()
            </h2> 
  
        </center>
  
        <svg width="300" height="300">
        </svg>
  
    </div>
      
    <script> 
        var data = [1,2,4.4,2,1,5,4];
  
        // Selecting SVG using d3.select()
        var svg = d3.select("svg");
          
        // Creating Pie generator
        var pie = d3.pie();
  
        // Creating arc
        var arc = d3.arc()
                    .innerRadius(0)
                    .outerRadius(100);
  
        let g = svg.append("g")
                   .attr("transform", "translate(150,120)");
  
        // Grouping different arcs
        var arcs = g.selectAll("arc")
                    .data(pie(data))
                    .enter()
                    .append("g");
  
        // Appending path 
        arcs.append("path")
            .attr("fill", (data, i)=>{
                let value=data.data;
                return d3.schemeSet3[i+1];
            })
            .attr("d", arc);
          
        // Adding data to each arc
        arcs.append("text")
            .attr("transform",(d)=>{ 
                    return "translate("+ 
                    arc.centroid(d) + ")"; 
            })
            .text(function(d){
               return d.data; 
               });
    </script> 
</body>
  
</html>


Output:



Previous Article
Next Article

Similar Reads

D3.js pie.padAngle() Function
The pie.padAngle() function in D3.js is used to set the padding angle between consecutive arcs. When an angle is specified, it sets the pad angle to the given angle or function and returns a pie generator. When the angle is not specified, it returns the current pad angle accessor, which defaults to no padding. Syntax: pie.padAngle( angle ) Paramete
3 min read
D3.js pie.startAngle() Function
The pie.startAngle() function in D3.js is used to set the start angle of the pie. When an angle is specified, it sets the start angle to the given angle or function and returns a pie generator. When the angle is not specified, it returns the current start angle accessor, which defaults to no start angle. Syntax: pie.startAngle( angle ) Parameters:
3 min read
D3.js pie.endAngle() Function
The pie.endAngle() function in D3.js is used to set the end angle of the pie. When an angle is specified, it sets the end angle to the given angle or function and returns a pie generator. When the angle is not specified, it returns the current end angle accessor, which defaults to no end angle. Syntax: pie.endAngle( angle ) Parameters: This functio
3 min read
D3.js pie.value() Function
The pie.value() function is used to set the value property of the data returned by the pie generator function. If the value is specified then it sets the value to the given dynamic or static function or the number. Syntax: pie.value([value]); Parameters: This function accepts a single parameter as mentioned above and described below. value: This pa
3 min read
D3.js pie.sortValues() Function
The pie.sortValues() function in D3,js is used to set the value comparator to the given function. It returns the current value comparator when the comparator function is not specified which defaults to the descending order. The difference between a value and data comparator is that the two arguments, a and b are values which are derived from the da
4 min read
D3.js pie.sort() Function
The pie.sort() function in D3.js is used to sort the data objects based on different properties of data. The comparator function can be used to define the basis on which the sorting would be done. Syntax: pie.sort( compare ) Parameters: This function accepts a single parameter as mentioned above and described below: compare: This takes a comparison
4 min read
How to create pie chart in react using material UI and DevExpress ?
We'll explore how to create dynamic and visually appealing pie charts in a React application by leveraging two powerful libraries: Material-UI and DevExpress. DevExpress: DevExpress is a package for controlling and building the user interface of the Window, Mobile, and other applications. Pie Charts: A pie chart is a circular statistical graphic, w
2 min read
How to draw a pie chart using react bootstrap ?
A Pie Chart, a circular statistical plot, visually represents a single series of data where each slice's area corresponds to the percentage it represents in the overall data, providing an intuitive visualization of proportional relationships. Prerequisites:NodeJS or NPMReact JSBootstrapApproach to create Pie Chart:This React component uses the "rea
2 min read
How to Show Labels on Pie Chart in ChartJS ?
Pie charts are an attractive way to represent data in an easy-to-understand manner. In the pie chart, the labels are very useful because, with the help of the labels, we can understand what type of data is represented in the chart. Below are the different approaches to show labels on a pie chart: Table of Content Using pluginsUsing Looltip Label Us
5 min read
How to Display Data with a Doughnut & Pie Graph in React ?
Doughnut and Pie Charts are used to visually represent data distribution in a circular format, where doughnut charts have a central hole, while pie charts do not. They are commonly used in data visualization to showcase proportions and percentages. Prerequisites:React JSNPM &amp; Node.jsRechartsSteps to Create React Application And Installing Modul
3 min read
three90RightbarBannerImg