當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。