Skip to content

Instantly share code, notes, and snippets.

@tomgp
Last active April 28, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomgp/6a950975181a98ef466746d9f9a018f6 to your computer and use it in GitHub Desktop.
Save tomgp/6a950975181a98ef466746d9f9a018f6 to your computer and use it in GitHub Desktop.
measure the elements in a d3 selection, and get the max dimensions
function measureSelection(selection){
const nodes = [];
selection.each(function(){
nodes.push(this);
});
return nodes.reduce(function(acc,current){
const bounds = current.getBoundingClientRect();
acc[0] = Math.max(bounds.width, acc[0]);
acc[1] = Math.max(bounds.height, acc[1]);
return acc;
}, [0,0]);
}
//example usage
measureSelection(d3.selectAll('.axis text')); //returns and array [width,height]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment