| px | top | add code | search | signup | login | help |
<?
/*this class contains the function for printing the line graph*/
class LineGraph
{
var $im; //is the image identifier
var $dst_im; //is the destination image identifier
var $width; //width of the destination image
var $height; //height of the destination image
var $font_ht; //height of the font
var $font_wd; //width of the font
var $ft_ht_half; //half the height of the font
var $ft_wd_half; //half the width of the font
var $ratio_x; //position on x axis where the ratio starts
var $ratio_y; //position on y axis where the ratio starts
var $ratio_label; //the ratio\'s label that has be displayed
var $w; //width of the image
var $h; //height of the image
var $upper_rnd; //upper range(value) of the graph
var $lower_rnd; //lower range(value) of the graph
var $dst_x; //position on the x axis of the destination graph
var $dst_y; //position on the y axis of the destination graph
var $dst_w; //width of the destination image
var $dst_h; //height of the destination image
var $src_x; //position on the x axis of the source graph
var $src_y; //position on the y axis of the source graph
var $src_w; //width of the source image
var $src_h; //height og the source image
var $origin_x; //x co-ordinate at the origin
var $origin_y; //y co-ordinate at the origin
var $x_axis_x; //end x co-ordinate on the x axis
var $x_axis_y; //end y co-ordinate on the x axis
var $y_axis_x; //end x co-ordinate on the y axis
var $y_axis_y; //end y co-ordinate on the y axis
var $close_val_diff; //difference between closeprice and lowerrange
var $close_freq_diff; //difference between closeprice and lowerrange after mapping
var $y_value_diff; //difference between upperrange and lowerrange
var $y_axis_diff; //difference between graph\'s upper and lower y co-ordinates
var $map_factor; //mapping factor
var $y_label_pos; //position of the labels on the y axis after mapping
var $y_pos; //the exact position of the labels on the y axis
var $dash_x; //x position of the dash on the y axis
var $x; //x position of ths label on the y axis
var $y_interval_val; //no. of labels present on the y axis
var $y_value; //label to be displayed olong the y axis
var $x_step; //the interval by which x co-ordinate is to incremented
var $y_step; //the interval by which y co-ordinate is to incremented
var $x_freq; //x co-ordinate for plotting the line
var $y_freq; //y co-ordinate for plotting the line
var $x_frq; //x co-ordinate for plotting close line
var $y_frq; //y co-ordinate for plotting close line
var $horzlinecolor; //color of the horizontal lines of the graph
var $closelinecolor; //color of the closing line
var $graphBGColor; //background color of the graph
var $graphXaxiscolor; //color of the x axis
var $graphYaxiscolor; //color of the y axis
var $graphlinecolor; //color of the line of the graph
var $graphResizeColor; //background color of the graph after resize(destination graph)
var $labeltextcolor; //color of the labels in the graph
var $SizeData; //no. of data points that are to be plotted on x axis
var $XAxisValue=0; //x value after x interval increment for plotting line
var $XAxisValueClose=0; //x value after x interval increment for plotting close line
var $max_length=0; //maximum length of the labels
var $length; //length of the labels to be displayed on y axis
/*The LineGraph() function for printing the line graph. This functions calls the different functions which create the various components of the line graph.*/
function LineGraph($wd,$ht,$x_dst,$y_dst,$font,$dash,$upperrange,$lowerrange,$y_interval,$x_interval,$colors,$datas,$horaStart,$horaEnd,$closeprice,$desc,$origin_start)
{
$this->w=$this->roundoff($wd); //to round off the width to integer value
$this->h=$this->roundoff($ht); //to round off the height to interger value
$this->upper_rnd=$upperrange; //upper range of the graph
$this->lower_rnd=$lowerrange; //lower range of the graph
$this->im = ImageCreate($this->w,$this->h); //create the source image
$this->SetBGColor($colors); //set the background color for image
ImageColorTransparent($this->im,$this->graphBGColor); //make image transparent
$this->SetAxisCoOrdinates($x_dst,$y_dst); //assign the co-ordinates of the graph
$this->FontSize($font); //set font height and width
$this->ResizeImage($colors,$x_dst,$y_dst); //resize the source image
$this->SetXaxisColor($colors); //set color of X axis
$this->SetYaxisColor($colors); //set color of Y axis
$this->SetLineColor($colors); //set color of the line graph
$this->SetCloseLineColor($colors); //set color of the closing line
$this->SetLabelColor($colors); //set color of the labels
$this->PrintX_axis(); //print X axis
$this->PrintY_axis(); //print Y axis
$this->PrintX_axis2(); //print axis parallel to X axis at other end
$this->PrintY_axis2(); //print axis parallel to Y axis at other end
$this->YFactorMap(); //calculate mapping factor for datas
if($this->map_factor!=0)
{
if($x_interval!=0)
{
$this->XAxisStep($datas,$x_interval); //calculate the x interval(step)
$this->PrintLineGraph($datas,$desc, origin_start); //print the line graph
if(isset($closeprice))
{
if($datas!=0)
$this->PrintCloseLine($closeprice,$x_interval); //print close line
}
}
$this->PrintLabel($font,$y_interval,$dash,$desc); //display the labels
$this->PrintYSteps(); //display the intermediate lines parallel to X axis
if(isset($horaStart) or isset($horaEnd))
$this->PrintRatio($font,$horaStart,$horaEnd); //display label ratio
}
} //end of function PrintGraph()
function SetBGColor($colors)
{
$red = $colors[\"bgcolor\"][\"red\"];
$green = $colors[\"bgcolor\"][\"green\"];
$blue = $colors[\"bgcolor\"][\"blue\"];
$this->graphBGColor=ImageColorAllocate($this->im,$red,$green,$blue);
} //end of function SetBGColor()
function SetResizeColor($colors)
{
$red = $colors[\"resizecolor\"][\"red\"];
$green = $colors[\"resizecolor\"][\"green\"];
$blue = $colors[\"resizecolor\"][\"blue\"];
$this->graphResizeColor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetBGColor()
function SetXaxisColor($colors)
{
$red = $colors[\"Xaxiscolor\"][\"red\"];
$green = $colors[\"Xaxiscolor\"][\"green\"];
$blue = $colors[\"Xaxiscolor\"][\"blue\"];
$this->graphXaxiscolor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetXaxisColor()
function SetYaxisColor($colors)
{
$red = $colors[\"Yaxiscolor\"][\"red\"];
$green = $colors[\"Yaxiscolor\"][\"green\"];
$blue = $colors[\"Yaxiscolor\"][\"blue\"];
$this->graphYaxiscolor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetYaxisColor()
function SetLineColor($colors)
{
$red = $colors[\"linecolor\"][\"red\"];
$green = $colors[\"linecolor\"][\"green\"];
$blue = $colors[\"linecolor\"][\"blue\"];
$this->graphlinecolor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetLineColor()
function SetCloseLineColor($colors)
{
$red = $colors[\"closecolor\"][\"red\"];
$green = $colors[\"closecolor\"][\"green\"];
$blue = $colors[\"closecolor\"][\"blue\"];
$this->closelinecolor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetLineColor()
function SetLabelColor($colors)
{
$red = $colors[\"labelcolor\"][\"red\"];
$green = $colors[\"labelcolor\"][\"green\"];
$blue = $colors[\"labelcolor\"][\"blue\"];
$this->labeltextcolor=ImageColorAllocate($this->dst_im,$red,$green,$blue);
} //end of function SetLineColor()
function SetAxisCoOrdinates($x_dst,$y_dst)
{
$this->dst_x=$x_dst;
$this->dst_y=$y_dst;
$this->dst_w=$this->w;
$this->dst_h=$this->h;
$this->src_x=0;
$this->src_y=0;
$this->src_w=$this->w;
$this->src_h=$this->h;
$this->origin_x=$x_dst;
$this->origin_y=$y_dst+$this->h;
$this->x_axis_x=$x_dst+$this->w;
$this->x_axis_y=$y_dst+$this->h;
$this->y_axis_x=$x_dst;
$this->y_axis_y=$y_dst;
} //end of function SetAxisCoOrdinates()
function FontSize($font)
{
$this->font_wd=imagefontwidth($font);
$this->ft_wd_half=$this->roundoff($this->font_wd/2);
$this->font_ht=imagefontheight($font);
$this->ft_ht_half=$this->roundoff($this->font_ht/2);
} //end of function FontSize()
function ResizeImage($colors,$x_dst,$y_dst)
{
$this->width=$x_dst + $this->w + $this->ft_wd_half;
$this->height=$y_dst + $this->h + 2 * $this->font_ht;
$this->dst_im=ImageCreate($this->width,$this->height);
$this->SetResizeColor($colors);
ImageColorTransparent($this->dst_im,$this->graphResizeColor);
ImageCopyResized($this->dst_im,$this->im,$this->dst_x,$this->dst_y,$this->src_x,$this->src_y,$this->dst_w,$this->dst_h,$this->src_w,$this->src_h);
} //end of function ResizeImage()
function PrintX_axis()
{
imageline($this->dst_im,$this->origin_x,$this->origin_y,$this->x_axis_x,$this->x_axis_y,$this->graphXaxiscolor);
} //end of function PrintX_axis()
function PrintY_axis()
{
imageline($this->dst_im,$this->origin_x,$this->origin_y,$this->y_axis_x,$this->y_axis_y,$this->graphYaxiscolor);
} //end of function PrintY_axis()
function PrintX_axis2()
{
imageline($this->dst_im,$this->y_axis_x,$this->y_axis_y,$this->x_axis_x,$this->y_axis_y,$this->graphXaxiscolor);
} //end of function PrintX_axis()
function PrintY_axis2()
{
imageline($this->dst_im,$this->x_axis_x,$this->x_axis_y,$this->x_axis_x,$this->y_axis_y,$this->graphYaxiscolor);
} //end of function PrintY_axis()
function XAxisStep($datas,$x_interval)
{
$this->SizeData=sizeof($datas);
$this->x_step=$this->w/$x_interval;
} //end of function XAxisStep()
function YFactorMap()
{
$this->y_axis_diff=$this->origin_y - $this->y_axis_y;
$this->y_value_diff=$this->upper_rnd - $this->lower_rnd;
$this->map_factor=$this->y_value_diff / $this->y_axis_diff;
} //end of function YFactorMap()
function MapYValue($value,$sequence,$size,$lower_val,$lower_freq,$map)
{
$j=0;
if($sequence==1)
{
for($i=($size-1);$i>=0;$i--)
{
$val_diff[$j]=$value[$i] - $lower_val;
$freq_diff[$j]=$val_diff[$j] / $map;
$freq[$j]=$lower_freq - $freq_diff[$j];
$j++;
}
}
else
{
for($i=0;$i<$size;$i++)
{
$val_diff[$j]=$value[$i] - $lower_val;
$freq_diff[$j]=$val_diff[$j] / $map;
$freq[$j]=$lower_freq - $freq_diff[$j];
$j++;
}
}
return $freq;
} //end of function MapYValue()
function PrintLineGraph($datas,$desc,$origin_start)
{
$j=0;
$this->y_freq=$this->MapYValue($datas,$desc,$this->SizeData,$this->lower_rnd,$this->origin_y,$this->map_factor);
for($i=0;$i<$this->SizeData;$i++)
{
$this->XAxisValue += $this->x_step;
$this->x_freq[$j]=$this->XAxisValue + $this->origin_x;
if($i==0)
{
if($origin_start==1)
$initial_y = $this->origin_y;
else
$initial_y = $this->y_freq[$j];
imageline($this->dst_im,$this->origin_x,$initial_y,$this->x_freq[$j],$this->y_freq[$j],$this->graphlinecolor);
}
else
{
imageline($this->dst_im,$this->x_freq[$j-1],$this->y_freq[$j-1],$this->x_freq[$j],$this->y_freq[$j],$this->graphlinecolor);
}
$j++;
}
} //end of function PrintLineGraph()
function PrintCloseLine($closeprice,$x_interval)
{
$j=0;
$this->close_val_diff = $closeprice - $this->lower_rnd;
$this->close_freq_diff = $this->close_val_diff / $this->map_factor;
for($i=0;$i<$x_interval;$i++)
{
$this->XAxisValueClose += $this->x_step;
$this->x_frq[$j]=$this->XAxisValueClose + $this->origin_x;
$this->y_frq[$j]=$this->origin_y - $this->close_freq_diff;
if($i==0)
{
imageline($this->dst_im,$this->origin_x,$this->y_frq[$j],$this->x_frq[$j],$this->y_frq[$j],$this->closelinecolor);
}
else
{
imageline($this->dst_im,$this->x_frq[$j-1],$this->y_frq[$j-1],$this->x_frq[$j],$this->y_frq[$j],$this->closelinecolor);
}
$j++;
}
} //end of function PrintCloseLine()
function DashPosition()
{
$this->dash_x=$this->origin_x-$this->font_wd;
} //end of function DashPosition()
function NumberLength($y_interval)
{
for($i=0;$i<=$y_interval;$i++)
{
$this->length=strlen(number_format($this->y_value[$i],2,\".\",\"\"));
if($this->length > $this->max_length)
$this->max_length=$this->length;
}
} //end of NumberLength()
function LabelPosition($y_interval)
{
$this->NumberLength($y_interval);
$this->x=$this->origin_x-($this->max_length+1)*$this->font_wd;
} //end of function LabelPosition()
function YLabelPos($desc)
{
if($desc==1)
$seq_label = 0;
$this->y_label_pos=$this->MapYValue($this->y_value,$seq_label,$this->y_interval_val,$this->lower_rnd,$this->origin_y,$this->map_factor);
for($i=0;$i<$this->y_interval_val;$i++)
{
$this->y_pos[$i]=$this->y_label_pos[$i] - $this->ft_ht_half;
}
} //end of function YLabelPos()
function PrintLabel($font,$y_interval,$dash,$desc)
{
$this->y_interval_val=$y_interval+1;
$this->y_step=$this->y_value_diff/$y_interval;
for($i=0;$i<$this->y_interval_val;$i++)
{
if($i==0)
$this->y_value[$i]=$this->lower_rnd;
elseif($i==($y_interval))
$this->y_value[$i]=$this->upper_rnd;
else
$this->y_value[$i]=$this->y_value[$i-1]+$this->y_step;
}
$this->DashPosition();
$this->LabelPosition($y_interval);
$this->YLabelPos($desc);
for($i=0;$i<$this->y_interval_val;$i++)
{
imagestring($this->dst_im,$font,$this->dash_x,$this->y_pos[$i],$dash,$this->labeltextcolor);
imagestring($this->dst_im,$font,$this->x,$this->y_pos[$i],number_format($this->y_value[$i],2,\".\",\"\"),$this->labeltextcolor);
}
} //end of function PrintLabel()
function PrintYSteps()
{
for($i=1;$i<($this->y_interval_val-1);$i++)
{
imageline($this->dst_im,$this->y_axis_x,$this->y_label_pos[$i],$this->x_axis_x,$this->y_label_pos[$i],$this->graphXaxiscolor);
}
} //end of function PrintYSteps()
function PrintRatio($font,$horaStart,$horaEnd)
{
$this->ratio_x = $this->origin_x + $this->w * (20/100);
$this->ratio_y = $this->origin_y + $this->ft_ht_half;
$this->ratio_label = $horaStart.\"/\".$horaEnd;
imagestring($this->dst_im,$font,$this->ratio_x,$this->ratio_y,$this->ratio_label,$this->labeltextcolor);
} //end of function PrintRatio()
function roundoff($v)
{
if ($v - floor($v) >= 0.5)
{
return(ceil($v));
}
else
{
return(floor($v));
}
} //end of function roundoff()
function DisplayLineGraph()
{
header(\"Content-Type: image/gif\");
ImagePNG($this->dst_im);
} //end of function DisplayLineGraph()
} //end of class LineGraph
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.