timeline.js/timeline.min.js

1 line
13 KiB
JavaScript

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