﻿hD="0123456789ABCDEF";
function d2h(d) 
{
	var h = hD.substr(d&15,1);
	while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
	return h;
}

function h2d(h) 
{
	return parseInt(h,16);
}
//****************************************************************************************//
function graph()
{
 this.ct = 0;
 
 this.data      = new Array();
 this.x_name    = new Array();
 this.scale     = 1;
 this.max       = -64000; //MAX INT

 this.c_array = new Array();
 this.c_array[0] = new Array(255, 192, 95);
 this.c_array[1] = new Array(80, 127, 175);
 this.c_array[2] = new Array(159, 87, 112);
 this.c_array[3] = new Array(111, 120, 96);
 this.c_array[4] = new Array(224, 119, 96);
 this.c_array[5] = new Array(80, 159, 144);
 this.c_array[6] = new Array(207, 88, 95);
 this.c_array[7] = new Array(64, 135, 96);
 this.c_array[8] = new Array(239, 160, 95);
 this.c_array[9] = new Array(144, 151, 80);
 this.c_array[10] = new Array(255, 136, 80);

 this.getColor = function()
  {
   if(this.ct >= (this.c_array.length-1))
      this.ct = 0;
   else
      this.ct++;

   return "#" + d2h(this.c_array[this.ct][0]) + d2h(this.c_array[this.ct][1]) + d2h(this.c_array[this.ct][2]);
  }


 this.setScale = function(scale)
  {
   this.scale  = scale;
  }

 this.add = function(x_name, value)
  {
   value = parseFloat(value,10)*this.scale;

   this.x_name.push(x_name);  
   this.data.push(parseFloat(value,10));

   if(value > this.max)
      this.max = parseFloat(value,10);
  }

 this.render = function(canvas, title, height)
  {
   var jg = new jsGraphics(canvas);

   var h  = 250;

   if (typeof(height) == "number")
       h = height;

   var sx = 40;
   var dw = 15;
   var shadow = 3;
   var fnt    = 12;

   var rtmax = sx + 10 + (dw+Math.round((dw/2))+shadow)*(this.data.length);

   // Draw markers
   var i;
   for(i = 1 ; i <= 5 ; i++)
     {
      jg.drawLine(0,Math.round((h/5*i)),rtmax+20,Math.round((h/5*i)));
      var ff = Math.round(this.max - (this.max / 5 * i))/this.scale;
      jg.drawString(ff+"",4,Math.round((h/5*i)-2));
     }


   // Draw the bar graph
   for(i = 0; i < this.data.length; i++)
      {
       var ht1 = Math.round(this.data[i]*h/this.max);

       // fix for wierd IE bug that doesn't display h=0
       var ht2 = (ht1 > 0 ? ht1 : (ht1 == 0 ? 2 : ht1*-1));
       if (ht1 < 0)
           ht1 = 0;
       
       // shadow
       jg.setColor("gray");
       if(ht2-shadow > 1)
          jg.fillRect(sx+shadow,h-ht1+shadow,dw,ht2-shadow);

       jg.setColor(this.getColor());
       jg.fillRect(sx,h-ht1,dw,ht2);

       jg.setColor("black");
       jg.drawRect(sx,h-ht1,dw,ht2);

       jg.drawString(this.x_name[i], sx, h);

       sx = sx+dw+Math.round(dw/2)+shadow;
      }

   jg.setFont("Verdana", fnt,  Font.BOLD);
   jg.drawStringRect(title, 0, h+fnt+4, rtmax+20, "right");
   jg.paint();
  }

}
//***********************************************************************************//
function line_graph()
{
 this.ct = 0;
 
 this.data      = new Array();
 this.x_name    = new Array();
 this.scale     = 1;
 this.max       = -64000; //MAX INT
 this.user_min  = 64000; 
 this.min	= 0;

 this.c_array = new Array();
 this.c_array[0] = new Array(255, 192, 95);
 this.c_array[1] = new Array(80, 127, 175);
 this.c_array[2] = new Array(159, 87, 112);
 this.c_array[3] = new Array(111, 120, 96);
 this.c_array[4] = new Array(224, 119, 96);
 this.c_array[5] = new Array(80, 159, 144);
 this.c_array[6] = new Array(207, 88, 95);
 this.c_array[7] = new Array(64, 135, 96);
 this.c_array[8] = new Array(239, 160, 95);
 this.c_array[9] = new Array(144, 151, 80);
 this.c_array[10] = new Array(255, 136, 80);

 this.getColor = function()
  {
   if(this.ct >= (this.c_array.length-1))
      this.ct = 0;
   else
      this.ct++;

   return "#" + d2h(this.c_array[this.ct][0]) + d2h(this.c_array[this.ct][1]) + d2h(this.c_array[this.ct][2]);
  }


 this.setScale = function(scale)
  {
   this.scale  = scale;
  }

 this.setColor = function(value)
  {
   this.ct = value;
  }

 this.setMax = function(value)
  {
   this.max = value;
  }

 this.setMin = function(value)
  {
   this.user_min = (this.user_min < value ? this.user_min : value);
   this.min = this.user_min;
  }

 this.add = function(x_name, value)
  {
   value = parseFloat(value,10)*this.scale;

   this.x_name.push(x_name);  
   this.data.push(parseFloat(value,10));

   if(value > this.max)
      this.max = parseFloat(value,10);

   if(value < this.user_min)
      this.user_min = parseFloat(value,10);
  }

 this.render = function(canvas, title, height)
  {
   var jg = new jsGraphics(canvas);

   var h  = 250;

   if (typeof(height) == "number")
       h = height;

   var sx = 60;
   var dw = 15;
   var shadow = 0;
   var fnt    = 12;

   var rtmax = sx + 10 + (dw+Math.round((dw/2))+shadow)*(this.data.length);

   // Draw markers
   var i;
   jg.setColor("black");

   for(i = 1 ; i <= 5 ; i++)
     {
      jg.drawLine(0,Math.round((h/5*i)),rtmax+20,Math.round((h/5*i)));
      var ff = Math.round((this.max - (this.max / 5 * i))/(this.scale)+this.min);
      jg.drawString(ff+"",4,Math.round((h/5*i)-2));
     }

   // Draw the bar graph
   var color = this.getColor();
   var oldx, oldy;
   jg.setStroke(1);

   for(i = 0; i < this.data.length; i++)
      {
       var ht1 = Math.round(this.data[i]*h/this.max) - (this.min*h/this.max);

       if(i >= 1)
         {
          jg.setColor(color);
          jg.drawLine(oldx, h-oldy, sx, h-ht1);
	 }

       jg.setColor("red");
       jg.fillEllipse(sx-2, h-ht1-2, 5, 5);

       jg.setColor("black");
       jg.drawString(this.x_name[i], sx, h);

       oldx = sx;
       oldy = ht1;

       sx = sx+dw+Math.round(dw/2)+shadow;
      }

   jg.setFont("Verdana", fnt,  Font.BOLD);
   jg.drawStringRect(title, 0, h+fnt+4, rtmax+20, "right");
   jg.paint();
  }

}
//****************************************************************************************//
function pie()
{
	this.ct = 0;

	this.data      	= new Array();
	this.x_name    	= new Array();
	this.div_id		= new Array();
	this.max       	= 0;

	this.c_array = new Array();
	this.c_array[0] = new Array(255, 192, 95);
	this.c_array[1] = new Array(80, 127, 175);
	this.c_array[2] = new Array(159, 87, 112);
	this.c_array[3] = new Array(111, 120, 96);
	this.c_array[4] = new Array(224, 119, 96);
	this.c_array[5] = new Array(80, 159, 144);
	this.c_array[6] = new Array(207, 88, 95);
	this.c_array[7] = new Array(64, 135, 96);
	this.c_array[8] = new Array(239, 160, 95);
	this.c_array[9] = new Array(144, 151, 80);
	this.c_array[10] = new Array(255, 136, 80);

	this.getColor = function()
	{
		if(this.ct >= (this.c_array.length-1))
			this.ct = 0;
		else
			this.ct++;
		
		return "#" + d2h(this.c_array[this.ct][0]) + d2h(this.c_array[this.ct][1]) + d2h(this.c_array[this.ct][2]);
	}


	this.add = function(x_name, value, divId)
	{
		this.x_name.push(x_name);  
		this.data.push(parseInt(value,10));
		divId?this.div_id.push(divId):null;
		this.max += parseInt(value,10);
	}

	this.render = function(canvas, title, size)
	{
		var jg,cnv,r,sW,cH,sx,xy,st_angle,hyp,lblFnt,graphFnt;
		jg = new jsGraphics(canvas);
		cnv = document.getElementById(canvas);	//location of Graph
		cW = (cnv.style.width).substring(0,(cnv.style.width).lastIndexOf('px'));	//width of chart canvas
		cH = (cnv.style.height).substring(0,(cnv.style.height).lastIndexOf('px'));	//height of chart canvas
		size?size=size:size=cW*.5; //if no size is provided, default to 50% of the canvas width.
		r = size/2;
		sx = cW/2 - r;	//start X
		sy = cH/2 - r;	//start Y
		st_angle = 0;	//starting position for piechart graph
		hyp = r*1.25;	//the length of the ray used to position the text (125% of the radius)
		lblFnt = 10;
		graphFnt = 14;
		
		// pie shadow
		jg.setColor("gray");
		jg.fillEllipse(sx+3,sy+3,2*r,2*r);
		   
		for(i = 0; i<this.data.length; i++)
		{
			var angle = Math.round(this.data[i]/this.max*360);
			var pc    = Math.round(this.data[i]/this.max*100);
			jg.setColor(this.getColor());
			jg.fillArc(sx,sy,2*r,2*r,st_angle,st_angle+angle);
			var ang_rads = (st_angle+(angle/2)) * (Math.PI/180);
			var my  = Math.sin(ang_rads) * hyp;
			var mx  = Math.cos(ang_rads) * hyp;
			st_angle += angle;
			mxa = (mx < 0 ? 50 : 0);
			jg.setColor("black");
			jg.setFont("verdana, arial, sans-serif",lblFnt+"px",Font.PLAIN);
			jg.drawString(this.x_name[i]+"("+pc+"%"+")",cW/2+mx-mxa,cH/2-my, this.div_id[i]);
		}
		jg.setFont("verdana, arial, sans-serif", graphFnt+"px",Font.BOLD);
		jg.drawStringRect(title, 0, 0, cW, 'center');
		jg.setColor("black");
		jg.drawEllipse(sx, sy, 2*r, 2*r);
		jg.paint();
	}
}
//***********************************************************************************//
function flowchart()
{
 this.ct = 0;
 
 this.data      = new Array();
 this.x_name    = new Array();
 this.scale     = 1;
 this.max       = -64000; //MAX INT
 this.user_min  = 64000; 
 this.min	= 0;

 this.c_array = new Array();
 this.c_array[0] = new Array(255, 192, 95);
 this.c_array[1] = new Array(80, 127, 175);
 this.c_array[2] = new Array(159, 87, 112);
 this.c_array[3] = new Array(111, 120, 96);
 this.c_array[4] = new Array(224, 119, 96);
 this.c_array[5] = new Array(80, 159, 144);
 this.c_array[6] = new Array(207, 88, 95);
 this.c_array[7] = new Array(64, 135, 96);
 this.c_array[8] = new Array(239, 160, 95);
 this.c_array[9] = new Array(144, 151, 80);
 this.c_array[10] = new Array(255, 136, 80);

 this.getColor = function()
  {
   if(this.ct >= (this.c_array.length-1))
      this.ct = 0;
   else
      this.ct++;

   return "#" + d2h(this.c_array[this.ct][0]) + d2h(this.c_array[this.ct][1]) + d2h(this.c_array[this.ct][2]);
  }


 this.setScale = function(scale)
  {
   this.scale  = scale;
  }

 this.setColor = function(value)
  {
   this.ct = value;
  }

 this.setMax = function(value)
  {
   this.max = value;
  }

 this.setMin = function(value)
  {
   this.user_min = (this.user_min < value ? this.user_min : value);
   this.min = this.user_min;
  }

 this.add = function(x_name, value)
  {
   value = parseFloat(value,10)*this.scale;

   this.x_name.push(x_name);  
   this.data.push(parseFloat(value,10));

   if(value > this.max)
      this.max = parseFloat(value,10);

   if(value < this.user_min)
      this.user_min = parseFloat(value,10);
  }

 this.render = function(canvas, title, height)
  {
   var jg = new jsGraphics(canvas);

   var h  = 250;

   jg.setColor("black");

   // Draw the bar graph
   jg.setStroke(1);

   for(i = 0; i < this.data.length; i++)
      {
        jg.drawStringRect(this.x_name[i],100*i,0,100,100);
      }
   jg.setFont("Verdana", fnt,  Font.BOLD);
   jg.drawStringRect(title, 0, h+fnt+4, rtmax+20, "right");
   jg.paint();
  }

}
