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

Source: Internet
Author: User
Use the GD image library to generate the class of the line chart of the horizontal and vertical column chart. read the class that uses the GD image library to generate the line chart of the horizontal and vertical column chart, classImageReport {var $ X; // Image Size X axis var $ Y; // image size Y axis var $ R; // background color R value var $ G; //... g. var

A recently written GD Library is a class library used to generate horizontal and vertical column charts and line charts. it is a teaching routine.
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
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
Function PrintReport (){
Header ("Content-type: image/gif ");
// 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;
}
$ 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); // x axis
ImageLine ($ this-> IMAGE, $ this-> BORDER, $ this-> Y-$ this-> BORDER, $ this-> X-$ this-> BORDER, $ this-> Y-$ this-> BORDER, $ color); // Y axis
// Scale up on the 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 up on the 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 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 );
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, $ 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;
// Used for interval
$ Xx = $ xx + $ this-> BORDER;
}
}

// -------------- Print the graphic http://knowsky.com
Function printAll (){
ImageGIF ($ this-> IMAGE );
ImageDestroy ($ this-> IMAGE );
}
// -------------- Debug
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 (length, width, background 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"; // value, separated by a specified symbol
$ Report-> setItem (',', $ temparray, 3,20); // parameter (separates the specified symbol of the value, numerical variable, and style 1 is a line chart with the vertical column as the horizontal column, 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.