var TT={Viz:{}};TT.Viz.GraphLib={};TT.Viz.GraphLib.FuelGauge=function(a){this.containerElement=a};TT.Viz.GraphLib.FuelGauge.prototype.draw=function(a,b){this.init(b);this.initBackground()};
TT.Viz.GraphLib.FuelGauge.prototype.init=function(a){a=a||{};a["background-color"]=a["background-color"]||"#fff";a.value=a.value||0;a.label=a.label||"";this.value=a.value;a.minValue=a.maxValue||0;a.maxValue=a.maxValue||100;a.minAngle=a.minAngle||60;a.maxAngle=a.maxAngle||300;a.animationSpeed=a.animationSpeed||1100;a.dialRadius=a.dialRadius||45;a.width=a.width||128;a.height=a.height||140;a.smallTickColor=a.smallTickColor||"#333";a.tickNumberColor=a.tickNumberColor||"#333";a.needleColor=a.needleColor||
"#c60";a.borderColor=a.borderColor||"#666";a.dialBackgroundColor=a.dialBackgroundColor||"#fff";a.dialBackgroundColorOuter=a.dialBackgroundColorOuter||"#f9f9f9";a.labelColor=a.labelColor||"#333";a.valueColor=a.valueColor||"#000";a.arcData=a.arcData||[];this.options=a;r=Raphael(this.containerElement,a.width,a.height);r.customAttributes.arc=function(b,f,c,d){d=d||a.dialRadius;d-=6;c=c||a.maxValue;cX=a.width/2;cY=a.height/2;var e=a.maxAngle-a.minAngle,g=a.minAngle-90,i=e/c*b+g;e=e/c*f+g;g=i*Math.PI/180;
var h=e*Math.PI/180;c=cX-d*Math.cos(g);g=cY-d*Math.sin(g);var j=cX-d*Math.cos(h);h=cY-d*Math.sin(h);newR=d;largeSweepFlag=1;sweepSize=e-i;if(sweepSize<180)largeSweepFlag=0;d=[["M",c,g],["A",newR,newR,0,largeSweepFlag,1,j,h]];if(b===f)d=[["M",c,g]];return{path:d}};this.r=r};
TT.Viz.GraphLib.FuelGauge.prototype.initBackground=function(){var a=this.r,b=this.options;a.path().attr({"stroke-width":b.dialRadius,stroke:b.dialBackgroundColorOuter}).attr({arc:[0,100,100,b.dialRadius*1]});a.path().attr({"stroke-width":b.dialRadius,stroke:b.dialBackgroundColor}).attr({arc:[0,100,100,b.dialRadius*(2/3)-b.dialRadius/10]});a.path().attr({"stroke-width":1,stroke:b.borderColor}).attr({arc:[-3,103,100,b.dialRadius*1.5]});this.drawArcs(b.arcData);this.drawNotches(25,b.dialRadius/4,true);
this.drawNotches(5,b.dialRadius/8);this.setValue(this.options.value,true);this.initNeedle();this.setValue(this.options.value);this.setLabel(this.options.label)};TT.Viz.GraphLib.FuelGauge.prototype.drawArcs=function(a){opts=this.options;r=this.r;this.arcs=r.set();arcWidth=opts.dialRadius*(2/3)-opts.dialRadius/10;for(var b=0;b<a.length;b++){arc=a[b];this.arcs.push(r.path().attr({"stroke-width":opts.dialRadius,stroke:arc.color}).attr({arc:[arc.start,arc.stop,opts.maxValue,arcWidth]}))}};
TT.Viz.GraphLib.FuelGauge.prototype.drawNotches=function(a,b,f){var c=this.options,d=c.minValue,e=c.maxValue,g=c.minAngle,i=c.maxAngle,h=c.dialRadius,j=c.width/2,k=c.height/2,n=c.smallTickColor;c=c.tickNumberColor;var l=Math.ceil(d/a)*a,o=Math.floor((Math.floor(e/a)*a-l)/a),p=a*(i-g)/(e-d),m=0,q=h/5,t=k+h+h/6,u=f?h/30:h/60;m=d>=0?d%a==0?0:(a-d%a)*(i-g)/(e-d):-d%a*(i-g)/(e-d);for(d=0;d<=o;d++){e=g+m+d*p;r.path("M"+j+" "+(k+h)+"L"+j+" "+(k+h-b)).attr({rotation:e+" "+j+" "+k,"stroke-width":u,stroke:n});
f&&r.text(j,t,l+d*a).scale(-1,-1).attr({"font-size":q,rotation:e+" "+j+" "+k,fill:c})}};
TT.Viz.GraphLib.FuelGauge.prototype.initNeedle=function(){var a=this.options.width/2,b=this.options.height/2,f=this.options.dialRadius,c=this.options.minAngle;r=this.r;this.s=r.set();this.s.push(r.path("M"+(a-f/9)+" "+(b-f/4)+" L"+a+" "+(b+f-f/9)+" L"+(a+f/9)+" "+(b-this.options.dialRadius/4)+" L "+a+" "+(b-this.options.dialRadius/3)+" Z ").attr({fill:this.options.needleColor,"stroke-width":"none",stroke:this.options.needleColor}));this.s.push(r.circle(a,b,f/9).attr({fill:"none","stroke-width":1,
stroke:"#999"}));this.s.push(r.circle(a,b,f/6).attr({fill:"180-#3cc-#fff",stroke:"#aaa"}));this.s.animate({rotation:c+" "+a+" "+b},0,"<>")};
TT.Viz.GraphLib.FuelGauge.prototype.updateValue=function(a){value=this.value;var b=this.options.width/2,f=this.options.height/2;if(!this.valueNode){var c=this.options.dialRadius*0.6,d=c*0.8,e=this.options.dialRadius,g=e/3;r.rect(b-c/2+1,f+e*0.9-d/2,c,d,e/6).attr({fill:"none",stroke:"#aaa"});this.valueNode=r.text(b,f+e*0.9,"").attr({"font-size":g,"font-family":"Arial","font-weight":"bold",fill:this.options.valueColor})}this.valueNode.attr({text:value?value:null===value?"":"00",fill:this.options.valueColor});
if(!a){s=this.s;if(null!==value){s[0].show();s.animate({rotation:(value-this.options.minValue)*(this.options.maxAngle-this.options.minAngle)/(this.options.maxValue-this.options.minValue)+this.options.minAngle+" "+b+" "+f},this.options.animationSpeed,"cubic-bezier(.8,1,.1,1.2)")}else s[0].hide()}};TT.Viz.GraphLib.FuelGauge.prototype.setValue=function(a,b){this.value=a;this.updateValue(b)};
TT.Viz.GraphLib.FuelGauge.prototype.setLabel=function(a){var b=this.options.width/2,f=this.options.height/2;this.labelNode=this.labelNode||r.text(b,this.options.dialRadius+f+this.options.dialRadius*0.2,"").attr({"font-weight":"bold","font-size":this.options.dialRadius/4,fill:this.options.labelColor});this.labelNode.attr({text:a})};
TT.Viz.GraphLib.FuelGauge.demo=function(a,b,f){a=document.getElementById(a);b=b||{};f=f||3E3;var c=document.createElement("div");c.className="fuelGauge";a.appendChild(c);a=new TT.Viz.GraphLib.FuelGauge(c);c={label:"",value:33,dialRadius:40,width:120,height:120,borderColor:"#666",arcData:[{start:22,stop:87,color:"#ff9"},{start:30,stop:55,color:"#9f9"}]};for(var d in c)if(c.hasOwnProperty(d))b.hasOwnProperty(d)||(b[d]=c[d]);a.draw({label:""},b);setInterval(function(e){return function(){var g=e.arcs[0].attr("arc"),
i=Math.round(e.options.minValue+Math.random()*(e.options.maxValue*0.6-e.options.minValue)),h=Math.round(i+Math.random()*(e.options.maxValue-i)),j=Math.round(i+Math.random()*(h-i));e.arcs[0].attr({arc:[i,h,g[2],g[3]]});i=Math.round(i+Math.random()*(h*0.6-i));h=Math.round(i+Math.random()*(h-i));e.setValue(j);e.arcs[1].attr({arc:[i,h,g[2],g[3]]})}}(a),f)};
