morris.js/morris.min.js

1 line
8.6 KiB
JavaScript
Raw Normal View History

2012-03-08 21:03:45 +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",ymin:"auto 0",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,k=this;this.options.data.sort(function(a,b){return(a[k.options.xkey]<b[k.options.xkey])-(b[k.options.xkey]<a[k.options.xkey])}),this.columnLabels=a.map(this.options.data,function(a){return a[k.options.xkey]}),this.seriesLabels=this.options.labels,this.series=[],h=this.options.ykeys;for(e=0,g=h.length;e<g;e++)b=h[e],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 k.parseYear(a)}):this.xvals=function(){j=[];for(var a=i=this.columnLabels.length-1;i<=0?a<=0:a>=0;i<=0?a++:a--)j.push(a);return j}.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),typeof this.options.ymax=="string"&&this.options.ymax.slice(0,4)==="auto"&&(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);if(typeof this.options.ymin=="string"&&this.options.ymin.slice(0,4)==="auto")return d=Math.min.apply(null,Array.prototype.concat.apply([],this.series)),this.options.ymin.length>5?this.options.ymin=Math.min(parseInt(this.options.ymin.slice(5),10),d):this.options.ymin=d},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,bb,bc,bd=this;this.el.empty(),this.r=new Raphael(this.el[0]),w=Math.max(this.measureText(this.options.ymin,this.options.gridTextSize).width,this.measureText(this.options.ymax,this.options.gridTextSize).width),u=w+this.options.marginLeft,L=this.el.width()-u-this.options.marginRight,i=this.el.height()-this.options.marginTop-this.options.marginBottom,f=L/(this.xmax-this.xmin),g=i/(this.options.ymax-this.options.ymin),G=function(a){return bd.xvals.length===1?u+L/2:u+(a-bd.xmin)*f},H=function(a){return bd.options.marginTop+i-(a-bd.options.ymin)*g},Q=(this.options.ymax-this.options.ymin)/(this.options.numLines-1),h=Math.ceil(this.options.ymin/Q)*Q,t=Math.floor(this.options.ymax/Q)*Q;for(v=h;h<=t?v<=t:v>=t;v+=Q)K=Math.floor(v),P=H(K),this.r.text(u-this.options.marginLeft/2,P,K).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor).attr("text-anchor","end"),this.r.path("M"+u+","+P+"H"+(u+L)).attr("stroke",this.options.gridLineColor).attr("stroke-width",this.options.gridStrokeWidth);B=null,O=50;for(p=X=Math.ceil(this.xmin),Y=Math.floor(this.xmax);X<=Y?p<=Y:p>=Y;X<=Y?p++:p--)s=this.options.parseTime?p:this.columnLabels[this.columnLabels.length-p-1],q=this.r.text(G(p),this.options.marginTop+i+this.options.marginBottom/2,s).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor),r=q.getBBox(),B===null||B<=r.x?B=r.x+r.width+O:q.remove();d=function(){var a,b,c,d;c=this.xvals,d=[];for(a=0,b=c.length;a<b;a++)M=c[a],d.push(G(M));return d}.call(this),D=[],Z=this.series;for(T=0,V=Z.length;T<V;T++)C=Z[T],D.push(a.map(C,function(a,b){return{x:d[b],y:H(a)}}));for(p=_=D.length-1;_<=0?p<=0:p>=0;_<=0?p++:p--)e=D[p],e.length>1&&(x=this.createPath(e,this.options.margi