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


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


D3.js中的zoom.extent()函數用於將視口範圍設置為指定的點數組[左上角,右下角]。

用法:

zoom.extent([extent])

參數:該函數接受如上所述和以下描述的單個參數

  • extent:可以將該參數定義為指定為返回此類數組的函數。

返回值:此函數返回縮放行為。

以下示例程序旨在說明D3.js中的zoom.extent()函數



範例1:

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8"> 
  
    <script src="https://d3js.org/d3.v4.min.js">  
   </script> 
</head>  
    
<body>  
    <center> 
        <h1 style="color:green;">  
            Geeksforgeeks  
        </h1>  
      
        <h3>D3.js | zoom.extent() Function</h3> 
          
        <div id="GFG"></div> 
          
        <script> 
            var svg = d3.select("#GFG") 
              .append("svg") 
                .attr("width", 300) 
                .attr("height", 200) 
                .call(d3.zoom() 
                .extent([[0, 0], [200, 200]]) 
                .on("zoom", function () { 
                   svg.attr( 
             "transform", d3.event.transform) 
                })) 
              .append("g") 
              
            svg 
              .append("circle") 
                .attr("cx", 150) 
                .attr("cy", 100) 
                .attr("r", 40) 
                .style("fill", "green") 
              
        </script>  
    </center> 
</body>  
  
</html> 

輸出:

範例2:

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8"> 
       
    <script src="https://d3js.org/d3.v4.min.js">  
    </script> 
       
    <style> 
        svg text {   
            fill:green;   
            font:20px sans-serif;   
            text-anchor:center;   
        }   
           
        rect { 
          pointer-events:all; 
        } 
    </style> 
      
</head>  
     
<body>  
    <center> 
        <h1 style="color:green;">  
            Geeksforgeeks  
        </h1>  
       
        <h3>D3.js | zoom.extent() Function</h3> 
           
        <svg></svg> 
           
        <script> 
            var width = 400; 
            var height = 200; 
               
            var svg = d3.select("svg") 
              .attr("width", width) 
              .attr("height", height); 
                 
            // The scale used to display the axis. 
            var scale = d3.scaleLinear() 
              .range([10, width-20]) 
              .domain([0, 100]); 
                
            var shadowScale = scale.copy(); 
               
            var axis = d3.axisBottom() 
              .scale(scale); 
                 
            var g = svg.append("g") 
              .attr("transform", "translate(0, 50)") 
              .call(axis); 
                 
            // Standard zoom behavior:
            var zoom = d3.zoom() 
              .extent([[0, 0], [width, height]]) 
              .on("zoom", zoomed); 
                
            // Call the Zoom. 
            var rect = svg.append("rect") 
              .attr("width", width) 
              .attr("height", height) 
              .attr("fill", "none") 
              .call(zoom); 
                
            function zoomed() { 
              var t = d3.event.transform; 
              scale.domain(t.rescaleX(shadowScale).domain()); 
              g.call(axis); 
            } 
        </script>  
    </center> 
</body>  
   
</html>

輸出:




相關用法


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