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


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


d3.area.x() 方法用于将 x 访问器设置为传递给它的参数,该参数可以是数字或返回数字的函数。

用法:

d3.area.x(x_point)

参数:该函数接受如上所述和以下描述的单个参数。

  • x_point:此参数是一个数字,表示一个点或一个返回数字的函数。

返回值:此方法不返回任何值。

范例1:




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js">
    </script>
</head>
  
<body>
    <h1 style="text-align:center; color:green;">
        GeeksforGeeks
    </h1>
  
    <h3 style="text-align:center;">
        D3.js | d3.area.x() Method
    </h3>
  
    <center>
        <svg id="gfg" width="200" height="200">
        </svg>
    </center>
  
    <script>
        var data = [
          {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 xScale =
 d3.scaleLinear().domain([0, 8]).range([25, 200]);
        var yScale = 
d3.scaleLinear().domain([0, 20]).range([200, 25]);
  
        var Gen = d3.area()
          // Using area.x() method
          .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>

输出:

范例2:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js">
    </script>
</head>
<body>
    <h1 style="text-align:center; color:green;">
        GeeksforGeeks
    </h1>
  
    <h3 style="text-align:center;">
        D3.js | d3.area.x() Method
    </h3>
  
    <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}];
  
  
        var Gen = d3.area()
           // Using area.x() method
          .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>

输出:




相关用法


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