score:261
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()
score:2
once i faced with the issue when i did not know which the element currently stored in my variable (svg or html) but i needed to get it width and height. i created this function and want to share it:
function computedimensions(selection) {
var dimensions = null;
var node = selection.node();
if (node instanceof svggraphicselement) { // check if node is svg element
dimensions = node.getbbox();
} else { // else is html element
dimensions = node.getboundingclientrect();
}
console.log(dimensions);
return dimensions;
}
little demo in the hidden snippet below. we handle click on the blue div and on the red svg circle with the same function.
var svg = d3.select('svg')
.attr('width', 50)
.attr('height', 50);
function computedimensions(selection) {
var dimensions = null;
var node = selection.node();
if (node instanceof svgelement) {
dimensions = node.getbbox();
} else {
dimensions = node.getboundingclientrect();
}
console.clear();
console.log(dimensions);
return dimensions;
}
var circle = svg
.append("circle")
.attr("r", 20)
.attr("cx", 30)
.attr("cy", 30)
.attr("fill", "red")
.on("click", function() { computedimensions(circle); });
var div = d3.selectall("div").on("click", function() { computedimensions(div) });
* {
margin: 0;
padding: 0;
border: 0;
}
body {
background: #ffd;
}
.div {
display: inline-block;
background-color: blue;
margin-right: 30px;
width: 30px;
height: 30px;
}
<h3>
click on blue div block or svg circle
</h3>
<svg></svg>
<div class="div"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
score:36
.getboundingclientrect() returns the size of an element and its position relative to the viewport.we can easily get following
- left, right
- top, bottom
- height, width
example :
var element = d3.select('.elementclassname').node();
element.getboundingclientrect().width;
Source: stackoverflow.com
Related Query
- D3.js: How to get the computed width and height for an arbitrary element?
- How can I get the natural width and height of an svg image element?
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- How to get the position and width of a tspan element
- How to get the height and width of text in a span not the whole span width and height
- How do I get the width of an svg element using d3js?
- How to maintain the SVG marker width and height ?
- D3 treemap: reserve min width and height for the smallest node
- What is the default width and height of a group element in html (if it has any)?
- how can I set a height and width for this map in d3.js
- How to specify height and width of a d3 tree dynamically to allow it to take up all the space it needs
- How to define the width and id of an element created by .append()?
- how to use getElementById with getBBox to determine the svg width and height
- How to set the width and height of svg to be equal to its g?
- how to adjust width and height when i click the rect in multilevel treeemap using d3.js?
- How does the following code get away with not defining initial x and y coordinates for nodes?
- How can I get the D3.js axis ticks and positions as an array?
- Auto width and height for SVG image
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- D3 how to change the width and length of SVG
- How can I get "call" to execute for each data element in D3?
- Where can I get the .geojson file for India and not separate files for each state/territory or any other distinction?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How can i get the startAngle and endAngle of each arc in the sunburst example using d3.js?
- Draw a map with D3.js: How to get the right scale() and translate()?
- How to get value from the element using selection in d3
- How to get web element (id) from the element position in D3.js force graph
- How do you get the width of a shape that's already created in d3?
- The width seems to be zero (or NaN) always for an svg:text element in Firefox 15
- How to increase width and height of plotly area graph
More Query from same tag
- D3--Using div to place SVG Element
- Anonymous function and data binding mechanism explanation on example of D3.js implementation
- How can I get the heights of each in a group of generated D3.js elements?
- d3.js: Pie Chart Layout - Adjusting the Angle to Create Shutter Effect
- D3v6 convex hull examples
- Is there a way to tell crossfilter to treat elements of array as separate records instead of treating whole array as single key?
- Force directed graph drawn out of the allocated SVG size
- d3.js - evenly spaced bars on a time scale
- Get csv entries for grouping in D3.js
- Clean way to create scatter plot from x and y vectors
- Can I force bubble size ratio in D3 Bubble charts?
- D3.js tree layout - Want to get count of descendant
- Draw line with d3js with .data()
- Always trigger event when element of a class is clicked
- How to make datastructure of venn diagram in d3 js javascript
- d3 line.defined doesn't draw zero value
- D3.js scope issue with external data.json file and brush
- D3.js - DOM reorders after sort but chart does not update
- How to convert to nested json from dataframe
- d3 multiple svg with 1 row of data each
- Focus, blur, focusin, focusout, don't work with D3 and SVG Elements? Is there an alternative?
- D3js minimal reusable module not looping?
- How do I know which bar the mouse is in for D3 stacked bar?
- custom tooltip on cumulativeLineChart in nvd3
- Why the graph lines are off axis Y and X?
- How to flip by Y venn diagramm? (venn.js)
- Sort JSON by a given order via Array (JavaScript, D3)
- Custom checkboxes in d3 legend not working
- Making an interactive scatter plot with d3?
- Gridster grid with dc.js and d3-tip Not working