Skip to content
Related Articles

Related Articles

Improve Article

D3.js area() Method

  • Last Updated : 31 Aug, 2020
Geek Week

The d3.area() method in D3.js is used to return an area generator with default settings that can be further used to create areas.

Syntax:

d3.area()

Parameters: This method does not accept parameters.

Return Value: This method returns no value.

Below examples illustrate the d3.area() method in D3.js:



Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center;
             color: green;">
        GeeksforGeeks
    </h1>
  
    <center>
        <svg id="gfg" width="200" height="200">
        </svg>
    </center>
      
    <script>
        var data = [
            { x: 0, y: 10 },
            { x: 10, y: 30 },
            { x: 20, y: 150 },
            { x: 50, y: 10 },
            { x: 60, y: 150 },
            { x: 70, y: 50 },
            { x: 80, y: 190 }];
  
        data.sort((a, b) => a.y - b.y);
  
        var xScale = d3.scaleLinear()
            .domain([0, 8])
            .range([25, 200]);
        var yScale = d3.scaleLinear()
            .domain([0, 20])
            .range([200, 25]);
  
        // Using area() function to
        // generate area
        var Gen = d3.area()
            .x((p) => p.x)
            .y0((p) => 0)
            .y1((p) => p.y);
  
        d3.select("#gfg")
            .append("path")
            .attr("d", Gen(data))
            .attr("fill", "green")
            .attr("stroke", "black");
    </script>
</body>
  
</html>

Output:

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center; 
             color: green;">
        GeeksforGeeks
    </h1>
  
    <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 }];
  
        // Using area() function to generate area
        var Gen = d3.area()
            .x((p) => p.xpoint)
            .y0((p) => 0)
            .y1((p) => p.ypoint);
  
        d3.select("#gfg")
            .append("path")
            .attr("d", Gen(points))
            .attr("fill", "green")
            .attr("stroke", "black");
  
    </script>
</body>
  
</html>

Output:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :