// moochart 0.1b1 - http://moochart.coneri.se/
var Chart=new Class({Version:"0.1b1",Implements:Options});Chart.Bubble=new Class({Extends:Chart,options:{width:600,height:400,xmin:0,xmax:100,ymin:0,ymax:100,zmin:0,zmax:1,xsteps:5,ysteps:5,xlabel:null,ylabel:null,bubbleSize:30,lineColor:"#000"},initialize:function(A,I){this.setOptions(I);this.options.xsteps--;
this.options.xsteps=this.options.xsteps.limit(1,50),this.options.ysteps--;this.options.ysteps=this.options.ysteps.limit(1,50),this.container=$(A);this.container.setStyles({width:this.options.width,height:this.options.height,});this.canvas=new Element("canvas");this.canvas.adopt(new Element("div",{text:"Your browser does not support the canvas element, get a better one!",styles:{"text-align":"center","background-color":"#8b2e19",width:this.options.width,height:this.options.height,color:"#fff"}}));
this.canvas.width=this.options.width;this.canvas.height=this.options.height;this.container.adopt(this.canvas);if(!this.canvas.getContext){return false}this.overlay=new Element("div",{styles:{position:"relative",width:this.options.width,height:this.options.height,top:0-this.options.height-3,"margin-bottom":0-this.options.height-3,"font-family":"Helvetica, Arial, sans-serif","z-index":240}});
this.overlay.addEvent("mousemove",this.mouseHandler.bind(this));this.overlay.addEvent("mouseout",function(){this.tip.style.display="none";this.activeBubble=-1;this.redraw()}.bind(this));this.container.adopt(this.overlay);this.tip=new Element("div",{text:"",styles:{position:"absolute",display:"none",border:"2px solid #000","background-color":"#262626",padding:"0.5em","-webkit-border-radius":"3px","-moz-border-radius":"3px","white-space":"nowrap","z-index":250,color:"#fff","font-size":"11px","line-height":"1.3em","text-align":"left"}});
this.overlay.adopt(this.tip);this.ctx=this.canvas.getContext("2d");this.bubbles=new Array;this.activeBubble=-1;this.paddingTop=30;this.paddingLeft=40;this.paddingBottom=30;this.paddingRight=40;if(this.options.ylabel){this.paddingLeft+=30}if(this.options.xlabel){this.paddingBottom+=20}this.xwork=(this.options.width-(this.paddingLeft+this.paddingRight))-this.options.bubbleSize*2;
this.ywork=(this.options.height-(this.paddingTop+this.paddingBottom))-this.options.bubbleSize*2;this.xmax=this.options.xmax;this.xmin=this.options.xmin;this.ymax=this.options.ymax;this.ymin=this.options.ymin;this.zmax=this.options.zmax;this.zmin=this.options.zmin;this.xnumbers=new Array;this.ynumbers=new Array;
var G=this.xwork/this.options.xsteps;var E=this.ywork/this.options.ysteps;(this.options.xsteps+1).times(function(J){this.xnumbers.push(new Element("div",{text:"",styles:{position:"absolute","font-size":"10px","line-height":"20px",height:"20px",width:G+"px","text-align":"center",top:(this.options.height-this.paddingBottom+10)+"px",left:(this.paddingLeft+this.options.bubbleSize)-(G/2)+J*G+"px",color:this.options.lineColor}}))
}.bind(this));(this.options.ysteps+1).times(function(J){this.ynumbers.push(new Element("div",{text:"",styles:{position:"absolute","font-size":"10px","line-height":"20px",height:"20px","vertical-align":"middle",width:(this.paddingLeft-15)+"px","text-align":"right",top:(this.options.bubbleSize+(J*E)+this.paddingTop-10)+"px",left:"0px",color:this.options.lineColor}}))
}.bind(this));this.overlay.adopt(this.xnumbers);this.overlay.adopt(this.ynumbers);var F={position:"absolute","font-size":"10px","line-height":"20px",width:(this.xwork)+"px","text-align":"center",bottom:"0px","letter-spacing":"0.1em",left:(this.paddingLeft+this.options.bubbleSize)+"px",color:this.options.lineColor};
if(this.options.xlabel){this.xlabel=new Element("div",{text:this.options.xlabel,styles:F});this.overlay.adopt(this.xlabel,this.ylabel)}if(this.options.ylabel){var H="";var C=this.options.ylabel;for(var D=0;D<C.length;D++){H+="<br />"+C.charAt(D)}this.ylabel=new Element("div",{html:H,styles:F});this.ylabel.setStyles({width:"20px",height:1.1*(D+2)+"em",left:"0px",top:"0px","line-height":"1.1em"});
this.overlay.adopt(this.ylabel);var B=this.ylabel.getSize().y;this.ylabel.setStyle("top",(this.paddingTop+this.options.bubbleSize)+((this.ywork-B)/2))}this.drawLabels();this.updateNumbers();this.redraw()},drawLabels:function(){this.ctx.lineWidth=4;this.ctx.lineCap="round";this.ctx.strokeStyle=this.options.lineColor;
this.ctx.beginPath();this.ctx.moveTo(this.paddingLeft,this.paddingTop);this.ctx.lineTo(this.paddingLeft,this.options.height-this.paddingBottom);this.ctx.lineTo(this.options.width-this.paddingRight,this.options.height-this.paddingBottom);this.ctx.stroke();var A=this.xwork/this.options.xsteps;var B=this.ywork/this.options.ysteps;
this.ctx.beginPath();this.ctx.lineWidth=2;(this.options.xsteps+1).times(function(C){var D=this.paddingLeft+this.options.bubbleSize+A*C;this.ctx.moveTo(D,this.options.height-this.paddingBottom);this.ctx.lineTo(D,this.options.height-this.paddingBottom+10)}.bind(this));(this.options.ysteps+1).times(function(C){var D=this.options.height-(this.paddingBottom+this.options.bubbleSize+B*C);
this.ctx.moveTo(this.paddingLeft,D);this.ctx.lineTo(this.paddingLeft-10,D)}.bind(this));this.ctx.stroke()},addBubble:function(A,E,D,B,C){if($type(B)=="array"){B="rgb("+B.join(",")+")"}A=parseInt(A);E=parseInt(E);D=parseInt(D);C=C.replace(/%x/ig,A);C=C.replace(/%y/ig,E);C=C.replace(/%z/ig,D);this.bubbles.push({x:A,y:E,z:D,color:B,tip:C});
if(D>this.zmax){this.zmax=D}if(D<this.zmin){this.zmin=D}if(A>this.xmax){this.xmax=A}if(A<this.xmin){this.xmin=A}if(E>this.ymax){this.ymax=E}if(E<this.ymin){this.ymin=E}this.updateNumbers();this.bubbles.sort(function(G,F){return F.z-G.z})},updateNumbers:function(){var A=(this.xmax-this.xmin)/this.options.xsteps;
this.xnumbers.each(function(D,C){D.set("text",(A*C+this.xmin).round())}.bind(this));var B=(this.ymax-this.ymin)/this.options.ysteps;this.ynumbers.each(function(D,C){D.set("text",(this.ymax+this.ymin)-((B*C)+this.ymin).round())}.bind(this))},mouseHandler:function(G){var H=this.canvas.getCoordinates();
var J=G.page.x-H.left,I=G.page.y-H.top;var C=-1,D=this.bubbles.length;for(var F=D-1;F>=0;F--){var E=J-this.bubbles[F].realx,B=I-this.bubbles[F].realy,A=this.bubbles[F].realz+1;if((E*E)+(B*B)<=(A*A)){C=F;break}}if(this.activeBubble!=C){this.activeBubble=C;this.redraw();if(this.activeBubble>=0){this.tip.set("html",this.bubbles[this.activeBubble].tip);
this.tip.setStyle("display","block")}else{this.tip.setStyle("display","none")}}if(this.activeBubble>=0){this.tip.setStyle("left",J+10);this.tip.setStyle("top",I+15)}},redraw:function(){var B=this.bubbles.length;this.ctx.clearRect(this.paddingLeft+2,0,this.options.width,this.options.height-(this.paddingBottom+2));
this.ctx.lineWidth=1;for(var C=0;C<B;C++){var A=(((this.bubbles[C].x-this.xmin)/(this.xmax-this.xmin))*this.xwork).round()+this.paddingLeft+this.options.bubbleSize;var E=(this.ywork-(((this.bubbles[C].y-this.ymin)/(this.ymax-this.ymin))*this.ywork).round())+this.paddingTop+this.options.bubbleSize;var D=(((this.bubbles[C].z-this.zmin)/(this.zmax-this.zmin))*(this.options.bubbleSize-8)).round()+5;
this.ctx.beginPath();this.ctx.globalAlpha=1;this.ctx.fillStyle=this.bubbles[C].color;this.ctx.strokeStyle=this.bubbles[C].color;this.ctx.arc(A,E,D,0,Math.PI*2,true);this.ctx.stroke();if(this.activeBubble!=C){this.ctx.globalAlpha=0.6}this.ctx.fill();this.bubbles[C].realx=A;this.bubbles[C].realy=E;this.bubbles[C].realz=D
}},clear:function(){this.ctx.clearRect(0,0,this.options.width,this.options.width);this.drawLabels()},empty:function(){this.xmax=this.options.xmax;this.xmin=this.options.xmin;this.ymax=this.options.ymax;this.ymin=this.options.ymin;this.zmax=this.options.zmax;this.zmin=this.options.zmin;this.addBubble(this.xmax,this.ymax,this.zmax,[0,0,0],"");
delete this.bubbles;this.bubbles=new Array;this.redraw()}});