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


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

D3.js是Java語言中的一個庫,該庫主要用於製作圖形和可視化HTML SVG元素上的數據。 D3代表數據驅動文檔,主要用於數據可視化。 Path.rect()用於在svg元素中製作一個矩形。

用法:

Path.rect(x, y, w, h);

參數:該函數接受上述和以下所述的四個參數:

  • X:它是矩形的x-position。
  • Y:它是矩形的y-position。
  • W:它是矩形的寬度。
  • H:它是矩形的高度

以下示例說明了D3.js中的函數

範例1:



Javascript

<!DOCTYPE html> 
<html lang="en"> 
   <head> 
      <meta charset="UTF-8"> 
      <meta name="viewport" 
            path1tent="width=device-width,  
                       initial-scale=1.0"> 
      <title>D3.js | Path.rect() function</title> 
   </head> 
   <style> 
      h1 { 
          color:green; 
      } 
      body { 
          text-align:center; 
      } 
      svg{ 
      background-color:green; 
      } 
      .path1{ 
      fill:aliceblue; 
      } 
   </style> 
   <body> 
      <div> 
         <h1>GeeksforGeeks</h1> 
         <b>D3.js | Path.rect() function</b> 
         <br> 
         <svg width="400" height="300"> 
            <text x="50" y="50" font-family="Verdana" 
                  fill="white"> 
                SVG Element 
            </text> 
            <path class="path1"> 
         </svg> 
      </div> 
      <script src = 
"https://d3js.org/d3.v4.min.js"> 
      </script> 
      <script> 
         // Creating path object 
         var path1= d3.path(); 
           
         // Creating rectangle at x:50 and y:100 
         // and height:200, width:300 
         path1.rect(50,70,300,200);  
         d3.select(".path1").attr("d",path1); 
      </script> 
   </body> 
</html>

輸出:

範例2:

Javascript

<!DOCTYPE html> 
<html lang="en"> 
   <head> 
      <meta charset="UTF-8"> 
      <meta name="viewport" 
            path1tent="width=device-width,  
                       initial-scale=1.0"> 
      <title>D3.js | Path.rect() function</title> 
   </head> 
   <style> 
      h1 { 
          color:green; 
      } 
      body { 
          text-align:center; 
      } 
      svg{ 
      background-color:green; 
      } 
      .path1{ 
      fill:aliceblue; 
      } 
   </style> 
   <body> 
      <div> 
         <h1>GeeksforGeeks</h1> 
         <b>D3.js | Path.rect() function</b> 
         <br> 
         <svg width="400" height="300"> 
            <text x="50" y="50" font-family="Verdana" 
                  fill="white"> 
                SVG Element 
            </text> 
            <path class="path1"> 
         </svg> 
      </div> 
      <script src = 
"https://d3js.org/d3.v4.min.js"> 
      </script> 
      <script> 
         // Creating path object 
         var path1= d3.path(); 
           
         // Creating rectangle at x:50 and y:100 
         // and height:200, width:300 
         path1.rect(100, 70, 200, 200);  
         d3.select(".path1").attr("d",path1); 
      </script> 
   </body> 
</html>

輸出:

相關用法


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