PHP lightweight chart type PHPLightChart: PLChart can generate pie charts (2D/3D), bar charts (2D/3D), and line charts to control output to browsers or files, controls the quality of the output jpeg image.
All settings work only five functions:
1. Setbgcolor ()
2. Setchartdata ()
2. Settitle ()
3. Setdesc ()
4. Graph setting setgraphics ()
For detailed parameter settings, see the document comment or the demo. php on the test page below.
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, 80, 0), "lightblue" => array (0, 0,255), "darkblue" => array (0, 0, 80), "lightyellow" => array (255,255, 0), "darkyellow" => array (200,200, 50), "lightcyan" => array (0,255,255), "darkcyan" => array (0, 90, 90),); // = ====================================== Set the function-setting functions ============== =========================/ ** @ todo set the image background color-set the backgroundcolor 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 the image parameter-set the parameters of the chart ima Ge ** @ param string $ type * @ param int $ d3d * @ param int $ width * @ param int $ height * @ param int $ quality * @ param array $ data *@ param string $ savefile **/function setchartdata ($ type = "pie ", $ d3d = 1, $ width = 400, $ height = 400, $ quality = 70, $ data = array (), $ savefile = '') {$ this-> type = $ type; // chart type-type of the chart: pie, column, line $ this-> d3d = $ d3d; // 3D effect-$ this-> width = $ w Idth; // chart width $ this-> height = $ height; // chart height $ this-> quality = $ quality; // quality of chart display $ this-> param = $ data; // source data array $ this-> savefile = $ savefile; // Save the file path}/** @ todo set the image title-set the title of the chart ** @ param string $ tstring * @ param float $ tfontsize * @ param float $ tangle * @ param int $ tposx * @ param int $ tposy * @ param string $ tfontfile * @ param int $ tfred * @ param int $ tfgreen * @ param int $ Tfblue **/function settitle ($ tstring = '', $ tfontsize = 20, $ tangle = 0, $ tposx = 10, $ tposy = 20, $ tfontfile = '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 the image annotation-set the decsription of the chart ** @ param int $ dposx * @ param int $ dposy * @ param Int $ dwidth * @ param 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 = 100, $ dheight = 300, $ dmargin = 10, $ dsize = 10, $ dangle = 0, $ dfontfile = 'C: /windows/fonts/simhei. ttf', $ dfred = 0, $ dfgreen = 0, $ dfblue = 0) {$ this-> des C = array ($ dposx, $ dposy, $ dwidth, $ dheight, $ dmargin, $ dsize, $ dangle, $ dfontfile, $ dfred, $ dfgreen, $ dfblue );} /** @ todo: set the graphics of the chart ** @ param int $ gstartx * @ param int $ gstarty * @ param int $ gwidth * @ param int $ gheight * @ param int $ gmargin * @ param float $ shadow **/function setgraphics ($ gstartx = 0, $ gstarty = 0, $ gwidth = 100, $ gheight = 100, $ gmargin = 10, $ shad Ow = 0.1) {$ this-> graphics = array ($ gstartx, $ gstarty, $ gwidth, $ gheight, $ gmargin, $ shadow );} // ============================== generate a function-build functions ====== ========================================// ** @ todo to generate a chart instance-build chart object **/function buildchart () {// image header information-header of the image file header ("Content-type: image/jpeg "); // create image $ this-> chart = imagecreatetruecolor ($ this-> width, $ this-> Height); // fill in the background color-set backgroundcolor $ bgc = imagecolorallocate ($ this-> chart, $ this-> bgcolor [0], $ this-> bgcolor [1], $ this-> bgcolor [2]); imagefill ($ this-> chart, 0, 0, $ bgc ); // define the color-allocate colors in the graphics foreach ($ this-> colordefine as $ key => $ value) {$ key = imagecolorallocate ($ this-> chart, $ value [0], $ value [1], $ value [2]); array_push ($ this-> colors, $ key ); // add color var Iables into array}/** @ todo generate the chart title-build title of the chart **/function buildtitle () {// set the title color-set title color $ titlecolor = imagecolorallocate ($ this-> chart, $ this-> title [6], $ this-> title [7], $ this-> title [8]); // write the title-write title imagettftext ($ this-> chart, $ this-> title [0], $ this-> title [1], $ this-> title [2], $ this-> title [3], $ titlecolor, $ this-> title [4], $ this-> title [5]);}/** @ todo Generate chart description-build description of the chart **/function builddesc () {// comment on the location variable-image position variables $ dposx = $ this-> desc [0]; $ dposy = $ this-> desc [1]; // annotation start coordinate-the start position of the description $ w = $ this-> desc [2]; // annotation part width-width of all description $ h = $ this-> desc [3]; // comments part height-height of all description $ num = count ($ this-> param); // number of comments-number of description lines $ perh = roun D ($ h/$ num); // average height of each line comment-height of each description line $ margin = $ this-> desc [4]; // annotation spacing-margin between square and font $ dsize = $ this-> desc [5]; // font size of the comment-font size of description $ dangle = $ this-> desc [6]; // font angle of the comment-font display angle of description $ dfontfile = $ this-> desc [7]; // annotate the font file-font file of description $ dfcolor = imagecolorallocate ($ this-> chart, $ this-> desc [8], $ this-> d Esc [9], $ this-> desc [10]); // font color of the annotation-font color of description // write the annotation-write description $ dstring = array_keys ($ this-> param); for ($ I = 0; $ I <$ num; $ I ++) {// rectangle color block-colorful square imagefilledrectangle ($ this-> chart, $ dposx, $ dposy, $ dposx + $ dsize, $ dposy + $ dsize, $ this-> colors [$ I * 2]); // write text-write string imagettftext ($ this-> chart, $ dsize, $ dangle, $ dposx + $ dsize + $ margin, $ dposy + $ ds Ize, $ dfcolor, $ dfontfile, $ dstring [$ I]. "-". $ this-> param [$ dstring [$ I]); // move down-move down to write next description $ dposy + = $ perh ;}} /** @ todo generate a graph-build graphics of the chart ** @ param source $ chart * @ param int $ d3d * @ param array $ param * @ param array $ colordefine ** /function buildgraphics () {// define the graph generation function-define make graphics function // ================================== pie chart-pie = ===== ================ If ($ this-> type = "pie ") {// Obtain the image location-get the positoin of the graphics $ posx = $ this-> graphics [0]; $ posy = $ this-> graphics [1]; // Obtain the image width and height-get width and height of the graphics $ w = $ this-> graphics [2]; $ h = $ this-> graphics [3]; // Image margin-graphics margin $ margin = $ this-> graphics [4]; // ratio of 3D shadow height to elliptical height-percent of 3D effect shadow height as the height of the ellipse $ shadow = $ this -> Graphics [5]; // image position variable-image position variables $ centerx = round ($ posx + $ w/2 + $ margin ); $ centery = round ($ posy + $ h/2 + $ margin ); // elliptic center coordinate-the center of the ellipse // data processing-data process $ total = array_sum ($ this-> param ); // Retrieve total-get total $ percent = array (0); // save ratio-save each percent $ temp = 0; foreach ($ this-> param as $ v) {$ temp + = 360 * ($ v/$ total); array_push ($ percent, $ tem P ); // save angle-save angle} // Generate pie chart-make pie chart // generate a 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 ++) {imagefilledarc ($ this-> chart, $ centerx, $ j, $ w, $ h, $ percent [$ k], $ percent [$ k + 1], $ this-> colors [$ k * 2 + 1], IMG_ARC_NOFILL) ;}}// generate a 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 );}} // ================================= bar chart-column ====================== ===== elseif ($ this-> type = "column ") {// Obtain the image location-get the positoin of the graphics $ posx = $ this-> graphics [0]; $ posy = $ this-> graphics [1]; // Obtain the image width and height-get width and he Ight of the graphics $ w = $ this-> graphics [2]; $ h = $ this-> graphics [3]; // Image margin-graphics margin $ margin = $ this-> graphics [4]; // ratio of 3D shadow height to the width of the bar-percent of 3D effect shadow height as the width of the column $ shadow = $ this-> graphics [5]; // image position variable-image position variables $ startx = round ($ posx + $ margin); $ starty = round ($ posy + $ h-$ margin ); // coordinate in the lower left corner of the graph-the left-bottom position of the graph Ics // data processing-data process $ maxvalue = max ($ this-> param); // obtain the maximum value-get max value $ num = count ($ this-> param ); // Obtain the number of columns-get number of columns $ multiple = floor (log10 ($ maxvalue )); // obtain the value range-get data field $ field = floor ($ maxvalue/pow (10, $ multiple) + 1; // number of intervals-number of unit fields $ fieldnumber = $ field> 5? $ Field: 5; // number of data regions-number of data fields $ topvalue = $ field * pow (10, $ multiple ); // the highest point value of the chart-value of the top $ unitx = ($ w-$ margin * 2)/$ num; // get x unit length-get x unit length $ unity = ($ h-$ margin * 2)/$ fieldnumber; // get y unit length-get y unit length $ shadowheight = $ unitx/2 * $ shadow; // shadow width-shadow height // initialize the coordinate system-initialize reference frame if ($ this-> d3d = 1) // 3D effect-3D effect {Imagefilledpolygon ($ this-> chart, array ($ startx, $ starty, $ startx + $ shadowheight, $ starty-$ shadowheight, $ startx + $ shadowheight, $ posy-$ shadowheight, $ startx, $ posy), 4, $ this-> colors [0]); imageline ($ this-> chart, $ startx + $ shadowheight, $ starty-$ shadowheight, $ startx + $ shadowheight, $ posy-$ shadowheight, $ 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 ID-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 ($ t His-> chart, $ startx + $ shadowheight, $ starty-$ unity * $ I-$ shadowheight, $ posx + $ w + $ shadowheight, $ starty-$ unity * $ I-$ shadowheight, $ this-> colors [1]);} // 2D-2D else {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, $ starty-$ u Nity * $ I, $ this-> colors [0], $ this-> desc [7], $ topvalue/$ fieldnumber * $ I );} // Generate bar-make columns $ paramkeys = array_keys ($ this-> param); for ($ j = 0; $ j <$ num; $ j ++) {$ columnheight = ($ h-$ margin * 2) * ($ this-> param [$ paramkeys [$ j]/$ topvalue ); // bar height-column height $ columnx = $ startx + $ unitx/4 + $ unitx * $ j; // bar start point x coordinate-x coordinate of column imagefilledrectangle ($ this-> chart, $ colu Mnx, $ 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, $ colu Mnx + $ unitx/2, $ starty-$ columnheight, $ columnx + $ unitx/2 + $ shadowheight, $ starty-$ columnheight-$ shadowheight, $ columnx + $ shadowheight, $ starty-$ columnheight-$ shadowheight), 4, $ this-> colors [$ j * 2]); imagepolygon ($ this-> chart, array ($ columnx, $ starty-$ columnheight, $ columnx + $ unitx/2, $ starty-$ columnheight, $ columnx + $ unitx/2 + $ shadowheight, $ starty-$ columnheight-$ Shadowheight, $ columnx + $ shadowheight, $ starty-$ columnheight-$ 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-$ shadowheight, $ columnx + $ unitx/2, $ starty-$ columnheight), 4, $ This-> colors [$ j * 2 + 1]); imagepolygon ($ this-> chart, array ($ columnx + $ unitx/2, $ starty, $ columnx + $ unitx/2 + $ shadowheight, $ starty-$ shadowheight, $ columnx + $ unitx/2 + $ shadowheight, $ starty-$ columnheight-$ shadowheight, $ columnx + $ unitx/2, $ starty-$ columnheight), 4, $ this-> colors [$ j * 2 + 1]);} // ===================================== graph-line ====================== ===== else {// Obtain the image location-ge T the positoin of the graphics $ posx = $ this-> graphics [0]; $ posy = $ this-> graphics [1]; // Obtain the image width and height-get width and height of the graphics $ w = $ this-> graphics [2]; $ h = $ this-> graphics [3]; // Image margin-graphics margin $ margin = $ this-> graphics [4]; // the diameter of each vertex-diameter of each point $ pointsize = $ this-> graphics [5] * 20; // image position variable-image position variables $ startx = round ($ posx + $ margin); $ starty = r Ound ($ posy + $ h-$ margin ); // coordinate in the lower left corner of the image-the left-bottom position of the graphics // data processing-data process $ maxvalue = max ($ this-> param ); // obtain the maximum value-get max value $ minvalue = min ($ this-> param); // obtain the minimum value-get min value $ num = count ($ this-> param ); // Obtain the number of vertices-get number of points $ fieldnumber = $ num; // number of data regions-number of data fields $ fielddata = $ maxvalue-$ minvalue; // obtain the data range-get data field $ unit Data = $ fielddata/$ fieldnumber; // Obtain the 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 unit length-get y unit length // initialize the coordinate system-initialize reference frame imageline ($ this-> chart, $ startx, $ starty, $ posx + $ w, $ starty, $ this-> colors [1]); // x imageline ($ this-> chart, $ startx, $ starty, $ startx, $ posy, $ This-> colors [1]); // y // interval ID-declare fields for ($ I = 0; $ I <= $ fieldnumber; $ I ++) {// identification line-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, $ starty-$ unity * $ I, $ this-> colors [0], $ this-> desc [7], $ minvalue + $ unitda Ta * $ I);} // Generate a line-make line $ paramkeys = array_keys ($ this-> param); $ loca = array (); // save the trajectory array-array to save locas for ($ I = 0; $ I <$ num; $ I ++) // Obtain the trajectory array-get loca array {// point x coordinate-x coordinate of the point $ pointx = $ startx + $ unitx * $ I; // point y coordinate-y coordinate of the point $ pointy = $ starty-$ unity * $ fieldnumber * ($ this-> param [$ paramkeys [$ I]-$ minvalue) /$ fielddata); // coordinate data into the stack-p Ush coordinates into array $ loca [$ I * 2] = $ pointx; $ loca [$ I * 2 + 1] = $ pointy; // draw point-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 the line color-define line color for ($ I = 0; $ I <$ num + 3;) {// draw a line-draw line imageline ($ this-> char T, $ loca [$ I], $ loca [$ I + 1], $ loca [$ I + 2], $ loca [$ I + 3], $ linecolor ); $ I + = 2 ;}}/ ** @ todo output the image to the browser or file-output the chart image to browser or file */function outputchart () {// create an icon-build chart $ this-> buildchart (); // write a graph-build graphics $ this-> buildgraphics (); // write comments-build description $ this-> builddesc (); // write the title-build title $ this-> buildtitle (); // output image-flush image imagejpeg ($ th Is-> chart, $ this-> savefile, $ this-> quality); imagedestroy ($ this-> chart) ;}}?>
**************************************** **************************************** *
Demo. php:
**************************************** **************************************** **
20, "August: 16%" => 23, "August: 19%" => 27, "August: 13%" => 18, "August: 18% "=> 24," March: 12% "=> 25); // instantiate $ chart = new plchart (); // set the background color. // The three parameters are 0-255 $ chart-> setbgcolor (255,255,255 ); // set the title text $ title = <string chart test pie chart string; // Set The CHART Data/** The first parameter is of the CHART type and can only be three values: pie, column, line * The second parameter is whether to enable 3D display. 0 indicates that 3D display is not enabled, 1 indicates starting * The third parameter is the chart width * the fourth parameter is the chart height * The Fifth parameter is the image quality of the chart, and 0-100,100 is the best quality, but the file is relatively large * The sixth parameter is the original data array */$ c Hart-> setchartdata ("line", 0,800,600,100, $ data ); // set the title/** The first parameter is the title string * The second parameter is the title font size * The third parameter is the title tilt angle * the fourth parameter is the title located at the beginning of the chart x coordinate * The Fifth parameter is the reference ordinate of the title text * The sixth parameter is the title font file * the last three parameters are the RGB colors of the title font */$ chart-> settitle ($ title, 20, 0, 10, 20, 'C:/windows/fonts/simhei. ttf', 0, 0, 0 ); // Set the comment/** The first parameter is the comment at the starting abscissa of the chart * The second parameter is the comment at the starting ordinate of the chart * The third parameter is the width of the comment * The fourth the parameter is the height of the comment part * The Fifth parameter is the spacing between the comment content and the comment part Border * The sixth parameter is the comment font size * The seventh parameter is the comment Font skew angle * The sixth parameter is the comment font file * the last three parameters are the RGB color of the comment font */$ chart-> setdesc (500,100,200,300, 10, 18, 0, 'C:/windows/fonts/simhei. ttf', 0, 0, 0 ); // set the image. ** The first parameter is the horizontal coordinate in the upper left corner of the entire chart. * The second parameter is the vertical coordinate in the upper left corner of the entire chart. * The third parameter is width * fourth parameter is the height of the graphic part * fifth parameter is the spacing between the border of the graphic part (width marked by the ordinate) * The sixth parameter is the relative proportion of the 3D shadow or the dot diameter in the trajectory graph */$ chart-> setgraphics (10,100,500,500, 50, 0.3 ); // Output chart $ chart-> outputchart (); // destroy the instance unset ($ chart);?>