Advertisement
Guest User

bandaid for jsphylosvg

a guest
Sep 6th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //first step is to check that the tree is out of frame.
  2. var frame_h = parseInt($('svg')[0].attributes.getNamedItem('height').value);
  3. //in my correctly rendering graph, the y val of the lowest path = frame_h - 20
  4. var biggest_h = 0;//this variable is for recording the largest y value seen.
  5. for( var i = 0; i < $('svg path').length; i++ ){
  6.  var temp = $('svg path')[i].attributes.getNamedItem('d').value.split(',');
  7.  //d is the string of the path object which defines where to draw the line from (M) and to (L)
  8.  //split string by ',' -> an array like ["M10.0001","280L10","280L10","540L10.0001","540"]
  9.  //note the coords should be read like e.g. "M: x = 10.0001, y = 280"
  10.  for( var j = 1; j < temp.length; j++ ){
  11.   cur_y = parseInt(temp[j].split('L')[0]);
  12.   if( biggest_h < cur_y ){ biggest_h = cur_y; }
  13.   //systematically test and record the largest y value seen.
  14.  }
  15. }
  16. if( biggest_h > frame_h ){
  17.  //this means the tree is not correctly placed and all y values need to be adjusted...
  18.  //by the following amount:
  19.  y_adjust = biggest_h - frame_h + 20;//+20 comes from -(frame_h - 20);
  20.  //let's adjust the paths first:
  21.  for( var i = 0; i < $('svg path').length; i++ ){
  22.   var temp = $('svg path')[i].attributes.getNamedItem('d').value.split(',');
  23.   //splitting to easily know where y values are (which must be replaced)
  24.   var d_replace = '' + temp[0];
  25.   for( var j = 1; j < temp.length; j++ ){
  26.    var jsplit = temp[j].split('L');
  27.    //j = 0 and j = temp.length-1 have length 1 jsplits. j=0 is already handled and won't occur here.
  28.    //otherwise it is length 2 and the 'L' character must be reinserted after the y value
  29.    if( jsplit.length != 1 ){
  30.     d_replace = d_replace + ',' + (parseInt(jsplit[0]) - y_adjust).toString() + 'L' + jsplit[1];
  31.    } else {
  32.     d_replace = d_replace + ',' + (parseInt(jsplit[0]) - y_adjust).toString();
  33.    }
  34.   }
  35.   $('svg path')[i].setAttribute('d', d_replace);
  36.  }
  37.  //thankfully the next part is much easier...
  38.  for( var i = 0; i < $('svg text').length; i++ ){
  39.   $('svg text')[i].setAttribute('y',
  40.    (parseInt($('svg text')[i].attributes.getNamedItem('y').value) - y_adjust).toString()
  41.    );
  42.  }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement