Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //first step is to check that the tree is out of frame.
- var frame_h = parseInt($('svg')[0].attributes.getNamedItem('height').value);
- //in my correctly rendering graph, the y val of the lowest path = frame_h - 20
- var biggest_h = 0;//this variable is for recording the largest y value seen.
- for( var i = 0; i < $('svg path').length; i++ ){
- var temp = $('svg path')[i].attributes.getNamedItem('d').value.split(',');
- //d is the string of the path object which defines where to draw the line from (M) and to (L)
- //split string by ',' -> an array like ["M10.0001","280L10","280L10","540L10.0001","540"]
- //note the coords should be read like e.g. "M: x = 10.0001, y = 280"
- for( var j = 1; j < temp.length; j++ ){
- cur_y = parseInt(temp[j].split('L')[0]);
- if( biggest_h < cur_y ){ biggest_h = cur_y; }
- //systematically test and record the largest y value seen.
- }
- }
- if( biggest_h > frame_h ){
- //this means the tree is not correctly placed and all y values need to be adjusted...
- //by the following amount:
- y_adjust = biggest_h - frame_h + 20;//+20 comes from -(frame_h - 20);
- //let's adjust the paths first:
- for( var i = 0; i < $('svg path').length; i++ ){
- var temp = $('svg path')[i].attributes.getNamedItem('d').value.split(',');
- //splitting to easily know where y values are (which must be replaced)
- var d_replace = '' + temp[0];
- for( var j = 1; j < temp.length; j++ ){
- var jsplit = temp[j].split('L');
- //j = 0 and j = temp.length-1 have length 1 jsplits. j=0 is already handled and won't occur here.
- //otherwise it is length 2 and the 'L' character must be reinserted after the y value
- if( jsplit.length != 1 ){
- d_replace = d_replace + ',' + (parseInt(jsplit[0]) - y_adjust).toString() + 'L' + jsplit[1];
- } else {
- d_replace = d_replace + ',' + (parseInt(jsplit[0]) - y_adjust).toString();
- }
- }
- $('svg path')[i].setAttribute('d', d_replace);
- }
- //thankfully the next part is much easier...
- for( var i = 0; i < $('svg text').length; i++ ){
- $('svg text')[i].setAttribute('y',
- (parseInt($('svg text')[i].attributes.getNamedItem('y').value) - y_adjust).toString()
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement