D3.js arc.cornerRadius() Function

The arc.cornerRadius() function is used to set the corner radius for rounded corners. If the radius is not specified, returns the current corner radius accessor, which defaults to:

function cornerRadius() {
    return 0;
}

Syntax:

arc.cornerRadius([radius]);

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

  • radius: This parameter takes a number that decides the radius of the corners.

Return Values: This function returns the arc generator.

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">
                zambiatek
            </h1
  
            <h2>
                arc.cornerRadius()
            </h2
        </center>
  
        <svg width="300" height="300">
        </svg>
    </div>
  
    <script
        var svg = d3.select("svg")
            .append("g")
            .attr("transform", "translate(150,200)");
  
        // An arc generator created
        var arc = d3.arc()
            .outerRadius(-5)
            .innerRadius(150)
            .startAngle(0)
            // Use of cornerRadius Function
            .cornerRadius(6666)
            .endAngle(1);
  
        svg.append("path")
            .attr("class", "arc")
            .attr("d", arc);
  
        let p = document.querySelector(".arc");
        p.style.fill="green";
    </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">
                zambiatek
            </h1
  
            <h2>
                arc.cornerRadius()
            </h2
  
        </center>
  
        <svg width="300" height="300">
        </svg>
    </div>
  
    <script
        var svg = d3.select("svg")
            .append("g")
            .attr("transform", "translate(120,200)");
  
        // An arc generated
        var arc = d3.arc()
            .outerRadius(140)
            .innerRadius(200)
            .startAngle(0)
            // Use of cornerRadius Function
            .cornerRadius(20)
            .endAngle(1);
  
        svg.append("path")
            .attr("class", "arc")
            .attr("d", arc);
  
        let p = document.querySelector(".arc");
        p.style.fill="green";
    </script
</body
  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button