PHP image class that displays multiple graphical reports. Vertical Column chart, horizontal column chart, column chart, line chart, pie chart

Source: Internet
Author: User

<? PHP // graph class
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
 
VaR $ numx = 1; // start scale value of the X axis
VaR $ stepx = 1; // The interval between each scale on the X axis
 
// -------- 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;
}
// Set the X axis scale value
Function setx ($ numx = 1, $ stepx = 1 ){
$ This-> numx = $ numx;
$ This-> stepx = $ stepx;
}
// ---------------- Subject
Function printreport (){
// 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 );
If ($ this-> transparent = "1 "){
// Transparent background
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 );
}
// Small integer 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;
Case "4 ":
$ This-> imagecircle ();
Break;
}
$ This-> printxy ();
$ This-> printall ();
}
// ----------- Print the XY axis
Function printxy (){
$ Rulery = $ rulerx = "";
// 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); // X axis
Imageline ($ this-> image, $ this-> border, $ this-> Y-$ this-> border, $ this-> X-$ this-> border, $ this-> Y-$ this-> border, $ color); // y axis
Imagestring ($ this-> image, $ this-> fontsize, $ this-> border-2, $ this-> Y-$ this-> border + 5, "0 ", $ color );
// Scale up on the Y axis
$ Rulery = $ this-> Y-$ this-> border;
$ I = 0;
While ($ rulery> $ this-> border * 2 ){
$ Rulery = $ rulery-$ this-> border;
Imageline ($ this-> image, $ this-> border, $ rulery, $ this-> border-2, $ rulery, $ color );

If ($ this-> reporttype = 2) {// horizontal bar chart
Imagestring ($ this-> image, $ this-> fontsize, $ this-> border-10, $ rulerY-2-$ this-> border * ($ I +. 5), $ this-> numx, $ color );
$ This-> numx + = $ this-> stepx;
}
$ I ++;
}
// Scale up on the X axis
$ Rulerx = $ rulerx + $ this-> border;
$ I = 0;
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 );

// Scale value
If ($ this-> reporttype = 1) {// vertical bar chart
Imagestring ($ this-> image, $ this-> fontsize, $ rulerX-2 + $ this-> border * ($ I +. 5), $ this-> Y-$ this-> border + 5, $ this-> numx, $ color );
$ This-> numx + = $ this-> stepx;
} Else if ($ this-> reporttype = 3) {// line chart
Imagestring ($ this-> image, $ this-> fontsize, $ rulerX-2, $ this-> Y-$ this-> border + 5, $ this-> numx, $ color );
$ This-> numx + = $ this-> stepx;
}
$ I ++;
}
}
 
// -------------- 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 );
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 );
$ Leight = $ Leight <$ this-> border? $ This-> border: $ Leight;
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;
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 = 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 );
If ($ I! = "0 ")
Imageline ($ this-> image, $ XX-$ this-> border, $ height_next, $ XX, $ height_now, $ color );

Imagestring ($ this-> image, $ this-> fontsize, $ xx + 2, $ height_now-$ this-> border/2, $ item_array [$ I], $ this-> fontcolor );
$ Height_next = $ height_now;
// Used for Interval
$ Xx = $ xx + $ this-> border;
}
}
// -------------- Pie chart
Function imagecircle (){
$ Total = 0;
$ 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]);
$ Total + = $ item_array [$ I];
}
$ YY = $ this-> Y-$ this-> border * 2;

// Draw the shadow part of the pie chart
$ E = 0;
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 );
}
$ S = $ E;
$ Leight = $ item_array [$ I]/$ total * 360;
$ E = $ S + $ Leight;
$ Color = imagecolorallocate ($ this-> image, $ R, $ g, $ B );
$ Colorarray [$ I] = $ color;
// Circle
For ($ J = 90; $ j> 70; $ j --) imagefilledarc ($ this-> image, 110, $ J, 200,100, $ S, $ E, $ color, img_arc_pie );
// Imagefilledarc ($ this-> image, 110, 70,200,100, $ S, $ E, $ color, img_arc_pie );
// 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;
}

// Draw the surface of the pie chart
$ E = 0;
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 );
}
$ S = $ E;
$ Leight = $ item_array [$ I]/$ total * 360;
$ E = $ S + $ Leight;
// $ Color = $ colorarray [$ I];
$ Color = imagecolorallocate ($ this-> image, $ R, $ g, $ B );
// Circle
// For ($ J = 90; $ j> 70; $ j --) imagefilledarc ($ this-> image, 110, $ J, 200,100, $ S, $ e, $ color, img_arc_pie );
Imagefilledarc ($ this-> image, 110, 70,200,100, $ S, $ E, $ color, img_arc_pie );
}
}
// -------------- 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-> debug (); // call for debugging
/*
Header ("Content-Type: image/PNG ");
$ Report = new imagereport;
$ Report-> setimage (600,500,255,255,255, 1); // parameter (long, high, back color R, G, B, whether transparent 1 or 0)
$ Temparray = "0,260,400,124, 48,720,122,440,475"; // value, separated by a specified symbol
$ Report-> setitem (',', $ temparray, 3, 23); // parameters (specify the separator between values, numerical variables, style 1 is a vertical bar chart 2 is a horizontal bar chart 3 is a line chart 4 is a pie chart, distance)
$ Report-> setfont (1); // font size: 1-10
// $ Report-> setx (); // set the X axis scale value (start scale value = 1, scale interval value = 1)
$ Report-> printreport ();
*/
?>

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.