diff --git a/timeline.js b/timeline.js index 845b656..75e39ab 100644 --- a/timeline.js +++ b/timeline.js @@ -21,8 +21,7 @@ SVG.labels = []; * height / width = size of the svg * grid = small / big / both */ -SVG.init = function (id, height, width, grid, x_axis, rounded) -{ +SVG.init = function (id, height, width, grid, x_axis, rounded) { if(!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1")) { alert("Your browser does not support embedded SVG."); } @@ -124,6 +123,54 @@ SVG.init = function (id, height, width, grid, x_axis, rounded) SVG.rounded = rounded; SVG.x_axis = x_axis; + + SVG.parent_holder.addEventListener('mousemove', function(e) { + var evt = e || window.event; + var rect = false; + + // Reinitialize all states + var rects = SVG.holder.querySelectorAll('.over'); + for(rect = 0; rect < rects.length; rect ++) { + SVG.holder.getElementById(rects[rect].getAttribute('id').replace('over', 'point')).setAttribute('r', '4'); + if(SVG.labels[graph][parseInt(rects[rect].getAttribute('id').replace('over_', ''))] !== '') { + SVG.holder.getElementById(rects[rect].getAttribute('id').replace('over', 'label')).setAttribute('display', 'none'); + } + } + + SVG.overEffect(evt.clientX, evt.clientY); + }); +} + +SVG.hasClass = function (element, cls) { + return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + cls + ' ') > -1; +} + +SVG.overEffect = function(x, y) { + if(!document.elementFromPoint(x, y)) { + return; + } + + // Recursive function to pass event to all superposed rects + var rect = document.elementFromPoint(x, y); + + if(!SVG.hasClass(rect, 'over')) { + return; + } + + // Handle the event on current rect + SVG.holder.getElementById(rect.getAttribute('id').replace('over', 'point')).setAttribute('r', '6'); + if(SVG.labels[graph][parseInt(rect.getAttribute('id').replace('over_', ''))] !== '') { + SVG.holder.getElementById(rect.getAttribute('id').replace('over', 'label')).setAttribute('display', 'block'); + } + + // Hide it + rect.setAttribute('display', 'none'); + + // Recursive call + SVG.overEffect(x, y); + + // Display again the rect element + rect.setAttribute('display', 'block'); } SVG.newCoordinates = function(value, min, max, minValue, maxValue) { @@ -444,29 +491,16 @@ SVG.draw = function() { rect.setAttribute('fill', 'white'); rect.setAttribute('opacity', '0'); if(point == SVG.raw_points[graph].data.length - 1) { - rect.setAttribute('width', (x[point] - x[point - 1])/2 + SVG.marginLeft); + rect.setAttribute('width', SVG.parent_holder.offsetWidth - (x[point] + x[point - 1])/2); } else if(point == 0) { - rect.setAttribute('width', (x[1] - x[0])/2 + SVG.marginLeft); + rect.setAttribute('width', (x[1] + x[0])/2 + SVG.marginLeft); } else { rect.setAttribute('width', (x[point + 1] - x[point - 1])/2); } rect.setAttribute('height', '100%'); SVG.g.appendChild(rect); - - rect.addEventListener('mouseover', function() { - SVG.holder.getElementById(this.getAttribute('id').replace('over', 'point')).setAttribute('r', '6'); - if(SVG.labels[graph][parseInt(this.getAttribute('id').replace('over_', ''))] !== '') { - SVG.holder.getElementById(this.getAttribute('id').replace('over', 'label')).setAttribute('display', 'block'); - } - }) - rect.addEventListener('mouseout', function() { - SVG.holder.getElementById(this.getAttribute('id').replace('over', 'point')).setAttribute('r', '4'); - if(SVG.labels[graph][parseInt(this.getAttribute('id').replace('over_', ''))] !== '') { - SVG.holder.getElementById(this.getAttribute('id').replace('over', 'label')).setAttribute('display', 'none'); - } - }) } } } diff --git a/timeline.min.js b/timeline.min.js index e62fa8c..90c45db 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.init=function(f,v,g,e,u,j){if(!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")){alert("Your browser does not support embedded SVG.")}SVG.parent_holder=document.getElementById(f);var s=document.createElementNS(SVG.ns,"svg:svg");s.setAttribute("width",g);s.setAttribute("height",v);s.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xlink",SVG.xlinkns);SVG.parent_holder.appendChild(s);SVG.holder=SVG.parent_holder.querySelector("svg");var o=document.createElementNS(SVG.ns,"defs");SVG.holder.appendChild(o);if(e==="small"||e==="both"){var t=document.createElementNS(SVG.ns,"pattern");t.setAttribute("id","smallGrid");t.setAttribute("width",8);t.setAttribute("height",8);t.setAttribute("patternUnits","userSpaceOnUse");var l=document.createElementNS(SVG.ns,"path");l.setAttribute("d","M 8 0 L 0 0 0 8");l.setAttribute("fill","none");l.setAttribute("stroke","gray");l.setAttribute("stroke-width","0.5");t.appendChild(l);o.appendChild(t)}if(e==="big"||e==="both"){var k=document.createElementNS(SVG.ns,"pattern");k.setAttribute("id","grid");k.setAttribute("width",80);k.setAttribute("height",80);k.setAttribute("patternUnits","userSpaceOnUse");if(e==="both"){var h=document.createElementNS(SVG.ns,"rect");h.setAttribute("width",80);h.setAttribute("height",80);h.setAttribute("fill","url(#smallGrid)");k.appendChild(h)}var q=document.createElementNS(SVG.ns,"path");q.setAttribute("d","M 80 0 L 0 0 0 80");q.setAttribute("fill","none");q.setAttribute("stroke","gray");q.setAttribute("stroke-width","1");k.appendChild(q);o.appendChild(k)}SVG.grid=e;var p=document.createElementNS(SVG.ns,"marker");p.setAttribute("id","markerArrow");p.setAttribute("markerWidth",13);p.setAttribute("markerHeight",13);p.setAttribute("refX",2);p.setAttribute("refY",6);p.setAttribute("orient","auto");var d=document.createElementNS(SVG.ns,"path");d.setAttribute("d","M2,2 L2,11 L10,6 L2,2");d.setAttribute("fill","gray");p.appendChild(d);o.appendChild(p);SVG.g=document.createElementNS(SVG.ns,"g");SVG.g.setAttribute("transform","translate(0, "+SVG.parent_holder.offsetHeight+") scale(1, -1)");SVG.holder.appendChild(SVG.g);if(u===true){SVG.axis=document.createElementNS(SVG.ns,"line");SVG.axis.setAttribute("x1",SVG.marginLeft);SVG.axis.setAttribute("x2",SVG.parent_holder.offsetWidth-13-SVG.marginRight);SVG.axis.setAttribute("stroke","gray");SVG.axis.setAttribute("stroke-width",3);SVG.axis.setAttribute("marker-end",'url("#markerArrow")');SVG.g.appendChild(SVG.axis)}if(SVG.grid!=="none"){var e=document.createElementNS(SVG.ns,"rect");e.setAttribute("width","100%");e.setAttribute("height","100%");if(SVG.grid==="big"||SVG.grid==="both"){e.setAttribute("fill","url(#grid)")}else{e.setAttribute("fill","url(#smallGrid)")}SVG.g.appendChild(e)}SVG.rounded=j;SVG.x_axis=u};SVG.newCoordinates=function(h,f,d,g,j){var e=(j-g)/(d-f);return e*h-e*f+g};SVG.scale=function(o){var h=new Array(),g=new Array();var e=new Array(),d=new Array();var u=0,t=0;var f=false,s=false,v=false;var q=false;var p=0;var l=false;var k=false;for(graph in o){q=false;p=0;l=false;k=false;for(point=0;pointl||l===false){l=u}if(tk||k===false){k=t}}h.push(q);g.push(p);e.push(l);d.push(k)}h=Math.min.apply(null,h);g=Math.min.apply(null,g);e=Math.max.apply(null,e);d=Math.max.apply(null,d);u=SVG.newCoordinates(h+Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight)-SVG.newCoordinates(h,h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight);t=SVG.newCoordinates(g+Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop)-SVG.newCoordinates(g,g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop);if(SVG.grid==="big"||SVG.grid==="both"){SVG.holder.getElementById("grid").setAttribute("width",u);SVG.holder.getElementById("grid").setAttribute("height",t);SVG.holder.getElementById("grid").setAttribute("y",SVG.newCoordinates(Math.floor(g/Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop));SVG.holder.getElementById("grid").setAttribute("x",SVG.newCoordinates(Math.floor(h/Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight));SVG.holder.getElementById("grid").querySelector("path").setAttribute("d","M "+u+" 0 L 0 0 0 "+t);if(SVG.grid==="both"){SVG.holder.getElementById("grid").querySelector("rect").setAttribute("width",u);SVG.holder.getElementById("grid").querySelector("rect").setAttribute("height",t)}}if(SVG.grid==="small"||SVG.grid==="both"){u=u/10;t=t/10;SVG.holder.getElementById("smallGrid").setAttribute("width",u);SVG.holder.getElementById("smallGrid").setAttribute("height",t);if(SVG.grid==="small"){SVG.holder.getElementById("smallGrid").setAttribute("y",SVG.newCoordinates(Math.floor(g/Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop));SVG.holder.getElementById("smallGrid").setAttribute("x",SVG.newCoordinates(Math.floor(h/Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight))}SVG.holder.getElementById("smallGrid").querySelector("path").setAttribute("d","M "+u+" 0 L 0 0 0 "+t)}if(SVG.x_axis===true){t=SVG.newCoordinates(0,g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop);SVG.axis.setAttribute("y1",t);SVG.axis.setAttribute("y2",t)}var j=new Array();j.minX=h;j.minY=g;j.maxX=e;j.maxY=d;return j};SVG.addGraph=function(e,d){SVG.raw_points[e]={};SVG.raw_points[e].color=d;SVG.raw_points[e].data=new Array();SVG.labels[e]=new Array()};SVG.addPoints=function(e,d){d.sort(function(g,f){if(g.x=0;--i){p1[i]=(r[i]-c[i]*p1[i+1])/b[i]}for(i=0;i","").split("");var j=0;var h=false;for(j=0;j]+)>)/ig,"").replace("%y",SVG.raw_points[graph].data[w][1]).replace("%x",SVG.raw_points[graph].data[w][0]);if(j%2==0){k.appendChild(document.createTextNode(z[j]))}else{h=document.createElementNS(SVG.ns,"tspan");h.setAttribute("dy","-5");h.appendChild(document.createTextNode(z[j]));k.appendChild(h)}}var A=document.createElementNS(SVG.ns,"path");A.setAttribute("stroke","black");A.setAttribute("stroke-width",2);A.setAttribute("fill","white");A.setAttribute("opacity",0.5);l.appendChild(A);l.appendChild(k);var f=u[w]-k.getBoundingClientRect().width/2;var p=SVG.parent_holder.offsetHeight-q[w]-20;var o=k.getBoundingClientRect().width;var d=k.getBoundingClientRect().height;if(u[w]+o/2>SVG.parent_holder.offsetWidth){f=u[w]-o-20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d/2+2.5)+" L "+(f+o+10)+" "+(p-d/2+5)+" L "+(f+o+5)+" "+(p-d/2+7.5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{if(u[w]-k.getBoundingClientRect().width/2<0){f=u[w]+20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d/2+7.5)+" L "+(f-10)+" "+(p-d/2+5)+" L "+(f-5)+" "+(p-d/2+2.5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{if(q[w]+k.getBoundingClientRect().height>SVG.parent_holder.offsetHeight){f=u[w]+20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d/2+7.5)+" L "+(f-10)+" "+(p-d/2+5)+" L "+(f-5)+" "+(p-d/2+2.5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" L "+(f+o/2+2.5)+" "+(p+5)+" L "+(f+o/2)+" "+(p+10)+" L "+(f+o/2-2.5)+" "+(p+5)+" Z")}}}k.setAttribute("x",f);k.setAttribute("y",p);l.setAttribute("display","none")}}for(w=0;w-1};SVG.overEffect=function(d,f){if(!document.elementFromPoint(d,f)){return}var e=document.elementFromPoint(d,f);if(!SVG.hasClass(e,"over")){return}SVG.holder.getElementById(e.getAttribute("id").replace("over","point")).setAttribute("r","6");if(SVG.labels[graph][parseInt(e.getAttribute("id").replace("over_",""))]!==""){SVG.holder.getElementById(e.getAttribute("id").replace("over","label")).setAttribute("display","block")}e.setAttribute("display","none");SVG.overEffect(d,f);e.setAttribute("display","block")};SVG.newCoordinates=function(h,f,d,g,j){var e=(j-g)/(d-f);return e*h-e*f+g};SVG.scale=function(o){var h=new Array(),g=new Array();var e=new Array(),d=new Array();var u=0,t=0;var f=false,s=false,v=false;var q=false;var p=0;var l=false;var k=false;for(graph in o){q=false;p=0;l=false;k=false;for(point=0;pointl||l===false){l=u}if(tk||k===false){k=t}}h.push(q);g.push(p);e.push(l);d.push(k)}h=Math.min.apply(null,h);g=Math.min.apply(null,g);e=Math.max.apply(null,e);d=Math.max.apply(null,d);u=SVG.newCoordinates(h+Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight)-SVG.newCoordinates(h,h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight);t=SVG.newCoordinates(g+Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop)-SVG.newCoordinates(g,g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop);if(SVG.grid==="big"||SVG.grid==="both"){SVG.holder.getElementById("grid").setAttribute("width",u);SVG.holder.getElementById("grid").setAttribute("height",t);SVG.holder.getElementById("grid").setAttribute("y",SVG.newCoordinates(Math.floor(g/Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop));SVG.holder.getElementById("grid").setAttribute("x",SVG.newCoordinates(Math.floor(h/Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight));SVG.holder.getElementById("grid").querySelector("path").setAttribute("d","M "+u+" 0 L 0 0 0 "+t);if(SVG.grid==="both"){SVG.holder.getElementById("grid").querySelector("rect").setAttribute("width",u);SVG.holder.getElementById("grid").querySelector("rect").setAttribute("height",t)}}if(SVG.grid==="small"||SVG.grid==="both"){u=u/10;t=t/10;SVG.holder.getElementById("smallGrid").setAttribute("width",u);SVG.holder.getElementById("smallGrid").setAttribute("height",t);if(SVG.grid==="small"){SVG.holder.getElementById("smallGrid").setAttribute("y",SVG.newCoordinates(Math.floor(g/Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(d-g)/Math.log(10))),g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop));SVG.holder.getElementById("smallGrid").setAttribute("x",SVG.newCoordinates(Math.floor(h/Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))))*Math.pow(10,Math.floor(Math.log(e-h)/Math.log(10))),h,e,SVG.marginLeft,SVG.parent_holder.offsetWidth-SVG.marginRight))}SVG.holder.getElementById("smallGrid").querySelector("path").setAttribute("d","M "+u+" 0 L 0 0 0 "+t)}if(SVG.x_axis===true){t=SVG.newCoordinates(0,g,d,2*SVG.marginBottom,SVG.parent_holder.offsetHeight-SVG.marginTop);SVG.axis.setAttribute("y1",t);SVG.axis.setAttribute("y2",t)}var j=new Array();j.minX=h;j.minY=g;j.maxX=e;j.maxY=d;return j};SVG.addGraph=function(e,d){SVG.raw_points[e]={};SVG.raw_points[e].color=d;SVG.raw_points[e].data=new Array();SVG.labels[e]=new Array()};SVG.addPoints=function(e,d){d.sort(function(g,f){if(g.x=0;--i){p1[i]=(r[i]-c[i]*p1[i+1])/b[i]}for(i=0;i","").split("");var j=0;var h=false;for(j=0;j]+)>)/ig,"").replace("%y",SVG.raw_points[graph].data[w][1]).replace("%x",SVG.raw_points[graph].data[w][0]);if(j%2==0){k.appendChild(document.createTextNode(z[j]))}else{h=document.createElementNS(SVG.ns,"tspan");h.setAttribute("dy","-5");h.appendChild(document.createTextNode(z[j]));k.appendChild(h)}}var A=document.createElementNS(SVG.ns,"path");A.setAttribute("stroke","black");A.setAttribute("stroke-width",2);A.setAttribute("fill","white");A.setAttribute("opacity",0.5);l.appendChild(A);l.appendChild(k);var f=u[w]-k.getBoundingClientRect().width/2;var p=SVG.parent_holder.offsetHeight-q[w]-20;var o=k.getBoundingClientRect().width;var d=k.getBoundingClientRect().height;if(u[w]+o/2>SVG.parent_holder.offsetWidth){f=u[w]-o-20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d/2+2.5)+" L "+(f+o+10)+" "+(p-d/2+5)+" L "+(f+o+5)+" "+(p-d/2+7.5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{if(u[w]-k.getBoundingClientRect().width/2<0){f=u[w]+20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d/2+7.5)+" L "+(f-10)+" "+(p-d/2+5)+" L "+(f-5)+" "+(p-d/2+2.5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{if(q[w]+k.getBoundingClientRect().height>SVG.parent_holder.offsetHeight){f=u[w]+20;p=SVG.parent_holder.offsetHeight-q[w]+5;A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d/2+7.5)+" L "+(f-10)+" "+(p-d/2+5)+" L "+(f-5)+" "+(p-d/2+2.5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" Z")}else{A.setAttribute("d","M "+(f-5)+" "+(p+5)+" L "+(f-5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p-d+5)+" L "+(f+o+5)+" "+(p+5)+" L "+(f+o/2+2.5)+" "+(p+5)+" L "+(f+o/2)+" "+(p+10)+" L "+(f+o/2-2.5)+" "+(p+5)+" Z")}}}k.setAttribute("x",f);k.setAttribute("y",p);l.setAttribute("display","none")}}for(w=0;w