morris.js/morris.min.js

1 line
8.2 KiB
JavaScript
Raw Permalink Normal View History

2012-03-06 23:06:40 +01:00
((function(){var a,b;a=jQuery,b={},b.Line=function(){function c(c){if(!(this instanceof b.Line))return new b.Line(c);typeof c.element=="string"?this.el=a(document.getElementById(c.element)):this.el=a(c.element),this.options=a.extend({},this.defaults,c);if(this.options.data===void 0||this.options.data.length===0)return;this.el.addClass("graph-initialised"),this.precalc(),this.redraw()}return c.prototype.defaults={lineWidth:3,pointSize:4,lineColors:["#0b62a4","#7A92A3","#4da74d","#afd8f8","#edc240","#cb4b4b","#9440ed"],ymax:"auto",marginTop:25,marginRight:25,marginBottom:30,marginLeft:25,numLines:5,gridLineColor:"#aaa",gridTextColor:"#888",gridTextSize:12,gridStrokeWidth:.5,hoverPaddingX:10,hoverPaddingY:5,hoverMargin:10,hoverFillColor:"#fff",hoverBorderColor:"#ccc",hoverBorderWidth:2,hoverOpacity:.95,hoverLabelColor:"#444",hoverFontSize:12,smooth:!0,hideHover:!1,parseTime:!0},c.prototype.precalc=function(){var b,c,d,e,f,g,h,i,j=this;this.options.data.sort(function(a,b){return(a[j.options.xkey]<b[j.options.xkey])-(b[j.options.xkey]<a[j.options.xkey])}),this.columnLabels=a.map(this.options.data,function(a){return a[j.options.xkey]}),this.seriesLabels=this.options.labels,this.series=[],g=this.options.ykeys;for(d=0,f=g.length;d<f;d++)b=g[d],this.series.push(a.map(this.options.data,function(a){return a[b]}));this.options.parseTime?this.xvals=a.map(this.columnLabels,function(a){return j.parseYear(a)}):this.xvals=function(){i=[];for(var a=h=this.columnLabels.length-1;h<=0?a<=0:a>=0;h<=0?a++:a--)i.push(a);return i}.apply(this),this.xmin=Math.min.apply(null,this.xvals),this.xmax=Math.max.apply(null,this.xvals),this.xmin===this.xmax&&(this.xmin-=1,this.xmax+=1);if(typeof this.options.ymax=="string"&&this.options.ymax.slice(0,4)==="auto")return c=Math.max.apply(null,Array.prototype.concat.apply([],this.series)),this.options.ymax.length>5?this.options.ymax=Math.max(parseInt(this.options.ymax.slice(5),10),c):this.options.ymax=c},c.prototype.redraw=function(){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,_,ba=this;this.el.empty(),this.r=new Raphael(this.el[0]),s=this.measureText(this.options.ymax,this.options.gridTextSize).width+this.options.marginLeft,I=this.el.width()-s-this.options.marginRight,h=this.el.height()-this.options.marginTop-this.options.marginBottom,f=I/(this.xmax-this.xmin),g=h/this.options.ymax,D=function(a){return ba.xvals.length===1?s+I/2:s+(a-ba.xmin)*f},E=function(a){return ba.options.marginTop+h-a*g},t=h/(this.options.numLines-1);for(o=0,T=this.options.numLines-1;0<=T?o<=T:o>=T;0<=T?o++:o--)M=this.options.marginTop+o*t,H=Math.round((this.options.numLines-1-o)*this.options.ymax/(this.options.numLines-1)),this.r.text(s-this.options.marginLeft/2,M,H).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor).attr("text-anchor","end"),this.r.path("M"+s+","+M+"H"+(s+I)).attr("stroke",this.options.gridLineColor).attr("stroke-width",this.options.gridStrokeWidth);y=null,L=50;for(o=U=Math.ceil(this.xmin),V=Math.floor(this.xmax);U<=V?o<=V:o>=V;U<=V?o++:o--)r=this.options.parseTime?o:this.columnLabels[this.columnLabels.length-o-1],p=this.r.text(D(o),this.options.marginTop+h+this.options.marginBottom/2,r).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor),q=p.getBBox(),y===null||y<=q.x?y=q.x+q.width+L:p.remove();d=function(){var a,b,c,d;c=this.xvals,d=[];for(a=0,b=c.length;a<b;a++)J=c[a],d.push(D(J));return d}.call(this),A=[],W=this.series;for(P=0,R=W.length;P<R;P++)z=W[P],A.push(a.map(z,function(a,b){return{x:d[b],y:E(a)}}));for(o=X=A.length-1;X<=0?o<=0:o>=0;X<=0?o++:o--)e=A[o],e.length>1&&(u=this.createPath(e,this.options.marginTop,s,this.options.marginTop+h,s+I),this.r.path(u).attr("stroke",this.options.lineColors[o]).attr("stroke-width",this.options.lineWidth));B=function(){var a,b;b=[];for(o=0,a=A.length-1;0<=a?o<=a:o>=a;0<=a?o++:o--)b.push([]);return b}();for(o=Y=A.length-1;Y<=0?o<=0:o>=0;Y<=0?o++:o--){Z=A[o];for(Q=0,S=Z.length;Q<S;Q++)b=Z[Q],c=this.r.circle(b.x,b.y,this.options.pointSize).attr("fill",th