Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var node = window.d3.selectAll('#L1 > *:nth-child(2)');
- var bbox = node.node().getBBox();
- console.log(bbox) // {height: 44, width: 44, y: -13, x: 144}
- "TypeError: Object [ PATH ] has no method 'getBBox' "
- var getBBox = function(selector){
- var xmin, xmax, ymin, ymax,p;
- // clean up path
- var t = d3.select(selector).attr("d"); // get svg line's code
- console.log(t)
- t = t.replace(/[a-z].*/g," ") // remove relative coords, could rather tag it for later processing to absolute!
- .replace(/[sA-Z]+/gi," ").trim().split(" "); // remove letters and simplify spaces.
- console.log(t)
- for(var i in t){ // set valid initial values
- if(t[i].length>1){
- p = t[i].split(",");
- xmin = xmax = p[0]; ymin = ymax = p[1]; }
- }
- for(var i in t){ // update xmin,xmax,ymin,ymax
- p = t[i].split(",");
- if(!p[1]){ p[0]=xmin; p[1] = ymin;} // ignore relative jumps such h20 v-10
- xmin = Math.min(xmin, p[0]);
- xmax = Math.max(xmax, p[0]);
- ymin = Math.min(ymin, p[1]);
- ymax = Math.max(ymax, p[1]);
- } return [[xmin,ymax],[xmax,ymin]]; // [[left, bottom], [right, top]] as for https://github.com/mbostock/d3/wiki/Geo-Paths#bounds
- }
- var bb = getBBox("path");
Add Comment
Please, Sign In to add comment