Wednesday, September 3, 2025
HomeLanguagesJavascriptD3.js arc.centroid() Function

D3.js arc.centroid() Function

The arc.centroid() function is used to compute the midpoint of the centerline of the arc. The midpoint that is generated is calculated using (startAngle+endAngle)/2 and (innerRadius+outerRadius)/2.

Syntax:

arc.centroid(arguments);

Parameters: This function accepts a single parameter as mentioned above and described below.

  • arguments: These are the arbitrary arguments.

Return Values: This function return an array containing the points.

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>
    <script
  
        // Creating an arc
        var arc = d3.arc()
            .innerRadius(40)
            .outerRadius(45)
            .startAngle(10)
            .endAngle(8);
  
        // arc.centroid function 
        console.log(arc.centroid());
    </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>
    <script
  
        // Creating an arc
        var arc = d3.arc()
            .innerRadius(40)
            .outerRadius(45)
            .startAngle(0)
            .endAngle(8);
        // arc.centroid function 
        console.log("Change in start and end angle");
        console.log("will change the centroid");
        console.log(arc.centroid());
    </script
</body>
   
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS