From fdd63375545ccaf3554529fd6552271d9a3fb68b Mon Sep 17 00:00:00 2001 From: Phyks Date: Sun, 6 Apr 2014 22:06:14 +0200 Subject: [PATCH] Bugfix with vertical scale --- timeline.js | 4 ++-- timeline.min.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/timeline.js b/timeline.js index cabbb1c..f7dea99 100644 --- a/timeline.js +++ b/timeline.js @@ -118,7 +118,7 @@ SVG.addPoints = function (graph, data) { // Compute new coordinates, knowing the min and max value to fit the graph in the container SVG.newCoordinate = function(value, min, max, minValue, maxValue) { var a = (maxValue - minValue) / (max - min); - return a * value - a * min + minValue; + return a *(value - min) + minValue; }; // Compute new X and Y values @@ -127,7 +127,7 @@ SVG.getNewXY = function (minX, maxX, minY, maxY) { return function (x, y) { return { 'x': SVG.newCoordinate(x, minX, maxX, SVG.marginLeft, SVG.parent_holder.offsetWidth - SVG.marginRight), - 'y': SVG.newCoordinate(y, minY, maxY, 2*SVG.marginBottom, SVG.parent_holder.offsetWidth - SVG.marginTop) + 'y': SVG.newCoordinate(y, minY, maxY, 2*SVG.marginBottom, SVG.parent_holder.offsetHeight - SVG.marginTop) }; }; }; diff --git a/timeline.min.js b/timeline.min.js index 58f945e..8342972 100644 --- a/timeline.min.js +++ b/timeline.min.js @@ -1 +1 @@ -var SVG={};SVG.ns="http://www.w3.org/2000/svg";SVG.xlinkns="http://www.w3.org/1999/xlink";SVG.marginBottom=10;SVG.marginTop=15;SVG.marginLeft=10;SVG.marginRight=10;SVG.rounded=false;SVG.x_axis=false;SVG.parent_holder=false;SVG.holder=false;SVG.g=false;SVG.axis=false;SVG.raw_points=[];SVG.labels=[];SVG.click=[];SVG.x_callback=false;SVG.addElement=function(b,a){var c=document.createElementNS(SVG.ns,b);for(attr in a){svg.setAttribute(attr,a[attr])}return c};SVG.hasClass=function(b,a){return(" "+b.getAttribute("class")+" ").indexOf(" "+a+" ")>-1};SVG.addGraph=function(b,a){SVG.raw_points[b]={};SVG.raw_points[b].color=a;SVG.raw_points[b].data=new Array();SVG.labels[b]=new Array();SVG.click[b]=new Array()};SVG.hasGraph=function(a){if(SVG.raw_points[a]===undefined){return false}else{return true}};SVG.clearGraphData=function(a){if(typeof(a)==="undefined"){SVG.raw_points=[];SVG.labels=[];SVG.click=[]}else{SVG.raw_points[a].data=new Array();SVG.labels[a]=new Array();SVG.click[a]=new Array()}};SVG.addPoints=function(c,b){b.sort(function(e,d){if(e.x=0;--g){p[g]=(d[g]-j[g]*p[g+1])/k[g]}for(var g=0;gh||h===false){h=n}if(mg||g===false){g=m}}f.push(k);d.push(j);b.push(h);a.push(g)}f=Math.min.apply(null,f);d=Math.min.apply(null,d);b=Math.max.apply(null,b);a=Math.max.apply(null,a);var e=SVG.getNewXY(f,b,d,a);n=e(f+Math.pow(10,Math.floor(Math.log(b-f)/Math.log(10))),0).x-e(f,0).x;m=e(0,d+Math.pow(10,Math.floor(Math.log(a-d)/Math.log(10)))).y-e(0,d).y;if(SVG.grid==="big"||SVG.grid==="both"){SVG.holder.getElementById("grid").setAttribute("width",n);SVG.holder.getElementById("grid").setAttribute("height",m);SVG.holder.getElementById("grid").setAttribute("y",e(0,Math.floor(d/Math.pow(10,Math.floor(Math.log(a-d)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(a-d)/Math.log(10)))).y);SVG.holder.getElementById("grid").setAttribute("x",e(Math.floor(f/Math.pow(10,Math.floor(Math.log(b-f)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(b-f)/Math.log(10))),0).x);SVG.holder.getElementById("grid").querySelector("path").setAttribute("d","M "+n+" 0 L 0 0 0 "+m);if(SVG.grid==="both"){SVG.holder.getElementById("grid").querySelector("rect").setAttribute("width",n);SVG.holder.getElementById("grid").querySelector("rect").setAttribute("height",m)}}if(SVG.grid==="small"||SVG.grid==="both"){n=n/10;m=m/10;SVG.holder.getElementById("smallGrid").setAttribute("width",n);SVG.holder.getElementById("smallGrid").setAttribute("height",m);if(SVG.grid==="small"){SVG.holder.getElementById("smallGrid").setAttribute("y",e(0,Math.floor(d/Math.pow(10,Math.floor(Math.log(a-d)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(a-d)/Math.log(10)))).y);SVG.holder.getElementById("smallGrid").setAttribute("x",e(Math.floor(f/Math.pow(10,Math.floor(Math.log(b-f)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(b-f)/Math.log(10))),0).x)}SVG.holder.getElementById("smallGrid").querySelector("path").setAttribute("d","M "+n+" 0 L 0 0 0 "+m)}if(SVG.x_axis===true){m=e(0,0).y;SVG.axis.setAttribute("y1",m);SVG.axis.setAttribute("y2",m)}return e};SVG.draw=function(){var t=SVG.scale(SVG.raw_points);var h,f,m;var l,k;var d;for(var c in SVG.raw_points){h=new Array();f=new Array();m="";for(var n=0;n","").split("");for(var o=0;o]+)>)/ig,"").replace("%y",SVG.raw_points[c].data[n][1]).replace("%x",SVG.raw_points[c].data[n][0]);if(o%2==0){d.appendChild(document.createTextNode(j[o]))}else{var r=SVG.createElement("tspan",{dy:"-5"});r.appendChild(document.createTextNode(j[o]));d.appendChild(r)}}m=SVG.createElement("path",{stroke:"black","stroke-width":2,fill:"white",opacity:0.5});q.appendChild(m);q.appendChild(d);var s=h[n]-d.getBoundingClientRect().width/2;var a=SVG.parent_holder.offsetHeight-f[n]-20;var e=d.getBoundingClientRect().width;var p=d.getBoundingClientRect().height;if(h[n]-d.getBoundingClientRect().width/2<0){s=h[n]+20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p/2+7.5)+" L "+(s-10)+" "+(a-p/2+5)+" L "+(s-5)+" "+(a-p/2+2.5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" Z")}else{if(f[n]+d.getBoundingClientRect().height+12>SVG.parent_holder.offsetHeight){s=h[n]+20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p/2+7.5)+" L "+(s-10)+" "+(a-p/2+5)+" L "+(s-5)+" "+(a-p/2+2.5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" Z");if(s+e>SVG.parent_holder.offsetWidth){s=h[n]-e-20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p/2+2.5)+" L "+(s+e+10)+" "+(a-p/2+5)+" L "+(s+e+5)+" "+(a-p/2+7.5)+" L "+(s+e+5)+" "+(a+5)+" Z")}}else{if(h[n]+e/2+12>SVG.parent_holder.offsetWidth){s=h[n]-e-20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p/2+2.5)+" L "+(s+e+10)+" "+(a-p/2+5)+" L "+(s+e+5)+" "+(a-p/2+7.5)+" L "+(s+e+5)+" "+(a+5)+" Z")}else{m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" L "+(s+e/2+2.5)+" "+(a+5)+" L "+(s+e/2)+" "+(a+10)+" L "+(s+e/2-2.5)+" "+(a+5)+" Z")}}}d.setAttribute("x",s);d.setAttribute("y",a);q.setAttribute("display","none")}}for(var n=0;n=-5&&w<=5&&w>=-5){SVG.holder.getElementById(this.getAttribute("id").replace("over","point")).onclick()}}})(h[n],f[n]);if(SVG.x_callback!==false){d=SVG.createElement("text",{"class":"legend_x",fill:"gray",transform:"translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)"});d.appendChild(document.createTextNode(SVG.x_callback(h[n])));SVG.g.appendChild(d);d.setAttribute("x",h[n]-d.getBoundingClientRect().width/2+2.5);d.setAttribute("y",SVG.parent_holder.offsetHeight-SVG.marginBottom-t(0,0).y);d=SVG.createElement("line",{"class":"legend_x",stroke:"gray","stroke-width":2,x1:h[n],x2:h[n],y1:t(0,0).y-5,y2:t(0,0).y+5});SVG.g.appendChild(d)}}}};var old=window.onresize||function(){};window.onresize=function(){old();if(SVG.g!==false){SVG.g.setAttribute("transform","translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)");if(SVG.x_axis===true){SVG.axis.setAttribute("x2",SVG.parent_holder.offsetWidth-13-SVG.marginRight)}[].forEach.call(SVG.holder.querySelectorAll(".label, .over, .point, .line, .graph, .legend_x"),function(a){a.parentNode.removeChild(a)});SVG.draw()}}; \ No newline at end of file +var SVG={};SVG.ns="http://www.w3.org/2000/svg";SVG.xlinkns="http://www.w3.org/1999/xlink";SVG.marginBottom=10;SVG.marginTop=15;SVG.marginLeft=10;SVG.marginRight=10;SVG.rounded=false;SVG.x_axis=false;SVG.parent_holder=false;SVG.holder=false;SVG.g=false;SVG.axis=false;SVG.raw_points=[];SVG.labels=[];SVG.click=[];SVG.x_callback=false;SVG.createElement=function(b,a){var c=document.createElementNS(SVG.ns,b);for(attr in a){c.setAttribute(attr,a[attr])}return c};SVG.hasClass=function(b,a){return(" "+b.getAttribute("class")+" ").indexOf(" "+a+" ")>-1};SVG.addGraph=function(b,a){SVG.raw_points[b]={};SVG.raw_points[b].color=a;SVG.raw_points[b].data=new Array();SVG.labels[b]=new Array();SVG.click[b]=new Array()};SVG.hasGraph=function(a){if(SVG.raw_points[a]===undefined){return false}else{return true}};SVG.clearGraphData=function(a){if(typeof(a)==="undefined"){SVG.raw_points=[];SVG.labels=[];SVG.click=[]}else{SVG.raw_points[a].data=new Array();SVG.labels[a]=new Array();SVG.click[a]=new Array()}};SVG.addPoints=function(c,b){b.sort(function(e,d){if(e.x=0;--g){p[g]=(d[g]-j[g]*p[g+1])/k[g]}for(var g=0;gj||j===false){j=x}if(yh||h===false){h=y}}p.push(k);n.push(i);o.push(j);m.push(h)}p=Math.min.apply(null,p);n=Math.min.apply(null,n);o=Math.max.apply(null,o);m=Math.max.apply(null,m);var r=SVG.getNewXY(p,o,n,m);var b=r(Math.pow(10,Math.floor(Math.log(o-p)/Math.log(10))),Math.pow(10,Math.floor(Math.log(m-n)/Math.log(10))));if(SVG.grid==="big"||SVG.grid==="both"){SVG.holder.getElementById("grid").setAttribute("width",b.x);SVG.holder.getElementById("grid").setAttribute("height",b.y);var c=r(Math.floor(p/Math.pow(10,Math.floor(Math.log(o-p)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(o-p)/Math.log(10))),Math.floor(n/Math.pow(10,Math.floor(Math.log(m-n)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(m-n)/Math.log(10))));SVG.holder.getElementById("grid").setAttribute("y",c.y);SVG.holder.getElementById("grid").setAttribute("x",c.x);SVG.holder.getElementById("grid").querySelector("path").setAttribute("d","M "+b.x+" 0 L 0 0 0 "+b.y);if(SVG.grid==="both"){SVG.holder.getElementById("grid").querySelector("rect").setAttribute("width",b.x);SVG.holder.getElementById("grid").querySelector("rect").setAttribute("height",b.y)}}if(SVG.grid==="small"||SVG.grid==="both"){b.x=b.x/10;b.y=b.y/10;SVG.holder.getElementById("smallGrid").setAttribute("width",b.x);SVG.holder.getElementById("smallGrid").setAttribute("height",b.y);if(SVG.grid==="small"){var e=r(Math.floor(p/Math.pow(10,Math.floor(Math.log(o-p)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(o-p)/Math.log(10))),Math.floor(n/Math.pow(10,Math.floor(Math.log(m-n)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(m-n)/Math.log(10))));SVG.holder.getElementById("smallGrid").setAttribute("y",e.y);SVG.holder.getElementById("smallGrid").setAttribute("x",e.x)}SVG.holder.getElementById("smallGrid").querySelector("path").setAttribute("d","M "+b.x+" 0 L 0 0 0 "+b.y)}if(SVG.x_axis===true){y=r(0,0).y;SVG.axis.setAttribute("y1",y);SVG.axis.setAttribute("y2",y)}return r};SVG.draw=function(){var t=SVG.scale(SVG.raw_points);var h,f,m;var l,k;var d;for(var c in SVG.raw_points){h=new Array();f=new Array();m="";for(var n=0;n","").split("");for(var o=0;o]+)>)/ig,"").replace("%y",SVG.raw_points[c].data[n][1]).replace("%x",SVG.raw_points[c].data[n][0]);if(o%2==0){d.appendChild(document.createTextNode(j[o]))}else{var r=SVG.createElement("tspan",{dy:"-5"});r.appendChild(document.createTextNode(j[o]));d.appendChild(r)}}m=SVG.createElement("path",{stroke:"black","stroke-width":2,fill:"white",opacity:0.5});q.appendChild(m);q.appendChild(d);var s=h[n]-d.getBoundingClientRect().width/2;var a=SVG.parent_holder.offsetHeight-f[n]-20;var e=d.getBoundingClientRect().width;var p=d.getBoundingClientRect().height;if(h[n]-d.getBoundingClientRect().width/2<0){s=h[n]+20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p/2+7.5)+" L "+(s-10)+" "+(a-p/2+5)+" L "+(s-5)+" "+(a-p/2+2.5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" Z")}else{if(f[n]+d.getBoundingClientRect().height+12>SVG.parent_holder.offsetHeight){s=h[n]+20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p/2+7.5)+" L "+(s-10)+" "+(a-p/2+5)+" L "+(s-5)+" "+(a-p/2+2.5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" Z");if(s+e>SVG.parent_holder.offsetWidth){s=h[n]-e-20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p/2+2.5)+" L "+(s+e+10)+" "+(a-p/2+5)+" L "+(s+e+5)+" "+(a-p/2+7.5)+" L "+(s+e+5)+" "+(a+5)+" Z")}}else{if(h[n]+e/2+12>SVG.parent_holder.offsetWidth){s=h[n]-e-20;a=SVG.parent_holder.offsetHeight-f[n]+5;m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p/2+2.5)+" L "+(s+e+10)+" "+(a-p/2+5)+" L "+(s+e+5)+" "+(a-p/2+7.5)+" L "+(s+e+5)+" "+(a+5)+" Z")}else{m.setAttribute("d","M "+(s-5)+" "+(a+5)+" L "+(s-5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a-p+5)+" L "+(s+e+5)+" "+(a+5)+" L "+(s+e/2+2.5)+" "+(a+5)+" L "+(s+e/2)+" "+(a+10)+" L "+(s+e/2-2.5)+" "+(a+5)+" Z")}}}d.setAttribute("x",s);d.setAttribute("y",a);q.setAttribute("display","none")}}for(var n=0;n=-5&&z<=5&&z>=-5){SVG.holder.getElementById(this.getAttribute("id").replace("over","point")).onclick()}}})(h[n],f[n]);if(SVG.x_callback!==false){d=SVG.createElement("text",{"class":"legend_x",fill:"gray",transform:"translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)"});d.appendChild(document.createTextNode(SVG.x_callback(h[n])));SVG.g.appendChild(d);d.setAttribute("x",h[n]-d.getBoundingClientRect().width/2+2.5);var u=t(0,0).y;d.setAttribute("y",SVG.parent_holder.offsetHeight-SVG.marginBottom-u);d=SVG.createElement("line",{"class":"legend_x",stroke:"gray","stroke-width":2,x1:h[n],x2:h[n],y1:u-5,y2:u+5});SVG.g.appendChild(d)}}}};var old=window.onresize||function(){};window.onresize=function(){old();if(SVG.g!==false){SVG.g.setAttribute("transform","translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)");if(SVG.x_axis===true){SVG.axis.setAttribute("x2",SVG.parent_holder.offsetWidth-13-SVG.marginRight)}[].forEach.call(SVG.holder.querySelectorAll(".label, .over, .point, .line, .graph, .legend_x"),function(a){a.parentNode.removeChild(a)});SVG.draw()}}; \ No newline at end of file