Class for generating line charts of horizontal and vertical bar charts using the GD Image Library in PHP

Source: Internet
Author: User

I found a GD class to generate multiple types of images, and I don't know whether the source is wrong or the reprinted person has a problem, after the experiment, if you change the image length and width parameters, the image data is displayed incorrectly. Therefore, you can adjust and modify the image length and width parameters to correct the problem and paste it for future reference:
<? PHP

Class imagereport {
VaR $ X; // image size X axis
VaR $ Y; // the Y axis of the image size.
VaR $ R; // R value of the background color
VaR $ g; //... G.
VaR $ B; //... B.
VaR $ transparent; // whether transparent 1 or 0
VaR $ image; // Image
//-------------------
VaR $ arraysplit; // specifies the symbol used to separate numeric values.
VaR $ itemarray; // Value
VaR $ reporttype; // chart type. 1 is a vertical column, 2 is a horizontal column, and 3 is a line.
VaR $ border; // distance
//-------------------
VaR $ fontsize; // font size
VaR $ fontcolor; // font color
// -------- Parameter setting function (for background image)
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;
}

// ---------------- Subject
// Based on the given parameters
Function printreport (){
Header ("Content-Type: image/PNG ");
// Create the canvas size
$ This-> image = imagecreate ($ this-> X, $ this-> Y );
// Set the canvas background color
$ Background = imagecolorallocate ($ this-> image, $ this-> r, $ this-> G, $ this-> B );

// Whether the background is transparent or not
If ($ this-> transparent = "1 "){
Imagecolortransparent ($ this-> image, $ background );
} Else {
// The background color can be filled if it is not transparent.
Imagefilledrectangle ($ this-> image, 0, 0, $ this-> X, $ this-> Y, $ background );
}

// Parameter font size and color
$ This-> fontcolor = imagecolorallocate ($ this-> image, 255-$ this-> r, 255-$ this-> G, 255-$ this-> B );

// Select vertical column, horizontal column, or line based on reporttype
Switch ($ this-> reporttype ){
Case "0 ":
Break;
Case "1 ":
$ This-> imagecolumns ();
Break;
Case "2 ":
$ This-> imagecolumnh ();
Break;
Case "3 ":
$ This-> imageline ();
Break;
}

// Call print to print the XY axis and Image
$ This-> printxy ();
$ This-> printall ();
}
// ----------- Print the XY axis
Function printxy (){
// Draw the 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-> Y-$ this-> border, $ color); // y axis
Imageline ($ this-> image, $ this-> border, $ this-> Y-$ this-> border, $ this-> X-$ this-> border, $ this-> Y-$ this-> border, $ color); // X axis
// The scale on the Y axis is determined by the total length-segment length.
$ 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 ); // The scale is outside the Y axis (left side), not inside.
}

// The scale on the X axis uses the length of a part + the length of a Part <total length to determine the underline position
$ Rulerx = '';
// $ Rulerx = $ rulerx + $ this-> border;
$ 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); // The scale is also on the outer side (below the X axis)
}
}

// -------------- Vertical bar 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 = revert (50,200 );
$ G = revert (50,200 );
$ B = revert (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); // the original statement. I don't know why it is so complicated.
$ Height = $ this-> Y-$ this-> border-$ item_array [$ I];
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 );
// Used 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 = revert (50,200 );
$ G = revert (50,200 );
$ B = revert (50,200 );
}
$ Color = imagecolorallocate ($ this-> image, $ R, $ g, $ B );
// Column Length
// $ Leight = ($ this-> X-$ this-> border * 2) * ($ item_array [$ I]/$ item_max ); // The original debugging shows an error
$ Leight = $ this-> border + $ item_array [$ I];
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 );
// Used 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;
$ Xx = $ this-> border; // If $ xx = 0, the line starts from x = 0, y = $ item_array (0) coordinate point.
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Item_max = max ($ item_max, $ item_array [$ I]);
}
 
// 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 = revert (50,200 );
$ G = revert (50,200 );
$ B = revert (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); // original
$ Height_now = $ this-> Y-$ this-> border-$ item_array [$ I];

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; // original
// Used for Interval
$ Xx = $ xx + $ this-> border;
}
}

// -------------- Print the image
Function printall (){
Imagepng ($ this-> image );
Imagedestroy ($ this-> image );
}
// -------------- Debug
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 of values:". $ 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); // parameter (length, width, background color R, G, B, whether transparent 1 or 0) Note: setitem, when the style is set to 2, $ sizex and $ sizey are reversed. The original text can only display the 600,300 bar chart normally. If the style is changed to 660 or 330, the display will go down.
$ Temparray = "79,25, 100,250,180,200,150,220,200,150, 50, 25, 100,250,180,200,150,220,200,150"; // value, separated by a specified symbol
$ Report-> setitem (',', $ temparray,); // parameters (specify the delimiter for separating values, numerical variables, style 1 is vertical bar chart 2 is horizontal bar chart 3 is a line chart, distance)
$ Report-> setfont (1); // font size: 1-10
$ Report-> printreport ();
// $ Report-> debug (); // call for debugging

?>

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.