8

When I run d3.select(this).attr("transform") on an element, I get a response translate(20.00,778). However I need to get individual values of translate.

In v3, one can use

var t = d3.transform(element.attr("transform"));
t.translate;

However d3.transform is not available in v4. How to achieve an equivalent result?

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
Joel
  • 1,650
  • 2
  • 22
  • 34
  • 1
    You can achieve it by creating your own function. THis answer shows how to do it: http://stackoverflow.com/a/38230545/5768908 – Gerardo Furtado Aug 03 '16 at 23:53
  • https://stackoverflow.com/questions/20340263/d3-retrieve-and-add-to-current-selections-attribute-value – M S Jul 26 '17 at 18:07

1 Answers1

8

Try this.

string = element.attr("transform");

translate = string.substring(string.indexOf("(")+1, string.indexOf(")")).split(",");

Then you can access dx by translate[0] and dy by translate[1].

patrickcipot
  • 307
  • 2
  • 10
  • 1
    Although it would be clean to use D3 function, this helps for the time being – Joel Aug 04 '16 at 00:29
  • 2
    This doesn't work for in Edge as edge does separate the arguments with whitespace such as translate(10 20) instead of translate(10,20) like chrome and firefox. – Andreas Dec 08 '17 at 07:37