Php bar chart

Source: Internet
Author: User

Let's take a look:

Next let's take a look at how php implements the plotting function.

<? Php
/***
* @ Project Bar Graph Program
* @ License GPL
* @ Package
* @ File GrapBar. php
* @ Date 2007-4-3
* @ Version 1.0
* @ Last modified

* Define the column chart class
*
* Note: Make sure that the font path exists and is accessible before use. If an error occurs, check whether the path is in php. open_basedir in ini configuration. If this path is not available, add it or set
*
* Intelligent bar chart program for reports
*
***/

Define ("DEFAULT_FONT_PATH", "c:/windows/fonts/simhei. ttf ");
Class SingleBar
{
Private $ _ x;
Private $ _ y;
Private $ _ h;
Public $ _ l = 50;
Private $ _ w = null;
Private $ _ srcPoints = array ();
Private $ _ points = array ();
 
Public function _ construct ($ x, $ y, $ h, $ l = 50, $ w = null)
{
$ This-> _ x = $ x;
$ This-> _ y = $ y;
$ This-> _ h = $ h;
$ This-> _ l = $ l;
$ This-> _ w = $ w;
$ This-> _ srcPoints = $ this-> getSrcPoints ();
$ This-> _ points = $ this-> getPoints ();
}
 
Public function getSrcPoints ()
{
Return array (
Array ($ this-> _ x, $ this-> _ y ),
Array ($ this-> _ x + $ this-> _ l, $ this-> _ y ),
Array ($ this-> _ x + (1.35 * $ this-> _ l), $ this-> _ y-(0.35 * $ this-> _ l )),
Array ($ this-> _ x + (0.35 * $ this-> _ l), $ this-> _ y-(0.35 * $ this-> _ l )),
Array ($ this-> _ x, $ this-> _ y + $ this-> _ h ),
Array ($ this-> _ x + $ this-> _ l, $ this-> _ y + $ this-> _ h ),
Array ($ this-> _ x + (1.35 * $ this-> _ l ), $ this-> _ y + $ this-> _ h-(0.35 * $ this-> _ l ))
);
}
 
Public function getPoints ()
{
$ Points = array ();
Foreach ($ this-> _ srcPoints as $ key => $ val)
{
$ Points [] = $ val [0];
$ Points [] = $ val [1];
}
Return $ points;
}
 
Public function getTopPoints ()
{
Return array_slice ($ this-> _ points, 0, 8); // top coordinate
}
 
Public function getBelowPoints ()
{
Return array_merge (array_slice ($ this-> _ points, 0, 2), array_slice ($ this-> _ points, 8, 4), array_slice ($ this-> _ points, 2, 2); // coordinates
}
 
Public function getRightSidePoints ()
{
Return array_merge (array_slice ($ this-> _ points, 2, 2), array_slice ($ this-> _ points, 10, 4), array_slice ($ this-> _ points, 4, 2); // coordinates on the right
}
 
Public function draw ($ image, $ topColor, $ belowColor, $ sideColor, $ borderColor = null, $ type = 'left ')
{
If (is_null ($ borderColor ))
{
$ BorderColor = 0 xcccccc;
}

$ Top_rgb = $ this-> getRGB ($ topColor );
$ Below_rgb = $ this-> getRGB ($ belowColor );
$ Side_rgb = $ this-> getRGB ($ sideColor );
$ Top_color = imagecolorallocate ($ image, $ top_rgb ['R'], $ top_rgb ['G'], $ top_rgb ['B']);
$ Below_color = imagecolorallocate ($ image, $ below_rgb ['R'], $ below_rgb ['G'], $ below_rgb ['B']);
$ Side_color = imagecolorallocate ($ image, $ side_rgb ['R'], $ side_rgb ['G'], $ side_rgb ['B']);

Imagefilledpolygon ($ image, $ this-> getTopPoints (), 4, $ top_color); // draw the top surface
Imagepolygon ($ image, $ this-> getTopPoints (), 4, $ borderColor); // draw the top edge

Imagefilledpolygon ($ image, $ this-> getBelowPoints (), 4, $ below_color); // draw the following
Imagepolygon ($ image, $ this-> getBelowPoints (), 4, $ borderColor); // draw the bottom edge

If ($ type = 'left ')
{
Imagefilledpolygon ($ image, $ this-> getRightSidePoints (), 4, $ side_color); // draw the right side
Imagepolygon ($ image, $ this-> getRightSidePoints (), 4, $ borderColor); // draw side Edges
}
}
 
Public function getRGB ($ color)
{
$ Ar = array ();
$ Color = hexdec ($ color );
$ Ar ['R'] = ($ color> 16) & 0xff;
$ Ar ['G'] = ($ color> 8) & 0xff;
$ Ar ['B'] = ($ color) & 0xff;
Return $ ar;
}
}

Class Bar
{
Private $ _ W;
Private $ _ H;
Private $ _ bgColor = "ffffff ";
Private $ _ barHeights = array ();
Private $ _ barTexts = array ();
Private $ _ barColors = array ();
Public $ _ title;
Public $ _ paddingTop = 30;
Public $ _ paddingBottom = 100;
Public $ _ paddingLeft = 45;
Public $ _ paddingRight = 2;
Public $ _ barL = 50;
Public $ image;
 
Public function _ construct ($ imgW, $ imgH, $ barHeights, $ barTexts = null, $ barColors = null)
{
$ This-> _ W = $ imgW;
$ This-> _ H = $ imgH;
$ This-> _ barHeights = $ barHeights;
$ This-> _ barTexts = $ barTexts;
$ This-> _ barColors = $ barColors;
$ This-> _ paddingBottom = $ this-> resetPaddingBottom ();
$ This-> _ H = $ this-> resetHeight ();
$ This-> image = imagecreatetruecolor ($ this-> _ W, $ this-> _ H );
}
 
Public function stroke ()
{
$ This-> drawBg ();
$ This-> drawBars ();
$ This-> drawTitle ();
$ This-> drawLables ();
Ob_start ();
// Header ("Content-type: image/png ");
// Imagepng ($ this-> image );
Header ("Content-type:". image_type_to_mime_type (IMAGETYPE_JPEG ));
Imagejpeg ($ this-> image );
Imagedestroy ($ this-> image );
}
 
Public function drawBg ()
{
$ Img_w = $ this-> _ W;
$ Img_h = $ this-> _ H;
$ PaddingTop = $ this-> _ paddingTop;
$ PaddingBottom = $ this-> _ paddingBottom;
$ PaddingLeft = $ this-> _ paddingLeft;
$ PaddingRight = $ this-> _ paddingRight;
$ Rgb = $ this-> getRGB ($ this-> _ bgColor );
$ Bg = imagecolorallocate ($ this-> image, $ rgb ['R'], $ rgb ['G'], $ rgb ['B']);
Imagefilledrectangle ($ this-> image, 0, 0, $ img_w, $ img_h, $ bg );
$ Side_bg = imagecolorallocatealpha ($ this-> image, 220,220,220, 75 );
$ Side_bg2 = imagecolorallocate ($ this-> image, 220,220,220 );
$ Border_color = imagecolorallocate ($ this-> image, 190,190,190 );
$ Line_color = imagecolorallocate ($ this-> image, 236,236,236 );
$ Dial_color = imagecolorallocate ($ this-> image, 131,131,131 );

$ X1 = $ paddingLeft;
$ Y1 = $ paddingTop;
$ X2 = $ img_w-$ paddingRight;
$ Y2 = $ img_h-$ paddingBottom;
Imagerectangle ($ this-> image, $ x1, $ y1, $ x2, $ y2, $ border_color );
Imagefilledpolygon ($ this-> image, array ($ x1-5, $ y1 + 10, $ x1-5, $ y2 + 10, $ x1, $ y2, $ x1, $ y1 ), 4, $ side_bg );
Imagepolygon ($ this-> image, array ($ x1-5, $ y1 + 10, $ x1-5, $ y2 + 10, $ x1, $ y2, $ x1, $ y1 ), 4, $ border_color );
Imagefilledpolygon ($ this-> image, array ($ x1-5, $ y2 + 10, $ x2-5, $ y2 + 10, $ x2, $ y2, $ x1, $ y2 ), 4, $ side_bg );
Imagepolygon ($ this-> image, array ($ x1-5, $ y2 + 10, $ x2-5, $ y2 + 10, $ x2, $ y2, $ x1, $ y2 ), 4, $ border_color );
Imageline ($ this-> image, $ x1, $ y2, $ x2, $ y2, $ side_bg2 );

$ Total_h = $ img_h-$ paddingTop-$ paddingBottom;
$ Every_h = $ total_h/11;
For ($ I = 1; $ I <= 10; $ I ++)
{
Imageline ($ this-> image, $ x1, $ y1 + ($ every_h * $ I), $ x2, $ y1 + ($ every_h * $ I), $ line_color ); // draw a network cable
}

$ Max_h = max ($ this-> _ barHeights );
For ($ I = 1; $ I <= 10; $ I ++)
{
$ Value = $ max_h-($ max_h/10) * ($ i-1 ));
$ Value = strval ($ value );
$ Str_w = strlen ($ value) * 5;
Imageline ($ this-> image, $ x1-5-3, $ y1 + 10 + ($ every_h * $ I), $ x1-3 + 1, $ y1 + 10 + ($ every_h * $ I), $ dial_color); // draw a dial line
Imagestring ($ this-> image, 3, $ x1-5-3-$ str_w-1, $ y1 + 10 + ($ every_h * $ I)-5, $ value, 0x000000 );
}
}
 
 
Public function drawBars ()
{
$ Counts = count ($ this-> _ barHeights );
If (empty ($ this-> _ barColors ))
{
$ Color = $ this-> setColor ();
$ This-> _ barColors = array_slice ($ color, 0, $ counts );
// Shuffle ($ this-> _ barColors );
}
$ Every_w = ($ this-> _ W-$ this-> _ paddingLeft-$ this-> _ paddingRight)/$ counts; // width of each segment
$ BarL = $ every_w;
$ BarL = ($ barL> $ this-> _ barL * 1.35 + 6 )? $ This-> _ barL: $ barL/1.35-6;
$ Max_h = max ($ this-> _ barHeights );
$ Ruler_h = $ this-> _ H-$ this-> _ paddingTop-$ this-> _ paddingBottom; // The overall height of the ruler.
$ Stander_h = $ ruler_h-($ ruler_h/11); // height of the scale of 10 equal points
$ I = 0;
Foreach ($ this-> _ barHeights as $ val)
{
$ H = ($ stander_h/$ max_h) * $ val;
$ X = $ this-> _ paddingLeft + ($ every_w * $ I) + ($ every_w-($ barL * 1.35)/2 );;
$ Y = $ this-> _ H-$ this-> _ paddingBottom + 10-$ h;
// $ T_color = $ this-> _ barColors [$ I];
$ B _color = $ this-> _ barColors [$ I];
// $ S_color = $ this-> _ barColors [$ I];


$ Rgb = $ this-> getRGB ($ this-> _ barColors [$ I]);
$ R = $ rgb ['R'] * 0.7;
$ G = $ rgb ['G'] * 0.7;
$ B = $ rgb ['B'] * 0.7;

$ C1 = $ R> 0? Dechex ($ R): '00 ';
$ C2 = $ G> 0? Dechex ($ G): '00 ';
$ C3 = $ B> 0? Dechex ($ B): '00 ';

$ T_color = $ B _color;
$ S_color = $ c1. $ c2. $ c3;

$ SingleBar = new SingleBar ($ x, $ y, $ h, $ barL );
$ SingleBar-> draw ($ this-> image, $ t_color, $ B _color, $ s_color );
$ I ++;
}
}
 
Public function drawTitle ()
{
If (empty ($ this-> _ title ))
{
Return;
}
$ Font = 5;
$ Font_w = imagefontwidth ($ font );
$ Len = strlen ($ this-> _ title );
$ X = ($ this-> _ W + $ this-> _ paddingLeft-$ this-> _ paddingRight)/2;
$ X-= ($ len * $ font_w)/2;
$ Y = ($ this-> _ paddingTop-$ font_w)/2 + 12;
// Imagestring ($ this-> image, $ font, $ x, $ y, $ title, 0x000000 );
Imagettftext ($ this-> image, 12, 0, $ x, $ y, 0x000000, DEFAULT_FONT_PATH, $ this-> _ title );
}
 
Public function drawLables ()
{
$ X1 = $ this-> _ paddingLeft-5;
$ Y1 = $ this-> _ H-$ this-> _ paddingBottom + 20;
$ X2 = $ this-> _ W-$ this-> _ paddingRight;
$ Y2 = $ this-> _ H-10;
// Imagefilledrectangle ($ this-> image, $ x1, $ y1, $ x2, $ y2, 0 xffffff );
Imagerectangle ($ this-> image, $ x1, $ y1, $ x2, $ y2, 0x000000 );
$ Space = 5;
$ X = $ x1 + 3;
$ Y = $ y1 + 3;
Foreach ($ this-> _ barTexts as $ key => $ val)
{
$ Color = $ this-> _ barColors [$ key];
$ Rgb = $ this-> getRGB ($ color );
$ Bg = imagecolorallocate ($ this-> image, $ rgb ['R'], $ rgb ['G'], $ rgb ['B']);
Imagefilledrectangle ($ this-> image, $ x, $ y, $ x + 12, $ y + 12, $ bg); // draw a rectangle of 12*12
Imagerectangle ($ this-> image, $ x, $ y, $ x + 12, $ y + 12, 0x000000); // draw a rectangle of 12*12
Imagettftext ($ this-> image, 12, 0, $ x + 12 + 3, $ y + 12, 0x000000, DEFAULT_FONT_PATH, $ val );
$ X + = 12 + $ space + (strlen ($ val) * 8) + $ space;
If ($ x + (strlen ($ val) * 8) >=$ this-> _ W-$ this-> _ paddingLeft-$ this-> _ paddingRight)
{
$ X = $ x1 + 3;
$ Y = $ y + 12 + 3;
}
}
}
 
Public function resetPaddingBottom ()
{
$ Ruler_w = $ this-> _ W-$ this-> _ paddingLeft-$ this-> _ paddingRight;
$ Label_w = $ this-> getLableTotalWidth ();
$ Lines = ceil ($ label_w/$ ruler_w );
$ H = 12 * $ lines + (3 * ($ lines + 1) + 30;
Return $ h;
}
 
Public function resetHeight ()
{
$ Padding_bottom = $ this-> resetPaddingBottom ();
If ($ this-> _ H-$ padding_bottom <222)
{
Return 222 + $ padding_bottom;
}
Return $ this-> _ H;
}

 
Public function getLableTotalWidth ()
{
$ Counts = count ($ this-> _ barTexts );
$ Space = 5;
$ Total_len = 0;
Foreach ($ this-> _ barTexts as $ val)
{
$ Total_len + = strlen ($ val );
}

$ Tx_w = ($ total_len * 9) + (12 + 3 + $ space) * $ counts );
Return $ tx_w;
}
 
Public function setBg ($ color)
{
$ This-> _ bgColor = $ color;
}
 
Public function setTitle ($ title)
{
$ This-> _ title = $ title;
}
 
Public function setColor ()
{
$ Ar = array ('ff ', '00', '33', '66', '99', 'cc ');
$ Color = array ();
For ($ I = 0; $ I <6; $ I ++)
{
For ($ j = 0; $ j <6; $ j ++)
{
For ($ k = 0; $ k <6; $ k ++)
{
$ Color [] = $ ar [$ I]. $ ar [$ j]. $ ar [$ k];
}
}
}

$ Color2 = array ();
For ($ I = 1; $ I <216; $ I + = 4)
{
$ Color2 [] = $ color [$ I];
}

Return $ color2;
}
 
Public function getRGB ($ color)
{
$ Ar = array ();
$ Color = hexdec ($ color );
$ Ar ['R'] = ($ color> 16) & 0xff;
$ Ar ['G'] = ($ color> 8) & 0xff;
$ Ar ['B'] = ($ color) & 0xff;
Return $ ar;
}
}

/***/
$ Bar = new Bar (500,300, array (600,300, 30,500,400,250,350,360), array ('aaaaa', 'bbbbbb', 'cccccc', 'ddddd ', 'eeeeee ', 'ffffff', 'ggggggggg', 'hhhhhhhhh '));
$ Bar-> setTitle ('creates a perfect bar chart! ');
$ Bar-> stroke ();
/***/
?>


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.