Skip to content
Related Articles

Related Articles

D3.js area.context() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 02 Sep, 2020

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=
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        GeeksforGeeks
    </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=
    </script>
</head>
  
<body>
    <h1 style="text-align: center; color: green;">
        GeeksforGeeks
    </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:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!