Tuesday, December 23, 2025
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!
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11959 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS