morris.js/morris.min.js

1 line
7.3 KiB
JavaScript
Raw Normal View History

((function(){var a;a={},a.Line=function(){function b(b){if(!(this instanceof a.Line))return new a.Line(b);this.el=$(document.getElementById(b.element)),this.options=$.extend($.extend({},this.defaults),b);if(this.options.data===void 0||this.options.data.length===0)return;this.el.addClass("graph-initialised"),this.precalc(),this.redraw()}return b.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},b.prototype.precalc=function(){var a,b,c,d,e,f=this;this.columnLabels=$.map(this.options.data,function(a){return a[f.options.xkey]}),this.seriesLabels=this.options.labels,this.series=[],e=this.options.ykeys;for(c=0,d=e.length;c<d;c++)a=e[c],this.series.push($.map(this.options.data,function(b){return b[a]}));this.xvals=$.map(this.columnLabels,function(a){return f.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 b=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),b):this.options.ymax=b},b.prototype.redraw=function(){var 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,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]),q=this.measureText(this.options.ymax,this.options.gridTextSize).width+this.options.marginLeft,G=this.el.width()-q-this.options.marginRight,g=this.el.height()-this.options.marginTop-this.options.marginBottom,e=G/(this.xmax-this.xmin),f=g/this.options.ymax,B=function(a){return Z.xvals.length===1?q+G/2:q+(a-Z.xmin)*e},C=function(a){return Z.options.marginTop+g-a*f},r=g/(this.options.numLines-1);for(n=0,R=this.options.numLines-1;0<=R?n<=R:n>=R;0<=R?n++:n--)K=this.options.marginTop+n*r,F=Math.round((this.options.numLines-1-n)*this.options.ymax/(this.options.numLines-1)),this.r.text(q-this.options.marginLeft/2,K,F).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor).attr("text-anchor","end"),this.r.path("M"+q+","+K+"H"+(q+G)).attr("stroke",this.options.gridLineColor).attr("stroke-width",this.options.gridStrokeWidth);w=null,J=50;for(n=S=Math.ceil(this.xmin),T=Math.floor(this.xmax);S<=T?n<=T:n>=T;S<=T?n++:n--)o=this.r.text(B(n),this.options.marginTop+g+this.options.marginBottom/2,n).attr("font-size",this.options.gridTextSize).attr("fill",this.options.gridTextColor),p=o.getBBox(),w===null||w<=p.x?w=p.x+p.width+J:o.remove();c=function(){var a,b,c,d;c=this.xvals,d=[];for(a=0,b=c.length;a<b;a++)H=c[a],d.push(B(H));return d}.call(this),y=[],U=this.series;for(N=0,P=U.length;N<P;N++)x=U[N],y.push($.map(x,function(a,b){return{x:c[b],y:C(a)}}));for(n=V=y.length-1;V<=0?n<=0:n>=0;V<=0?n++:n--)d=y[n],d.length>1&&(s=this.createPath(d,this.options.marginTop,q,this.options.marginTop+g,q+G),this.r.path(s).attr("stroke",this.options.lineColors[n]).attr("stroke-width",this.options.lineWidth));z=function(){var a,b;b=[];for(n=0,a=y.length-1;0<=a?n<=a:n>=a;0<=a?n++:n--)b.push([]);return b}();for(n=W=y.length-1;W<=0?n<=0:n>=0;W<=0?n++:n--){X=y[n];for(O=0,Q=X.length;O<Q;O++)a=X[O],b=this.r.circle(a.x,a.y,this.options.pointSize).attr("fill",this.options.lineColors[n]).attr("stroke-width",1).attr("stroke","#ffffff"),z[n].push(b)}k=this.options.hoverFontSize*1.5*(this.series.length+1),j=this.r.rect(-10,-k/2-this.options.hoverPaddingY,20,k+this.options.hoverPaddingY*2,10).attr("fill",this.options.hoverFillColor).attr("stroke",this.options.hoverBorderColor).attr("stroke-width",this.options.hoverBorderWidth).attr("opacity",this.options.hoverOpacity),I=this.r.text(0,this.options.hov