In D3.js v4 the d3.transform method has been removed, without any hint about how replacing it. Does anyone know how to replace the following D3.js v3 instruction?
d3.transform(String).translate;
In D3.js v4 the d3.transform method has been removed, without any hint about how replacing it. Does anyone know how to replace the following D3.js v3 instruction?
d3.transform(String).translate;
Edit 2016-10-07: For a more general approach see addendum below.
According to the changelog it is gone. There is a function in
transform/decompose.js
, though, which does the calculations for internal use. Sadly, it is not exposed for external use.That said, this is easily done even without putting any D3 to use:
This creates a dummy
g
element for calculation purposes using standard DOM methods and sets itstransform
attribute to the string containing your transformations. It then calls.consolidate()
of theSVGTransformList
interface to consolidate the possibly long list of transformation to a singleSVGTransform
of typeSVG_TRANSFORM_MATRIX
which contains the boiled down version of all transformations in itsmatrix
property. ThisSVGMatrix
per definition holds the values for the translation in its propertiese
andf
.Using this function
getTranslation()
you could rewrite your D3 v3 statementas
Addendum
Because this answer has gained some interest over time, I decided to put together a more general method capable of returning not only the translation but the values of all transformation definitions of a transform string. The basic approach is the same as laid out in my original post above plus the calculations taken from
transform/decompose.js
. This function will return an object having properties for all transformation definitions much like the formerd3.transform()
did.I am a little late to the party, but I had some code that was beneficial to me, I hope it helps you out too.
The code above by @altocumulus is quite thorough and works like a charm. However it didn't quite meet my needs since I was doing the calculations by hand and needed to alter some transform properties as painlessly as possible.
This might not be the solution for everyone, but it was perfect for me.
The really great thing with the first function is that I can manually alter a specific property (e.g. rotation), and not have to worry about how it affects translate or anything else (when rotating around a point), whereas when I rely on the built-in or even
d3.transform
methods they consolidate all the properties into one value.Why is this cool?
Imagine a some HTML
Using d3.transfrom I get:
In object form
In string form
Which is correct mathematically, but makes it hard for me to simply remove the angle of rotation and the translation that had to be introduced to rotate this element around a given point.
Using my
_getTokenizedTransformAttributeValue
functionIn object form
In string form using the function
_getStringFromTokenizedTransformAttributeObj
Which is perfect because now when you remove the rotation, your element can go back to where it was
Granted, the code could be cleaner and the function names more concise, but I really wanted to get this out there so others could benefit from it.
I found a way do achieve something similar by using this:
this will give you access to the x/y position and width/height You can see an example here: https://bl.ocks.org/mbostock/1160929
I found a little bit simpler solution than that.
In this matrix you have cordinates e and f witch are equivalent to x, y. (e === x, f === y). No need to implement your very own funtion for that.
baseVal is a list of transformations of the element. You can't use that for the object without previus transformation! (the list will be empty) Or if you done many tranformation to the object the last position will be under the last element of baseVal list.
If you pull in d3 v4 through npm, you can import the
src/transform/parse
file directly and callparseSvg
:On elements which have the d3.js
zoom
listener on them -- usually the<g>
element appended to the svg element -- you can use this call to get the transformation attributes outside of the zoom function:This returns the same values as when calling
d3.event.transform
within the zoom event function itself.Calling
d3.event.transform
outside the zoom event function will error:Uncaught TypeError: Cannot read property 'transform' of null
I have to use
d3.zoomTransform
to allow panning and zooming from buttons outside the graph.