Last active
April 28, 2017 11:21
-
-
Save tomgp/6a950975181a98ef466746d9f9a018f6 to your computer and use it in GitHub Desktop.
measure the elements in a d3 selection, and get the max dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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