A class for generating the line chart of the histogram with the GD Gallery

Source: Internet
Author: User
Tags count numeric numeric value rand split
Line Chart | Column chart

Recently wrote a GD library to generate the horizontal column and line chart of the class library, is a teaching routine bar
?
Class imagereport{
var $X;//Picture size X axis
var $Y;//Picture size Y axis
var $R//Background color R value
var $G//... G.
var $B//... B.
var $TRANSPARENT//transparent 1 or 0
var $IMAGE//Picture to Like
//-------------------
var $ARRAYSPLIT;//specify symbols for separating values
var $ITEMARRAY;//numeric value
var $REPORTTYPE//Chart type, 1 for vertical column 2 for horizontal column 3 for fold line
var $BORDER;//Distance
//-------------------
var $FONTSIZE;//font size
var $FONTCOLOR;//Font Color
--------parameter setting function
function SetImage ($SizeX, $SizeY, $R, $G, $B, $Transparent) {
$this->x= $SizeX;
$this->y= $SizeY;
$this->r= $R;
$this->g= $G;
$this->b= $B;
$this->transparent= $Transparent;
}
function SetItem ($ArraySplit, $ItemArray, $ReportType, $Border) {
$this->arraysplit= $ArraySplit;
$this->itemarray= $ItemArray;
$this->reporttype= $ReportType;
$this->border= $Border;
}
function SetFont ($FontSize) {
$this->fontsize= $FontSize;
}
----------------Body
function PrintReport () {
Header ("Content-type:image/gif");
Build Canvas Size
$this->image=imagecreate ($this->x, $this->y);
Set Canvas background color
$background =imagecolorallocate ($this->image, $this->r, $this->g, $this->b);
if ($this->transparent== "1") {
Figure Transparent
Imagecolortransparent ($this->image, $background);
}else{
Fill the background color if you don't want to be transparent
Imagefilledrectangle ($this->image,0,0, $this->x, $this->y, $background);
}
parameter font text small and color
$this->fontcolor=imagecolorallocate ($this->image,255-$this->r,255-$this->g,255-$this->b);
Switch ($this->reporttype) {
Case "0":
Break
Case "1":
$this->imagecolumns ();
Break
Case "2":
$this->IMAGECOLUMNH ();
Break
Case "3":
$this->imageline ();
Break
}
$this->printxy ();
$this->printall ();
}
-----------Print an XY axis
function Printxy () {
Draw XY Axis * *
$color =imagecolorallocate ($this->image,255-$this->r,255-$this->g,255-$this->b);
$xx = $this->x/10;
$yy = $this->y-$this->y/10;
Imageline ($this->image, $this->border, $this->border, $this->border, $this->y-$this->border,$ color);//x axis
Imageline ($this->image, $this->border, $this->y-$this->border, $this->x-$this->border, $this- >y-$this->border, $color);//y axis
Scale on y-axis
$rulerY = $this->y-$this->border;
while ($rulerY > $this->border*2) {
$rulerY = $rulerY-$this->border;
Imageline ($this->image, $this->border, $rulerY, $this->border-2, $rulerY, $color);
}
Scale on X axis
$rulerX = $rulerX + $this->border;
while ($rulerX < ($this->x-$this->border*2)) {
$rulerX = $rulerX + $this->border;
Imageline ($this->image, $this->border,10, $this->border+10,10, $color);
Imageline ($this->image, $rulerX, $this->y-$this->border, $rulerX, $this->y-$this->border+2, $color);
}
}

--------------Vertical Column Chart
function Imagecolumns () {
$item _array=split ($this->arraysplit, $this->itemarray);
$num =count ($item _array);
$item _max=0;
for ($i =0; $i < $num; $i + +) {
$item _max=max ($it

Em_max, $item _array[$i]);
}
$xx = $this->border*2;
Draw a Column chart
for ($i =0; $i < $num; $i + +) {
Srand (Double) microtime () *1000000);
if ($this->r!=255 && $this->g!=255 && $this->b!=255) {
$R =rand ($this->r,200);
$G =rand ($this->g,200);
$B =rand ($this->b,200);
}else{
$R =rand (50,200);
$G =rand (50,200);
$B =rand (50,200);
}
$color =imagecolorallocate ($this->image, $R, $G, $B);
Column height
$height = ($this->y-$this->border)-($this->y-$this->border*2) * ($item _array[$i]/$item _max);
Imagefilledrectangle ($this->image, $xx, $height, $xx + $this->border, $this->y-$this->border, $color);
Imagestring ($this->image, $this->fontsize, $xx, $height-$this->border, $item _array[$i], $this-> FontColor);
for intervals
$xx = $xx + $this->border*2;
}
}

-----------Horizontal Column Chart
function IMAGECOLUMNH () {
$item _array=split ($this->arraysplit, $this->itemarray);
$num =count ($item _array);
$item _max=0;
for ($i =0; $i < $num; $i + +) {
$item _max=max ($item _max, $item _array[$i]);
}
$yy = $this->y-$this->border*2;
Draw a Column chart
for ($i =0; $i < $num; $i + +) {
Srand (Double) microtime () *1000000);
if ($this->r!=255 && $this->g!=255 && $this->b!=255) {
$R =rand ($this->r,200);
$G =rand ($this->g,200);
$B =rand ($this->b,200);
}else{
$R =rand (50,200);
$G =rand (50,200);
$B =rand (50,200);
}
$color =imagecolorallocate ($this->image, $R, $G, $B);
Column length
$leight = ($this->x-$this->border*2) * ($item _array[$i]/$item _max);
Imagefilledrectangle ($this->image, $this->border, $yy-$this->border, $leight, $yy, $color);
Imagestring ($this->image, $this->fontsize, $leight +2, $yy-$this->border, $item _array[$i], $this-> FontColor);
for intervals
$yy = $yy-$this->border*2;
}
}

--------------Line Chart
function Imageline () {
$item _array=split ($this->arraysplit, $this->itemarray);
$num =count ($item _array);
$item _max=0;
for ($i =0; $i < $num; $i + +) {
$item _max=max ($item _max, $item _array[$i]);
}
$xx = $this->border;
Draw a Column chart
for ($i =0; $i < $num; $i + +) {
Srand (Double) microtime () *1000000);
if ($this->r!=255 && $this->g!=255 && $this->b!=255) {
$R =rand ($this->r,200);
$G =rand ($this->g,200);
$B =rand ($this->b,200);
}else{
$R =rand (50,200);
$G =rand (50,200);
$B =rand (50,200);
}
$color =imagecolorallocate ($this->image, $R, $G, $B);
Column height
$height _now= ($this->y-$this->border)-($this->y-$this->border*2) * ($item _array[$i]/$item _max);
if ($i!= "0") {
Imageline ($this->image, $xx, $height _next, $xx + $this->border, $height _now, $color);
}
Imagestring ($this->image, $this->fontsize, $xx + $this->border, $height _now-$this->border/2, $item _array [$i], $this->fontcolor);
$height _next= $height _now;
for intervals
$xx = $xx + $this->border;
}
}

--------------Finish Printing Graphics yemaosheng.com
function printall () {
Imagegif ($this->image);
Imagedestroy ($this->image); &nbs

P
}
--------------Debugging
function Debug () {
echo "X:". $this->x. " <br>y: ". $this->y;
echo "<br>border:". $this->border;
$item _array=split ($this->arraysplit, $this->itemarray);
$num =count ($item _array);
echo "<br> number:". $num. " <br> value: ";
for ($i =0; $i < $num; $i + +) {
echo "<br>" $item _array[$i];
}
}
}

$report =new Imagereport;
$report->setimage (600,300,255,255,255,1);//parameters (long, wide, back color r,g,b, transparent 1 or 0)
$temparray = "50,25,100,250,180,200,150,220,200,150,50,25,100,250,180,200,150,220,200,150";//numeric value, separated by the specified symbol
$report->setitem (', ', $temparray, 3,20);//argument (delimited value of the specified symbol, numeric variable, style 1 is vertical column Figure 2 for the horizontal column chart 3 for the line chart, distance)
$report->setfont (1);//font size 1-10
$report->printreport ();
$report->debug ()//mode
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.