/* 
Usage:
var g = new line_graph();
g.add("title1",value);
g.add("title2",value2);

g.render("canvas_name", "graph title");

//where canvas_name is a div defined INSIDE body tag 
<body>
<div id="canvas_name" style="overflow: auto; position:relative;height:300px;width:400px;"></div>


*/

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 line_graph()
{
 this.ct = 0;
 
 this.data      = new Array();
 this.x_name    = new Array();
 this.max       = -64000; //MAX INT

 this.c_array = new Array();
 this.c_array[0] = new Array(51, 207, 39);
 this.c_array[1] = new Array(51, 207, 39);
 this.c_array[2] = new Array(51, 207, 39);
 this.c_array[3] = new Array(51, 207, 39);
 this.c_array[4] = new Array(51, 207, 39);
//  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)
  {
   this.x_name.push(x_name);
   this.data.push(parseInt(value,10));

   if(value > this.max)
      this.max = parseInt(value,10);
  }

 this.render = function(canvas, title)
  {
   var jg = new jsGraphics(canvas);

   var h  = 150;
   var sx = 50;
   var dw = 25;
   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("green");
   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));
     // 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);

       if(i >= 1)
         {
          jg.setColor(color);
          jg.drawLine(oldx, h-oldy, sx, h-ht1);
	 }

       jg.setColor("white");
       jg.fillEllipse(sx-0, h-ht1-0, 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.drawLine(0,Math.round((h/5*i)),rtmax+20,Math.round((h/5*i)));
   jg.setFont("Verdana", fnt,  Font.BOLD);
   jg.drawStringRect(title, 0, h+fnt+4, rtmax+20, "right");
   jg.paint();
  }

}
