PHP Lightweight Chart class PHP light Chart:plchart

Source: Internet
Author: User
Tags save file set background
The class can generate a pie chart (2d/3d), a histogram (2d/3d), and a line chart that controls output to the browser or file, controlling the quality of the output JPEG image.
All settings work with only 5 functions:
1. Chart background color settings setbgcolor ()
2. Chart parameter Settings Setchartdata ()
2. Chart Title setting Settitle ()
3. Chart Comment Settings Setdesc ()
4. Chart graphic Settings setgraphics ()

For detailed setup parameters, refer to the documentation comments or the following test page demo.php

  Array (192, 192, 192), "darkgray" = = Array (144, 144, 144), "lightred" = = Array (255, 0, 0), "darkred" = = Array (128 , 0, 0), "lightgreen" = = Array (0, 255, 0), "darkgreen" = = Array (0,, 0), "lightblue" = = Array (0, 0, 255), "dark Blue "= = Array (0, 0, +)," lightyellow "= = Array (255, 255, 0)," darkyellow "= = Array ($, (), +)," Lightcyan "= > Array (0, 255, 255), "darkcyan" = = Array (0, 90, 90),); ============================ Set function-setting functions ============================/* * @todo Set image background Color-set the Backgro Undcolor of the chart image * * @param int $red * @param int $green * @param int $blue * */function setbgcolor ($red = 0, $  Green = 0, $blue = 0) {$this->bgcolor[0] = $red;  $this->bgcolor[1] = $green; $this->bgcolor[2] = $blue; }/* * @todo Set Image Parameters-set the parameters of the chart image * * @param string $type * @param int $d 3d * @param int $width * @param int $height * @param int $quality * @param array $data * @paramString $savefile * */function setchartdata ($type = "pie", $d 3d = 1, $width = +, $height = +, $quality =, $data = AR     Ray (), $savefile = ') {$this->type = $type;     Chart types-type of the Chart:pie, column, line $this->d3d = $d 3d;     3D effect-$this->width = $width;     Chart width $this->height = $height;     Chart height $this->quality = $quality;     The chart shows the quality $this->param = $data;     Source data array $this->savefile = $savefile; Save file Path}/* * @todo set Image title-set the title of the chart * * @param string $tstring * @param float $tfontsize * @param f Loat $tangle * @param int $tposx * @param int $tposy * @param string $tfontfile * @param int $tfred * @param int $tfgreen * @param int $tfblue * */function settitle ($tstring = ", $tfontsize = $, $tangle = 0, $tposx = ten, $tposy =, $tfontfi  Le = ' C:/windows/fonts/simhei.ttf ', $tfred = 0, $tfgreen = 0, $tfblue = 0) {$this->title = array ($tfontsize, $tangle, $tposx, $tposy, $tfontfile, $tstring, $tfred, $TFGreen, $tfblue); }/* * @todo Set image annotations-set the decsription of the chart * * @param int $DPOSX * @param int $dposy * @param int $dwidth * @p Aram Int $dheight * @param int $dmargin * @param int $dsize * @param int $dangle * @param string $dfontfile * @param int $ dfred * @param int $dfgreen * @param int $dfblue * */function setdesc ($dposx = 0, $dposy = 0, $dwidth = +, $dheight = 30 0, $dmargin = ten, $dsize = ten, $dangle = 0, $dfontfile = ' C:/windows/fonts/simhei.ttf ', $dfred = 0, $dfgreen = 0, $dfblue  = 0) {$this->desc = array ($dposx, $dposy, $dwidth, $dheight, $dmargin, $dsize, $dangle, $dfontfile, $dfred, $dfgreen, $dfblue); }/* * @todo Set Graph-Set the graphics of the chart * * @param int $GSTARTX * @param int $gstarty * @param int $gwidth * @pa  Ram int $gheight * @param int $gmargin * @param float $shadow * */function setgraphics ($gstartx = 0, $gstarty = 0, $gwidth = +, $gheight = +, $gmargin = ten, $shadow = 0.1) {$this->graphics = array ($gstartx, $gstarty, $gwidth, $gheiGht, $gmargin, $shadow); }//============================ build Function-Build functions ============================/* * @todo Generate Graph Instance-build chart Obje  CT * * */function Buildchart () {//Image header information-header of the the image file header ("Content-type:image/jpeg");  Create image-Create image $this->chart = Imagecreatetruecolor ($this->width, $this->height); Fill background Color-set BackgroundColor $BGC = Imagecolorallocate ($this->chart, $this->bgcolor[0], $this->bgcolor[1], $t         HIS-&GT;BGCOLOR[2]);  Imagefill ($this->chart, 0, 0, $BGC); Define color-allocate colors in the graphics foreach ($this->colordefine as $key + $value) {$ $key = I  Magecolorallocate ($this->chart, $value [0], $value [1], $value [2]);    Array_push ($this->colors, $ $key); Color variable into the stack-add color variables into array}}/* * @todo Generate Chart title-build title of the chart * */function Buildtitle ( {//Set title color-Set Title $titlecolor = Imagecolorallocate ($this->chart, $this-> title[6], $this->title[7], $this->title[8]); Write caption-write title Imagettftext ($this->chart, $this->title[0], $this->title[1], $this->title[2], $this-&G T;title[3], $titlecolor, $this->title[4], $this->title[5]);   }/* * @todo Generate Chart Description-build description of the chart * */function Builddesc () {//Comment position variable-image position variables  $DPOSX = $this->desc[0];     $dposy = $this->desc[1];     Note starting coordinates-the start position of the description $w = $this->desc[2];     Note section Width-width of all description $h = $this->desc[3];     Note section Height-height of all description $num = count ($this->param);      Comment Quantity-Number of description lines $perh = round ($h/$num);     Average height of comments per row-height of each description line $margin = $this->desc[4];     Spacing of notes-margin between square and font $dsize = $this->desc[5];     Font size for annotations-font size of description $dangle = $this->desc[6]; Font angle for annotations-font display angle of description $dfontfilE = $this->desc[7]; Note Font file-font files of description $dfcolor = Imagecolorallocate ($this->chart, $this->desc[8], $this->desc[9]     , $this->desc[10]);  Font color for annotations-font color of description//Write Comment-write description $dstring = Array_keys ($this->param); for ($i = 0; $i < $num; $i + +) {//Rectangular color blocks-colorful square imagefilledrectangle ($this->chart, $DPOSX, $DPO  Sy, $DPOSX + $dsize, $dposy + $dsize, $this->colors[$i * 2]); Write text-write String Imagettftext ($this->chart, $dsize, $dangle, $dposx + $dsize + $margin, $dposy + $dsize, $DFC Olor, $dfontfile, $dstring [$i]. " - " .  $this->param[$dstring [$i]]);    Move down to write next description $dposy + = $perh; }/* * @todo Generate graph-build graphics of the chart * * @param source $chart * @param int $d 3d * @param array $param * @ param array $colordefine * * */function Buildgraphics () {//define Generate graphics functions-Define make graphics function//=========== ========= Pie chart-Pie==================== if ($this->type = = "Pie") {//Get graphic position-get the positoin of the graphics $posx = $this->g  Raphics[0];  $posy = $this->graphics[1];  Get the graphics width and height-get width and height of the graphics $w = $this->graphics[2];  $h = $this->graphics[3];  Graphics margin-graphics margin $margin = $this->graphics[4];  3D Shadow height ratio for ellipse height-percent of 3D effect shadow height as the height of the ellipse $shadow = $this->graphics[5];  Graphics position variable-image position variables $centerx = round ($posx + $w/2 + $margin);     $centery = Round ($posy + $h/2 + $margin);     Ellipse Center coordinates-the center of the ellipse//data processing-data Process $total = Array_sum ($this->param);     Get Total $percent = array (0);  Save scale-Save each percent $temp = 0;  foreach ($this->param as $v) {$temp + = ($v/$total);     Array_push ($percent, $temp); Saving angle-Save angle}//Generate pie chart-make pie chart//Generate 3D Pie chart-make 3D pie chart if ($thIs->d3d = = 1) {//3D shadow-Make 3D shadow for ($j = ($centery * (1 + $shadow)) $j > $centery; $j-) {for ($k = 0; $k < count ($percent)-1; $k + +) {Imagefil Ledarc ($this->chart, $centerx, $j, $w, $h, $percent [$k], $percent [$k + 1], $this->colors[$k * 2 + 1], Img_arc_nofill                );        }}}//Generate flat pie chart-make 2D pie chart for ($i = 0; $i < count ($percent)-1; $i + +) {Imagefilledarc ($this->chart, $centerx, $centery, $w, $h, $percent [$i], $percent [$i + 1], $this->colors[$i        * 2], img_arc_pie); }}//==================== histogram-column ==================== elseif ($this->type = = "column") {//Get graphic position-get  The positoin of the graphics $posx = $this->graphics[0];  $posy = $this->graphics[1];  Get the graphics width and height-get width and height of the graphics $w = $this->graphics[2];  $h = $this->graphics[3]; Graphics Margins-Graphics Margin $margin = $this->graphics[4];  3D Shadow height proportional to column width-percent of 3D effect shadow height as the width of the column $shadow = $this->graphics[5];  Graphic position variable-image position variables $startx = round ($posx + $margin);     $starty = Round ($posy + $h-$margin);    Lower left corner coordinates-the left-bottom position of the graphics//data processing-data Process $maxvalue = max ($this->param);     Get maximum value-Get max value $num = count ($this->param);     Gets the number of bars-get numbers of columns $multiple = Floor (log10 ($maxvalue));     Get the value range-get data field $field = Floor ($maxvalue/pow ($multiple)) + 1; Interval count-Number of unit fields $fieldnumber = $field > 5?     $field: 5;     Number of data regions-numbers of data fields $topvalue = $field * POW ($multiple);     Chart highest point value-value of the top $UNITX = ($w-$margin * 2)/$num;     Get x unit Length-get x unit length $unity = ($h-$margin * 2)/$fieldnumber; Get y units Length-Get y unit length $shadowheight = $UNITX/2 * $shadoW            Shadow width-shadow height//initialization coordinate system-initialize reference frame if ($this->d3d = = 1)//3D effect-3D effect { Imagefilledpolygon ($this->chart, Array ($startx, $starty, $startx + $shadowheight, $starty-$shadowheight, $s            Tartx + $shadowheight, $posy-$shadowheight, $startx, $posy), 4, $this->colors[0]); Imageline ($this->chart, $startx + $shadowheight, $starty-$shadowheight, $startx + $shadowheight, $posy-$shadowheigh           T, $this->colors[1]);     } imageline ($this->chart, $startx, $starty, $posx + $w, $starty, $this->colors[1]);     X imageline ($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]);       Y//Interval identification-declare fields for ($i = 0; $i <= $fieldnumber; $i + +) {//Interval identification line-lines declaring fields 3D Effect-3D effect if ($this->d3d = = 1) {imageline ($this->chart, $startx, $starty- $unity * $i, $startx + $shadowheight, $starty-$unity* $i-$shadowheight, $this->colors[1]); Imageline ($this->chart, $startx + $shadowheight, $starty-$unity * $i-$shadowheight, $posx + $w + $shadowheight, $st       arty-$unity * $i-$shadowheight, $this->colors[1]); }//2d-2d else {imageline ($this->chart, $startx, $starty-$unity * $i, $posx + $w, $star             ty-$unity * $i, $this->colors[0]); }//Interval description-field description Imagettftext ($this->chart, $this->desc[5], $this->desc[6], $POSX, $sta         rty-$unity * $i, $this->colors[0], $this->desc[7], $topvalue/$fieldnumber * $i);  }//Generate bars-make columns $paramkeys = Array_keys ($this->param); for ($j = 0; $j < $num; $j + +) {$columnheight = ($h-$margin * 2) * ($this->param[$paramkeys [$j]]/$TOPV     Alue);     Bar height-Column $columnx = $startx + $unitx/4 + $unitx * $J; Bar beginning X coordinate-X coordinate of column imagefilledrectangle ($this->chart, $ColumnX, $starty-$columnheight, $columnx + $unitx/2, $starty-1, $this->colors[$j * 2]);                   Draw Bar-Draw columns//3D effect-3D effect if ($this->d3d = = 1) {//contour-Contour Line Imagerectangle ($this->chart, $columnx, $starty-$columnheight, $columnx + $unitx/2, $starty-1, $this->colors  [$j * 2 + 1]);  3D Surface-3D top Imagefilledpolygon ($this->chart, Array ($COLUMNX, $starty-$columnheight, $columnx + $UNITX/ 2, $starty-$columnheight, $columnx + $unitx/2 + $shadowheight, $starty-$columnheight-$shadowheight, $columnx + $s          Hadowheight, $starty-$columnheight-$shadowheight), 4, $this->colors[$j * 2]); Imagepolygon ($this->chart, Array ($COLUMNX, $starty-$columnheight, $columnx + $unitx/2, $starty-$columnheight, $co Lumnx + $UNITX/2 + $shadowheight, $starty-$columnheight-$shadowheight, $columnx + $shadowheight, $starty-$columnhe ight-$shadowheight), 4, $this->colors[$j * 2 + 1]); 3D Shadow-3D shadow Imagefilledpolygon ($this->chart, Array ($columnx + $UNITX/2, $starty, $columnx + $UNITX /2 + $shadowheight, $starty-$shadowheight, $columnx + $unitx/2 + $shadowheight, $starty-$columnheight-$shadowheig          HT, $COLUMNX + $UNITX/2, $starty-$columnheight), 4, $this->colors[$j * 2 + 1]); Imagepolygon ($this->chart, Array ($columnx + $UNITX/2, $starty, $columnx + $unitx/2 + $shadowheight, $starty-$shad  Owheight, $columnx + $unitx/2 + $shadowheight, $starty-$columnheight-$shadowheight, $columnx + $unitx/2, $starty-            $columnheight), 4, $this->colors[$j * 2 + 1]); }}}//==================== graph-line ==================== else {//get graphic position-get the positoin of the G  Raphics $posx = $this->graphics[0];  $posy = $this->graphics[1];  Get the graphics width and height-get width and height of the graphics $w = $this->graphics[2];  $h = $this->graphics[3]; Graphics margin-graphics margin $margin = $this->gRAPHICS[4];  Dia. per point-diameter of $pointsize = $this->graphics[5] * 20;  Graphic position variable-image position variables $startx = round ($posx + $margin);     $starty = Round ($posy + $h-$margin);    Lower left corner coordinates-the left-bottom position of the graphics//data processing-data Process $maxvalue = max ($this->param);    Get maximum value-Get max value $minvalue = min ($this->param);     Get min-Get min Value $num = count ($this->param);     Get number of points $fieldnumber = $num;     Number of data regions-numbers of data fields $fielddata = $maxvalue-$minvalue;     Get data range-get field $unitdata = $fielddata/$fieldnumber;     Get unit interval value-Get Unit field data value $UNITX = ($w-$margin * 2)/$num;     Get x unit Length-get x unit length $unity = ($h-$margin * 2)/$fieldnumber;  Get y units Length-Get y unit length//Initialize coordinate system-Initialize reference frame Imageline ($this->chart, $startx, $starty,     $posx + $w, $starty, $this->colors[1]);     XImageline ($this->chart, $startx, $starty, $startx, $posy, $this->colors[1]);       Y//Interval identification-declare fields for ($i = 0; $i <= $fieldnumber; $i + +) {//Logo-declaring fields line        Imageline ($this->chart, $startx, $starty-$unity * $i, $posx + $w, $starty-$unity * $i, $this->colors[0]); Interval Description-field description Imagettftext ($this->chart, $this->desc[5], $this->desc[6], $POSX, $st         arty-$unity * $i, $this->colors[0], $this->desc[7], $minvalue + $unitdata * $i);  }//Generate lines-make line $paramkeys = Array_keys ($this->param);     $loca = Array (); An array of saved tracks-array to save Locas for ($i = 0; $i < $num; $i + +)//Get the track array-get Loca array {//point x coordinate-X  Coordinate of the point $pointx = $startx + $unitx * $i; Point y-coordinate-y coordinate of the $pointy = $starty-$unity * $fieldnumber * ($this->param[$paramkeys [$i]]-$MINV  alue)/$fielddata); coordinate data into the stack-push coordinates into ARRAY $loca [$i * 2] = $pointx;  $loca [$i * 2 + 1] = $pointy; Draw points-Draw point Imagefilledellipse ($this->chart, $pointx, $pointy, $pointsize, $pointsize, $this->colors[$i           * 2]);     } $linecolor = Imagecolorallocate ($this->chart, $this->title[6], $this->title[7], $this->title[8]);     Define line Colors-Define lines color for ($i = 0; $i < $num + 3;)   {//Draw lines-Draw line Imageline ($this->chart, $loca [$i], $loca [$i + 1], $loca [$i + 2], $loca [$i + 3], $linecolor);     $i + = 2; }}}//* @todo output image to browser or file--output the chart image to browser or file */function Outputchart () {//Build icon-Buil  D Chart $this->buildchart ();  Write graphics-Build graphics $this->buildgraphics ();  Write Comment-Build description $this->builddesc ();  Write title-build title $this->buildtitle ();         Output image-Flush image imagejpeg ($this->chart, $this->savefile, $this->quality);     Imagedestroy ($this->chart); }}?>



*********************************************************************************

Demo File demo.php:
**********************************************************************************

  20, "February: 16%" = 23, "March: 19%" and "27," April: 13% "+ 18," May: 18% "+ 24," June: 12% "+ 25); Instantiation $chart = new Plchart ();     Set background color//three parameters for 0-255 $chart->setbgcolor (255,255,255); Set Caption Text $title = <<< String chart to test the plot of the tested PIE chart string; Set CHART Data */* The first parameter is a chart type and can only be three values: Pie, column, line * The second parameter is whether to enable 3D display, 0 for not enabled, 1 for start * The third parameter is the width of the chart * The fourth parameter is the height of the chart * The fifth parameter is a picture of the chart Quality, 0-100,100 is the best quality, but the file is also the largest. * The sixth parameter is the original data array */$chart->setchartdata ("line", 0, (+), (+), $data); Set Title/* * The first argument is the title of the string * The second argument is the title font size * The third parameter is the title of the tilt angle * The fourth parameter is the title in the beginning horizontal axis of the chart * The fifth parameter is the base ordinate of the caption text * The sixth parameter is the title font file * The last three parameters are the title The RGB color of the font */$chart->settitle ($title, 0, 0, 0, ' C:/windows/fonts/simhei.ttf ', 0,,);  Set comment/* * The first parameter is the note at the beginning of the chart * The second parameter is the comment in the beginning ordinate of the chart * The third parameter is the width of the comment section * The fourth parameter is the height of the comment section * The fifth parameter is the spacing between the comment content and the comment part border * Sixth parameter is the comment font size * Seventh parameter for note font Tilt angle * Sixth parameter for note font file * The last three parameters are the RGB color of the note font */$chart->setdesc ($,, 0, ' c:/windows/fonts /simhei.ttf ', 0, 0, 0); Set Graphics/* * The first parameter is the shape portion of the entireThe upper-left corner of a chart * The second parameter is the graphic part in the upper-left ordinate of the entire chart * The third parameter is the width of the graphics section * The fourth parameter is the height of the graphics section * The fifth parameter is the distance between the shape and the part border of the graphic (the width of the ordinate mark) * The sixth parameter is the relative proportions of the 3D shadow or the diameter of the dots in the trajectory chart/$chart->setgraphics (10, 100, 500, 500, 50, 0.3);     Output Chart $chart->outputchart (); Destroy instance unset ($chart);?>
  • 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.