当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js area.context()用法及代码示例


d3.area.context()方法可让您在画布元素的上下文中渲染该区域。调用生成器时,将在当前上下文中渲染该区域。我们可以使用颜色,描边,填充等方法自行设置行的上下文。默认值为null。

用法:

d3.area.context(_context);

Parameters: 

  • _context:用户设置的上下文。

返回值:此方法返回当前上下文。

范例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;"> 
        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>

输出:

范例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;"> 
        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>

输出:




相关用法


注:本文由纯净天空筛选整理自taran910大神的英文原创作品 D3.js area.context() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。