Skip to content
Related Articles

Related Articles

Improve Article

D3.js transform.toString() Function

  • Last Updated : 02 Sep, 2020

The transform.toString() Function in D3.js is used to get a string representing the SVG transform corresponding to this transform.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

transform.toString()

Parameters: This function does not accept any parameter.

Return Value: This function returns a string value that can be used to set zoom behavior.



Below programs illustrate the transform.toString() function in D3.js.

Example 1:

HTML




<!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 | transform.toString() Function</h3>
            
        <svg width="400" height="250"></svg>
            
        <script>
            var svg = d3.select("svg"),
                width = +svg.attr("width"),
                height = +svg.attr("height");
                   
            var radius = 30;    
            var circle = {x: 200, y: height /2 } ; 
                   
            var circle = svg.append("circle")
                .attr("cx", circle.x)
                .attr("cy", circle.y)
                .attr("r", radius)
                .attr("fill", "green");
                   
            // Defining zoom behaviour 
            var zoom_handler = d3.zoom()
                .on("zoom", zoom_actions);
               
            zoom_handler(circle);
               
            function zoom_actions(){
                var transform = d3.event.transform; 
  
                // Setting attribute using this method
                this.setAttribute("transform", 
                           transform.toString());
            }
   
        </script
    </center>
</body
    
</html>

Output:

Example 2:

HTML




<!DOCTYPE html> 
<html
  
<head
    <meta charset="utf-8">
        
    <script src=
        "https://d3js.org/d3.v4.min.js"
    </script>
        
    <style>
        circle {
          opacity: 0.7;
          fill: #9a05a3;
        }
    </style>
</head
      
<body
    <center>
        <h1 style="color: green;"
            Geeksforgeeks 
        </h1
        
        <h3>D3.js | transform.toString() Function</h3>
            
        <svg></svg>
            
        <script>
            var transform = d3.zoomIdentity
                .translate(100, 0).scale(1);
  
            var zoom = d3.zoom().on("zoom", handleZoom);
                  
            var svg = d3.select("svg")
              .attr('width', 300)
              .attr('height', 250)
              .style("background", "#31c460")
              .call(zoom)                      
              .call(zoom.transform, transform);
            
            var zoomable = svg
              .append("g")
              .attr("class", "zoomable")
              .attr("transform", transform);   
                  
            var circles = zoomable.append('circle')
              .attr("id", "circles")
              .attr("cx", 50)
              .attr("cy", 125)
              .attr('r', 30);
            
            function handleZoom(){
              if (zoomable) {
                // Setting attribute using this method
                zoomable.attr("transform", 
                    d3.event.transform.toString());
              }
            };
        </script
    </center>
</body
    
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :