Code _php tutorial for implementing a line chart effect under PHP

Source: Internet
Author: User
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;//whether transparent 1 or 0
var $IMAGE;//Picture pair image
//-------------------
var $ARRAYSPLIT;//Specify a symbol to separate the values
var $ITEMARRAY;//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 the canvas background color
$background =imagecolorallocate ($this->image, $this->r, $this->g, $this->b);
if ($this->transparent== "1") {
Background transparent
Imagecolortransparent ($this->image, $background);
}else{
Fill the background color if you are not 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 ($item _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 interval
$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);
Cylindrical 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 interval
$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 interval
$xx = $xx + $this->border;
}
}

--------------Finish Printing Graphics http://knowsky.com
function printall () {
Imagegif ($this->image);
Imagedestroy ($this->image);
}
--------------Debugging
function Debug () {
echo "X:". $this->x. "
Y: ". $this->y;
echo "
BORDER: ". $this->border;
$item _array=split ($this->arraysplit, $this->itemarray);
$num =count ($item _array);
echo "
Number of values: ". $num."
Value: ";
for ($i =0; $i < $num; $i + +) {
echo "
". $item _array[$i];
}
}
}

$report =new Imagereport;
$report->setimage (600,300,255,255,255,1);//parameter (long, wide, back color r,g,b, whether 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";//values, separated by specified symbols
$report->setitem (', ', $temparray, 3,20);//parameters (delimited value of the specified symbol, numeric variable, style 1 for the vertical column Figure 2 for the horizontal column Figure 3 is a line chart, distance)
$report->setfont (1);//font size 1-10
$report->printreport ();
$report->debug ();//mode of Use
?>

http://www.bkjia.com/PHPjc/318343.html www.bkjia.com true http://www.bkjia.com/PHPjc/318343.html techarticle ? php classimagereport{var$x;//picture size X axis var$y;//picture size Y axis var$r;//back color R value var$g;//... G. var$b;//... B. var$transparent;//is transparent 1 or 0 var$image;//picture to like ...

  • 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.