Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. var node = window.d3.selectAll('#L1 > *:nth-child(2)');
  2. var bbox = node.node().getBBox();
  3. console.log(bbox) // {height: 44, width: 44, y: -13, x: 144}
  4.  
  5. "TypeError: Object [ PATH ] has no method 'getBBox' "
  6.  
  7. var getBBox = function(selector){
  8. var xmin, xmax, ymin, ymax,p;
  9. // clean up path
  10. var t = d3.select(selector).attr("d"); // get svg line's code
  11. console.log(t)
  12. t = t.replace(/[a-z].*/g," ") // remove relative coords, could rather tag it for later processing to absolute!
  13. .replace(/[sA-Z]+/gi," ").trim().split(" "); // remove letters and simplify spaces.
  14. console.log(t)
  15.  
  16. for(var i in t){ // set valid initial values
  17. if(t[i].length>1){
  18. p = t[i].split(",");
  19. xmin = xmax = p[0]; ymin = ymax = p[1]; }
  20. }
  21. for(var i in t){ // update xmin,xmax,ymin,ymax
  22. p = t[i].split(",");
  23. if(!p[1]){ p[0]=xmin; p[1] = ymin;} // ignore relative jumps such h20 v-10
  24. xmin = Math.min(xmin, p[0]);
  25. xmax = Math.max(xmax, p[0]);
  26. ymin = Math.min(ymin, p[1]);
  27. ymax = Math.max(ymax, p[1]);
  28. } return [[xmin,ymax],[xmax,ymin]]; // [[left, bottom], [right, top]] as for https://github.com/mbostock/d3/wiki/Geo-Paths#bounds
  29. }
  30. var bb = getBBox("path");
Add Comment
Please, Sign In to add comment