tomgp
4/28/2017 - 11:21 AM

measure the elements in a d3 selection, and get the max dimensions

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]