timeline.js/javascripts/timeline.min.js
2014-04-18 21:57:32 +02:00

1 line
13 KiB
JavaScript

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