Php to draw a bar chart and a bar chart
Effect:
<? Php/***** @ project Bar Graph Program * @ license GPL * @ package * @ file GrapBar. php * @ date 2007-4-3 * @ version 1.0 * @ last modified * defines the bar chart class ** Note: before use, ensure that the font path exists and allows access. If an error occurs, also check in php. open_basedir in ini configuration. If you do not have this path, add it, or set a bar chart program that contains ** intelligent, used for reports, and other ***/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 ($ 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 )); // coordinate below} public function getRightSidePoints () {return array_merge (array_slice ($ this-> _ points, 2, 2), Rray_slice ($ this-> _ points, 10, 4), array_slice ($ this-> _ points, 4, 2 )); // coordinate 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, $ P_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 top imagepolygon ($ image, $ this-> getTopPoints (), 4, $ borderColor); // draw top edge imagefilledpolygon ($ image, $ thi S-> getBelowPoints (), 4, $ below_color); // draw the following imagepolygon ($ image, $ this-> getBelowPoints (), 4, $ borderColor ); // draw the following edge if ($ type = 'left') {imagefilledpolygon ($ image, $ this-> getRightSidePoints (), 4, $ side_color ); // draw the right side imagepolygon ($ image, $ this-> getRightSidePoints (), 4, $ borderColor); // draw the side edge} public function getRGB ($ color) {$ ar = array (); $ color = hexdec ($ color); $ ar ['R'] = ($ color> 16) & 0 Xff; $ 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; p Ublic 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); $ bor Der_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 an 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-> _ padd IngLeft-$ this-> _ paddingRight)/$ counts; // The 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; // scale height $ stander_h = $ ruler_h-($ ruler_h/11); // scale height of 10 = $ 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_c Olor = $ 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; // imagefilledrecta Ngle ($ 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 12*12. The rectangle imagerectangle ($ this-> image, $ x, $ y, $ x + 12, $ y + 12, 0x000000 ); // draw the 12*12 Rectangular Box 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 = c Ount ($ 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', 'cccccccc', 'ddddd ', 'eeeeeee', 'ffffff', 'gggggggggggg', 'hhhhhhhhh '); $ bar-> setTitle (' creates a perfect bar chart! '); $ Bar-> stroke ();/***/?>