morris.js/morris.min.js

1 line
7.8 KiB
JavaScript
Raw Normal View History

((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},c.prototype.precalc=function(){var b,c,d,e,f,g=this;this.options.data.sort(function(a,b){return(a[g.options.xkey]<b[g.options.xkey])-(b[g.options.xkey]<a[g.options.xkey])}),this.columnLabels=a.map(this.options.data,function(a){return a[g.options.xkey]}),this.seriesLabels=this.options.labels,this.series=[],f=this.options.ykeys;for(d=0,e=f.length;d<e;d++)b=f[d],this.series.push(a.map(this.options.data,function(a){return a[b]}));this.xvals=a.map(this.columnLabels,function(a){return g.parseYear(a)}),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,_=this;this.el.empty(),this.r=new Raphael(this.el[0]),r=this.measureText(this.options.ymax,this.options.gridTextSize).width+this.options.marginLeft,H=this.el.width()-r-this.options.marginRight,h=this.el.height()-this.options.marginTop-this.options.marginBottom,f=H/(this.xmax-this.xmin),g=h/this.options.ymax,C=function(a){return _.xvals.length===1?r+H/2:r+(a-_.xmin)*f},D=function(a){return _.options.marginTop+h-a*g},s=h/(this.options.numLines-1);for(o=0,S=this.options.numLines-1;0<=S?o<=S:o>=S;0<=S?o++:o--)L=this.options.marginTop+o*s,G=Math.round((this.options.numLines-1-o)*this.options.ymax/(this.options.numLines-1)),this.r.text(r-this.options.marginLeft/2,L,G).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor).attr("text-anchor","end"),this.r.path("M"+r+","+L+"H"+(r+H)).attr("stroke",this.options.gridLineColor).attr("stroke-width",this.options.gridStrokeWidth);x=null,K=50;for(o=T=Math.ceil(this.xmin),U=Math.floor(this.xmax);T<=U?o<=U:o>=U;T<=U?o++:o--)p=this.r.text(C(o),this.options.marginTop+h+this.options.marginBottom/2,o).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor),q=p.getBBox(),x===null||x<=q.x?x=q.x+q.width+K:p.remove();d=function(){var a,b,c,d;c=this.xvals,d=[];for(a=0,b=c.length;a<b;a++)I=c[a],d.push(C(I));return d}.call(this),z=[],V=this.series;for(O=0,Q=V.length;O<Q;O++)y=V[O],z.push(a.map(y,function(a,b){return{x:d[b],y:D(a)}}));for(o=W=z.length-1;W<=0?o<=0:o>=0;W<=0?o++:o--)e=z[o],e.length>1&&(t=this.createPath(e,this.options.marginTop,r,this.options.marginTop+h,r+H),this.r.path(t).attr("stroke",this.options.lineColors[o]).attr("stroke-width",this.options.lineWidth));A=function(){var a,b;b=[];for(o=0,a=z.length-1;0<=a?o<=a:o>=a;0<=a?o++:o--)b.push([]);return b}();for(o=X=z.length-1;X<=0?o<=0:o>=0;X<=0?o++:o--){Y=z[o];for(P=0,R=Y.length;P<R;P++)b=Y[P],c=this.r.circle(b.x,b.y,this.options.pointSize).attr("fill",this.options.lineColors[o]).attr("stroke-width",1).attr("stroke","#ffffff"),A[o].push(c)}l=this.options.hoverFontSize*1.5*(this.series.length+1),k=this.r.rect(-10,-l/2-this.options.hoverPaddingY,20,l+this.options.hoverPaddingY*2,10).attr("fill",this.options.hoverF