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


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


這個d3.area()D3.js中的方法用於返回具有默認設置的區域生成器,可以將其進一步用於創建區域。

用法:

d3.area()

參數:此方法不接受參數。

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

以下示例說明了D3.js中的d3.area()方法:



範例1:

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src="https://d3js.org/d3.v4.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 data = [ 
            { x:0, y:10 }, 
            { x:10, y:30 }, 
            { x:20, y:150 }, 
            { x:50, y:10 }, 
            { x:60, y:150 }, 
            { x:70, y:50 }, 
            { x:80, y:190 }]; 
  
        data.sort((a, b) => a.y - b.y); 
  
        var xScale = d3.scaleLinear() 
            .domain([0, 8]) 
            .range([25, 200]); 
        var yScale = d3.scaleLinear() 
            .domain([0, 20]) 
            .range([200, 25]); 
  
        // Using area() function to 
        // generate area 
        var Gen = d3.area() 
            .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:

HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src="https://d3js.org/d3.v4.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1 style="text-align:center;  
             color:green;"> 
        GeeksforGeeks 
    </h1> 
  
    <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 }]; 
  
        // Using area() function to generate area 
        var Gen = d3.area() 
            .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() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。