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


d3.js line.y()用法及代碼示例


line.y() 方法設置或返回行的 y 訪問器點如果提供了 y,則它必須是數字或返回數字的函數。

用法:

d3.line.y();

參數:

  • y-point:此方法采用可以從點數組中設置的 y-point。

返回值:此方法返回線的 y 訪問點。

範例1:使用此方法設置 y-point。對於這裏的 x-points,我們使用了 line.x() 函數。



HTML


<!DOCTYPE html>
<html>
<meta charset="utf-8">
  
<head>
    <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>
  
    <center>
        <svg id="gfg" width="200" 
            height="200">
        </svg>
    </center>
      
    <script>
        var points = [
            { xpoint:25, ypoint:150 },
            { xpoint:75, ypoint:85 },
            { xpoint:100, ypoint:115 },
            { xpoint:175, ypoint:25 }];
  
        var Gen = d3.line()
            .x((p) => p.xpoint)
            // Setting the y-point
            .y((p) => p.ypoint);
  
        d3.select("#gfg")
            .append("path")
            .attr("d", Gen(points))
            .attr("fill", "none")
            .attr("stroke", "green");
    </script>
</body>
  
</html>

輸出:

範例2:獲取 y 點的函數。

HTML


<!DOCTYPE html>
<html>
<meta charset="utf-8">
  
<head>
    <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>
  
    <center>
        <svg id="gfg" width="200" height="200"></svg>
    </center>
    <script>
        var points = [
            { xpoint:25, ypoint:150 },
            { xpoint:75, ypoint:85 },
            { xpoint:100, ypoint:115 },
            { xpoint:175, ypoint:25 }];
  
        var Gen = d3.line()
            .x((p) => p.xpoint)
            .y((p) => p.ypoint);
  
        d3.select("#gfg")
            .append("path")
            .attr("d", Gen(points))
            .attr("fill", "none")
            .attr("stroke", "green");
  
        console.log(Gen.y());
        console.log(Gen.y)
    </script>
</body>
  
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自taran910大神的英文原創作品 D3.js line.y() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。