Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 transform scale and translate

Tags:

svg

d3.js

I tried to understand how the transform is working in D3 but I think I didn't get it.

Does the scale change the size of the SVG object ? meaning if I give a big number the size of the object will look bigger ? Does the translate move the object from one place to different place ? I tried it but it didn't work like I thought.

Could you please explain to me how it should work ?

like image 958
user1365697 Avatar asked Jun 05 '12 07:06

user1365697


People also ask

What does transform and translate do in D3?

translate() Function. The transform. translate() function in D3. js library is used to get the transformation whose translation tx1 and ty1 is equal to tx0 + tk x and ty0 + tk y, where tx0 and ty0 is the transform's translation and tk is the transform's scale.

What does D3 translate do?

Translate is an important functionality in D3. js and is used to move the SVG elements inside the webpage. It takes two values, namely x and y. The x value translates the SVG element along the x-axis, while y translates the SVG element along the y-axis.

What is D3 zoomIdentity?

zoomIdentity() function in D3. js is used to get the identity transform, where k = 1, tx = ty = 0.

What is SVG translation?

The translate(<x> [<y>]) transform function moves the object by x and y . If y is not provided, it is assumed to be 0 .


2 Answers

Scott Murray wrote a great explanation of this[1]. For instance, for the code snippet:

svg.append("g")     .attr("class", "axis")     .attr("transform", "translate(0," + h + ")")     .call(xAxis); 

He explains using the following:

Note that we use attr() to apply transform as an attribute of g. SVG transforms are quite powerful, and can accept several different kinds of transform definitions, including scales and rotations. But we are keeping it simple here with only a translation transform, which simply pushes the whole g group over and down by some amount.

Translation transforms are specified with the easy syntax of translate(x,y), where x and y are, obviously, the number of horizontal and vertical pixels by which to translate the element.

[1]: From Chapter 8, "Cleaning it up" of Interactive Data Visualization for the Web, which used to be freely available and is now behind a paywall.

like image 56
canary_in_the_data_mine Avatar answered Sep 20 '22 00:09

canary_in_the_data_mine


The transforms are SVG transforms (for details, have a look at the standard; here are some examples). Basically, scale and translate apply the respective transformations to the coordinate system, which should work as expected in most cases. You can apply more than one transform however (e.g. first scale and then translate) and then the result might not be what you expect.

When working with the transforms, keep in mind that they transform the coordinate system. In principle, what you say is true -- if you apply a scale > 1 to an object, it will look bigger and a translate will move it to a different position relative to the other objects.

like image 31
Lars Kotthoff Avatar answered Sep 17 '22 00:09

Lars Kotthoff