Thursday, July 4, 2024
HomeLanguagesJavascriptD3.js area.curve() Method

D3.js area.curve() Method

The d3.area.curve() method is used to give a curve to the area. D3.js provides several curve factories that can be used to give different curves.

Syntax:

d3.area.curve(curve_factory)

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

  • curve_factory: This parameter is the type of curve to be given to the area.

Return Value: This method has no return value.

Example 1: 

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
    </script>
</head>
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <h3 style="text-align: center;">
        D3.js | d3.area.curve() Method
    </h3>
  
    <center>
        <svg id="gfg" width="250" height="200"></svg>
    </center>
  
    <script>
        var points = [
          {xpoint: 25,  ypoint: 150},
          {xpoint: 75,  ypoint: 50},
          {xpoint: 100, ypoint: 150},
          {xpoint: 100, ypoint: 50},
          {xpoint: 200, ypoint: 150}];
  
  
        var Gen = d3.area()
          .x((p) => p.xpoint)
          .y0((p) => p.ypoint/2)
          .y1((p) => p.ypoint)
          
          // Using curveBasis
          .curve(d3.curveBasis);
  
        d3.select("#gfg")
          .append("path")
          .attr("d", Gen(points))
          .attr("fill", "green")
          .attr("stroke", "black");
  
    </script>
</body>
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
    </script>
</head>
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <h3 style="text-align: center;">
        D3.js | d3.area.curve() Method
    </h3>
  
    <center>
        <svg id="gfg" width="250" height="200"></svg>
    </center>
  
    <script>
        var points = [
          {xpoint: 25,  ypoint: 150},
          {xpoint: 75,  ypoint: 50},
          {xpoint: 100, ypoint: 150},
          {xpoint: 100, ypoint: 50},
          {xpoint: 200, ypoint: 150}];
  
  
        var Gen = d3.area()
          .x((p) => p.xpoint)
          .y0((p) => p.ypoint/2)
          .y1((p) => p.ypoint)
          
          // Using curveCardinal
          .curve(d3.curveCardinal);
  
        d3.select("#gfg")
          .append("path")
          .attr("d", Gen(points))
          .attr("fill", "green")
          .attr("stroke", "black");
  
    </script>
</body>
  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments