Wednesday, July 3, 2024
HomeLanguagesJavascriptD3.js area() Method

D3.js area() Method

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;">
        neveropen
    </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;">
        neveropen
    </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:

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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments