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

D3.js area.context() Method

The d3.area.context() method lets you render the area in a canvas element’s context. The area will be rendered in the current context when the generator is invoked. We can set the context of our line on our own using this method like color, stroke, fill, etc. The default value is null.

Syntax:

d3.area.context(_context);

Parameters: 

  • _context: Context set by the user.

Return Value: This method returns the current context.

Example 1:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <h3 style="text-align: center;">
        D3.js | area.context() Method
    </h3>
  
    <center>
        <canvas id="gfg" width="200" height="200">
        </canvas>
    </center>
  
    <script>
        var data = [
          {xpoint: 25,  ypoint: 150},
          {xpoint: 75,  ypoint: 50},
          {xpoint: 100, ypoint: 150},
          {xpoint: 100, ypoint: 50},
          {xpoint: 200, ypoint: 150}];
  
        var context = d3.select("#gfg")
               .node().getContext("2d");
  
  
        var Gen = d3.area()
              .x((p) => p.xpoint)
              .y0((p) => p.ypoint/2)
              .y1((p) => p.ypoint)
              .context(context);
  
        context.translate(10,50);              
        Gen(data);
        context.strokeStyle = "black";
        context.fillStyle = "green";
        context.fill();
        context.stroke();
    </script>
</body>
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        neveropen
    </h1>
  
    <h3 style="text-align: center;">
        D3.js | area.context() Method
    </h3>
  
    <center>
        <canvas id="gfg" width="200" height="200">
        </canvas>
    </center>
  
    <script>
        var points = [
          {x: 50, y: 10},
          {x: 150, y: 30},
          {x: 200, y: 150},
          {x: 250, y: 10},
          {x: 300, y: 150},
          {x: 350, y: 50},
          {x: 400, y: 190}];
  
        var context = d3.select("#gfg")
               .node().getContext("2d");
  
  
        var Gen = d3.area()
              .x((p) => p.x)
              .y0((p) => p.y/2)
              .y1((p) => p.y)
              .context(context);
  
        context.translate(10,10);              
        Gen(points);
        context.strokeStyle = "black";
        context.fillStyle = "green";
        context.fill();
        context.stroke();
  
    </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