7

I am using D3 to add attributes to a g element. I am also using D3-selection-multi to make writing the attributes easier (allows you to pass an object to .attrs() function).

using webpack and babel-preset-env

d3.select('g').attrs({
  'class': 'thing',
  'transform-origin': '50% 50%',
  'transform': `translate(${opts.w/2} ${opts.h/2}) rotate(${opts.angle}deg)`
})

I keep getting this error: attribute transform: Expected ')'

Jamie S
  • 2,029
  • 2
  • 13
  • 19

2 Answers2

14

Turns out you can't use units in SVG transforms.

rotate(45deg) - doesn't work and will throw an error.

rotate(45) - expected behavior.

Jamie S
  • 2,029
  • 2
  • 13
  • 19
  • If I remove units, it seems to break in Safari. Any fix for that? see: https://stackoverflow.com/questions/49576545/error-text-attribute-transform-expected-translate-50-50 – Leon Vogler Jan 11 '23 at 12:21
-1

Try switching "attr" to "style"

d3.select('g').attrs('class': 'thing')
    .style('transform-origin', '50% 50%')
    .style('transform', `translate(${opts.w/2} ${opts.h/2}) rotate(${opts.angle}deg)`);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129