Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  138] [ 3]  / answers: 1 / hits: 128361  / 11 Years ago, mon, february 24, 2014, 12:00:00

I need to know exactly the width and height for an arbitrary g element in my SVG because I need to draw a selection marker around it once the user has clicked it.



What I've seen in the internet is something like: d3.select(myG).style(width). The problem is that the element will not always have an explicit width attribute set. For instance, when I create a circle inside the g, it will have the radious (r) set instead of the width. Even if I use the window.getComputedStyle method on a circle, it will return auto.



Is there a way to calculate the width of an arbitrary svg selement in D3?



Thank you.


More From » d3.js

 Answers
13

For SVG elements



Using something like selection.node().getBBox() you get values like



{
height: 5,
width: 5,
y: 50,
x: 20
}


For HTML elements



Use selection.node().getBoundingClientRect()


[#72339] Saturday, February 22, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronans

Total Points: 460
Total Questions: 109
Total Answers: 108

Location: Slovenia
Member since Sat, Sep 11, 2021
3 Years ago
;