timeline.js/javascripts/timeline.min.js

1 line
12 KiB
JavaScript

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.fill=true;SVG.line="line";SVG.dashed_style="5, 5";SVG.parent_holder=false;SVG.holder=false;SVG.g=false;SVG.axis=false;SVG.graphs=[];SVG.raw_points=[];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.graphs[b]=a};SVG.hasGraph=function(a){if(typeof(SVG.graphs[a])==="undefined"){return false}else{return true}};SVG.clearGraph=function(b){if(typeof(b)==="undefined"){SVG.raw_points=[];SVG.graphs=[]}else{for(var a=0;a<SVG.raw_points.length;a++){if(SVG.raw_points[a].graph===b){SVG.raw_points[a]=undefined}}}};SVG.addPoints=function(d,c){for(var a=0;a<c.length;a++){var b={graph:d,x:c[a].x,y:c[a].y};if(typeof(c[a].label)!=="undefined"){b.label=c[a].label}else{b.label=""}if(typeof(c[a].click)!=="undefined"){b.click=c[a].click}else{b.click=false}SVG.raw_points.push(b)}SVG.raw_points.sort(function(f,e){if(f.x<e.x){return -1}else{if(f.x==e.x){return 0}else{return 1}}})};SVG.newCoordinate=function(f,d,b,e,g){var c=(g-e)/(b-d);return c*(f-d)+e};SVG.getNewXY=function(a,c,d,b){return function(e,f){return{x:SVG.newCoordinate(e,a,c,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight),y:SVG.newCoordinate(f,d,b,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop)}}};SVG.getControlPoints=function(h){var p=new Array();var o=new Array();var e=h.length-1;var l=new Array();var k=new Array();var j=new Array();var d=new Array();l[0]=0;k[0]=2;j[0]=1;d[0]=h[0]+2*h[1];for(var g=1;g<e-1;g++){l[g]=1;k[g]=4;j[g]=1;d[g]=4*h[g]+2*h[g+1]}l[e-1]=2;k[e-1]=7;j[e-1]=0;d[e-1]=8*h[e-1]+h[e];var f;for(var g=1;g<e;g++){f=l[g]/k[g-1];k[g]=k[g]-f*j[g-1];d[g]=d[g]-f*d[g-1]}p[e-1]=d[e-1]/k[e-1];for(var g=e-2;g>=0;--g){p[g]=(d[g]-j[g]*p[g+1])/k[g]}for(var g=0;g<e-1;g++){o[g]=2*h[g+1]-p[g+1]}o[e-1]=0.5*(h[e]+p[e-1]);return{p1:p,p2:o}};SVG.init=function(j){if(!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")){alert("ERROR : Your browser does not support embedded SVG.")}SVG.parent_holder=document.getElementById(j.id);var h=SVG.createElement("svg:svg",{width:j.width,height:j.height});h.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xlink",SVG.xlinkns);SVG.parent_holder.appendChild(h);SVG.holder=SVG.parent_holder.querySelector("svg");defs=SVG.createElement("defs",{});SVG.holder.appendChild(defs);if(j.grid==="small"||j.grid==="both"){var i=SVG.createElement("pattern",{id:"smallGrid",width:8,height:8,patternUnits:"userSpaceOnUse"});var e=SVG.createElement("path",{d:"M 8 0 L 0 0 0 8",fill:"none",stroke:"gray","stroke-width":"0.5"});i.appendChild(e);defs.appendChild(i)}if(j.grid==="big"||j.grid==="both"){var d=SVG.createElement("pattern",{id:"grid",width:80,height:80,patternUnits:"userSpaceOnUse"});if(j.grid==="both"){var c=SVG.createElement("rect",{width:80,height:80,fill:"url(#smallGrid)"});d.appendChild(c)}var f=SVG.createElement("path",{d:"M 80 0 L 0 0 0 80",fill:"none",stroke:"gray","stroke-width":"1"});d.appendChild(f);defs.appendChild(d)}SVG.grid=j.grid;var g=SVG.createElement("marker",{id:"markerArrow",markerWidth:13,markerHeight:13,refX:2,refY:6,orient:"auto"});var b=SVG.createElement("path",{d:"M2,2 L2,11 L10,6 L2,2",fill:"gray"});g.appendChild(b);defs.appendChild(g);SVG.g=SVG.createElement("g",{transform:"translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)"});SVG.holder.appendChild(SVG.g);if(j.x_axis===true){SVG.axis=SVG.createElement("line",{x1:SVG.marginLeft,y1:SVG.parent_holder.offsetHeight/2+1.5,x2:SVG.parent_holder.offsetWidth-13-SVG.marginRight,y2:SVG.parent_holder.offsetHeight/2+1.5,stroke:"gray","stroke-width":3,"marker-end":'url("#markerArrow")'});SVG.g.appendChild(SVG.axis)}if(SVG.grid!=="none"){var a=SVG.createElement("rect",{width:"100%",height:"100%"});if(SVG.grid==="big"||SVG.grid==="both"){a.setAttribute("fill","url(#grid)")}else{a.setAttribute("fill","url(#smallGrid)")}SVG.g.appendChild(a)}SVG.rounded=j.rounded;SVG.x_axis=j.x_axis;SVG.line=j.line;SVG.fill=j.fill;SVG.x_callback=j.x_callback};SVG.scale=function(j){var k=true;for(graph in j){k=false;break}if(k){return false}var g=false,e=0;var c=false,b=false;var d=false,l=false,q=false;for(var p=0;p<j.length;p++){if(j[p].x<g||g===false){g=j[p].x}if(j[p].x>c||c===false){c=j[p].x}if(j[p].y<e){e=j[p].y}if(j[p].y>b||b===false){b=j[p].y}}var f=SVG.getNewXY(g,c,e,b);var i=f(Math.pow(10,Math.floor(Math.log(c-g)/Math.log(10))),Math.pow(10,Math.floor(Math.log(b-e)/Math.log(10))));var o=f(0,0);var n={x:i.x-o.x,y:i.y-o.y};if(SVG.grid==="big"||SVG.grid==="both"){var a=SVG.holder.getElementById("grid");a.setAttribute("width",n.x);a.setAttribute("height",n.y);var h=f(Math.floor(g/Math.pow(10,Math.floor(Math.log(c-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(c-g)/Math.log(10))),Math.floor(e/Math.pow(10,Math.floor(Math.log(b-e)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(b-e)/Math.log(10))));a.setAttribute("y",h.y);a.setAttribute("x",h.x);a.querySelector("path").setAttribute("d","M "+n.x+" 0 L 0 0 0 "+n.y);if(SVG.grid==="both"){a.querySelector("rect").setAttribute("width",n.x);a.querySelector("rect").setAttribute("height",n.y)}}if(SVG.grid==="small"||SVG.grid==="both"){n.x=n.x/10;n.y=n.y/10;var a=SVG.holder.getElementById("smallGrid");a.setAttribute("width",n.x);a.setAttribute("height",n.y);if(SVG.grid==="small"){var m=f(Math.floor(g/Math.pow(10,Math.floor(Math.log(c-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(c-g)/Math.log(10))),Math.floor(e/Math.pow(10,Math.floor(Math.log(b-e)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(b-e)/Math.log(10))));a.setAttribute("y",m.y);a.setAttribute("x",m.x)}a.querySelector("path").setAttribute("d","M "+n.x+" 0 L 0 0 0 "+n.y)}if(SVG.x_axis===true){y=f(0,0).y;SVG.axis.setAttribute("y1",y);SVG.axis.setAttribute("y2",y)}return f};SVG.draw=function(){var A=SVG.scale(SVG.raw_points);var t=[],o;var n,m;var d;for(var r=0;r<SVG.raw_points.length;r++){var w=A(SVG.raw_points[r].x,SVG.raw_points[r].y);t.push({id:r,x:w.x,y:w.y,graph:SVG.raw_points[r].graph,click:SVG.raw_points[r].click,label:SVG.raw_points[r].label})}for(var c in SVG.graphs){var j=t.filter(function(g){return g.graph==c});o="";if(SVG.rounded===true){var k=new Array(),h=new Array();for(var r=0;r<j.length;r++){k.push(j[r].x);h.push(j[r].y)}n=SVG.getControlPoints(k);m=SVG.getControlPoints(h);for(var r=0;r<j.length-1;r++){o+="C "+n.p1[r]+" "+m.p1[r]+" "+n.p2[r]+" "+m.p2[r]+" "+j[r+1].x+" "+j[r+1].y+" "}}else{for(var r=1;r<j.length;r++){o+="L "+j[r].x+" "+j[r].y+" "}}if(SVG.line!=="none"){d=SVG.createElement("path",{"class":"line",stroke:SVG.graphs[c],"stroke-width":2,fill:"none",d:"M "+j[0].x+" "+j[0].y+" "+o});if(SVG.line==="dashed"){d.setAttribute("style","stroke-dasharray: "+SVG.dashed_style)}SVG.g.appendChild(d)}if(SVG.fill){d=SVG.createElement("path",{"class":"graph",fill:SVG.graphs[c],opacity:"0.25",stroke:"none",d:"M "+j[0].x+" "+2*SVG.marginBottom+" L "+j[0].x+" "+j[0].y+" "+o+" L "+j[j.length-1].x+" "+2*SVG.marginBottom+" Z"});SVG.g.insertBefore(d,SVG.g.querySelectorAll(".over")[0])}}var p=0;for(var r=0;r<t.length;){var b=SVG.createElement("rect",{"class":"over",id:"over_"+r,y:0,fill:"white",opacity:0,height:"100%"});var e=[r];var q=r+1;if(r<t.length-1){while(t[q].x==t[r].x){if(u>t.length){break}q++}}for(var u=p+1;u<q;u++){e.push(u)}if(r==0){b.setAttribute("x",0)}else{b.setAttribute("x",(t[r].x+t[p].x)/2)}if(r==t.length-1){b.setAttribute("width",SVG.parent_holder.offsetWidth-(t[r].x+t[r-1].x)/2+1)}else{if(r==0){b.setAttribute("width",(t[1].x+t[0].x)/2+SVG.marginLeft+1)}else{b.setAttribute("width",(t[q].x-t[p].x)/2+1)}}SVG.g.appendChild(b);b.addEventListener("mouseover",(function(g){return function(){for(var x=0;x<g.length;x++){SVG.holder.getElementById("point_"+g[x]).setAttribute("r","6");SVG.holder.getElementById("label_"+g[x]).setAttribute("display","block")}}})(e));b.addEventListener("mouseout",function(){[].forEach.call(SVG.holder.querySelectorAll(".point"),function(g){g.setAttribute("r","4")});[].forEach.call(SVG.holder.querySelectorAll(".label"),function(g){g.setAttribute("display","none")})});if(SVG.x_callback!==false&&t[r].x+2.5<SVG.parent_holder.offsetWidth-SVG.marginRight){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(SVG.raw_points[r].x)));SVG.g.appendChild(d);d.setAttribute("x",t[r].x-d.getBoundingClientRect().width/2+2.5);var B=A(0,0).y;d.setAttribute("y",SVG.parent_holder.offsetHeight-SVG.marginBottom-B);d=SVG.createElement("line",{"class":"legend_x",stroke:"gray","stroke-width":2,x1:t[r].x,x2:t[r].x,y1:B-5,y2:B+5});SVG.g.appendChild(d)}p=q-1;r=q}for(var c in SVG.graphs){var j=t.filter(function(g){return g.graph==c});for(var r=0;r<j.length;r++){d=SVG.createElement("circle",{"class":"point",id:"point_"+j[r].id,cx:j[r].x,cy:j[r].y,r:4,fill:"#333",stroke:SVG.graphs[c],"stroke-width":2});SVG.g.insertBefore(d,SVG.g.querySelectorAll(".label")[0]);if(j[r].click!==false){d.onclick=j[r].click}d.addEventListener("mouseover",function(){this.setAttribute("r","6");SVG.holder.getElementById(this.getAttribute("id").replace("point","label")).setAttribute("display","block")});if(j[r].label!==""){var v=SVG.createElement("g",{"class":"label",id:"label_"+j[r].id,transform:"translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)"});SVG.g.appendChild(v);v.addEventListener("mouseover",function(){SVG.holder.getElementById(this.getAttribute("id").replace("label","point")).setAttribute("r","6");this.setAttribute("display","block")});d=SVG.createElement("text",{});var l=j[r].label.replace("</sup>","<sup>").split("<sup>");for(var u=0;u<l.length;u++){l[u]=l[u].replace(/(<([^>]+)>)/ig,"").replace("%y",SVG.raw_points[j[r].id].y).replace("%x",SVG.raw_points[j[r].id].x);if(u%2==0){d.appendChild(document.createTextNode(l[u]))}else{var w=SVG.createElement("tspan",{dy:"-5"});w.appendChild(document.createTextNode(l[u]));d.appendChild(w)}}o=SVG.createElement("path",{stroke:"black","stroke-width":2,fill:"white",opacity:0.5});v.appendChild(o);v.appendChild(d);var z=j[r].x-d.getBoundingClientRect().width/2;var a=SVG.parent_holder.offsetHeight-j[r].y-20;var f=d.getBoundingClientRect().width;var s=d.getBoundingClientRect().height;if(j[r].x-d.getBoundingClientRect().width/2<0){z=j[r].x+20;a=SVG.parent_holder.offsetHeight-j[r].y+5;o.setAttribute("d","M "+(z-5)+" "+(a+5)+" L "+(z-5)+" "+(a-s/2+7.5)+" L "+(z-10)+" "+(a-s/2+5)+" L "+(z-5)+" "+(a-s/2+2.5)+" L "+(z-5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a+5)+" Z")}else{if(j[r].y+d.getBoundingClientRect().height+12>SVG.parent_holder.offsetHeight){z=j[r].x+20;a=SVG.parent_holder.offsetHeight-j[r].y+5;o.setAttribute("d","M "+(z-5)+" "+(a+5)+" L "+(z-5)+" "+(a-s/2+7.5)+" L "+(z-10)+" "+(a-s/2+5)+" L "+(z-5)+" "+(a-s/2+2.5)+" L "+(z-5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a+5)+" Z");if(z+f>SVG.parent_holder.offsetWidth){z=j[r].y-f-20;a=SVG.parent_holder.offsetHeight-j[r].y+5;o.setAttribute("d","M "+(z-5)+" "+(a+5)+" L "+(z-5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s/2+2.5)+" L "+(z+f+10)+" "+(a-s/2+5)+" L "+(z+f+5)+" "+(a-s/2+7.5)+" L "+(z+f+5)+" "+(a+5)+" Z")}}else{if(j[r].x+f/2+12>SVG.parent_holder.offsetWidth){z=j[r].x-f-20;a=SVG.parent_holder.offsetHeight-j[r].y+5;o.setAttribute("d","M "+(z-5)+" "+(a+5)+" L "+(z-5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s/2+2.5)+" L "+(z+f+10)+" "+(a-s/2+5)+" L "+(z+f+5)+" "+(a-s/2+7.5)+" L "+(z+f+5)+" "+(a+5)+" Z")}else{o.setAttribute("d","M "+(z-5)+" "+(a+5)+" L "+(z-5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a-s+5)+" L "+(z+f+5)+" "+(a+5)+" L "+(z+f/2+2.5)+" "+(a+5)+" L "+(z+f/2)+" "+(a+10)+" L "+(z+f/2-2.5)+" "+(a+5)+" Z")}}}d.setAttribute("x",z);d.setAttribute("y",a);v.setAttribute("display","none")}}}};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()}};