Bar chart implemented by GD in PHP

Source: Internet
Author: User
In PHP, the column chart implemented by GD is used. in PHP, the column chart implemented by GD is used. you can write a class to draw a column chart and use the code.

1
 Title = $ title; 20 $ this-> xdata = $ xdata; 21 $ this-> ydata = $ ydata; 22 $ this-> color = array ('#058DC7 ', '#50B432', '# ED561B', '# DDDF00', '#24CBE5', '#64E572', '# ff9655',' # fff263 ', '#6AF9C4'); 23} 24 25/* 26 * public method, set the bar chart color 27 * Array color Array, the element value is '# 058dc7' in this format 28 */29 function setBarColor ($ color) {30 $ this-> color = $ color; 31} 32 33/* 34 * public method, bar chart 35 */36 function mkBarChart () {37 $ ydata Num = $ this-> arrayNum ($ this-> ydata); // get the number of data groups 38 $ max = $ this-> arrayMax ($ this-> ydata ); // get the maximum value of all rendered data 39 $ multi = ($ max> 100 )? $ Max/100: 1; // if the maximum data size is greater than 100, compress the data and obtain 40 $ barHeightMulti = 2.2; // the ratio of bar height scaling is 41 $ barWidth = (16-2 * ($ ydataNum-1)> 10? (16-2 * ($ ydataNum-1): 10; // The bar width is 42 $ barSpace = 16; // the spacing between the entries is 43 $ chartLeft = (1 + strlen ($ max) * 12; // Set margin 44 45 $ barY = 250 on the left of the image; // initialize Y coordinate 46 of the bar chart // set the width and height of the image to 47 $ this-> width = ($ ydataNum * $ barWidth + $ barSpace) * count ($ this-> xdata) + $ chartLeft; 48 $ this-> height = 300; 49 $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height); // prepare the canvas 50 $ this-> bgcolor = imagecolorallocate ($ thi S-> image, 255,255,255); // The background color of the image 51 52 // set the bar chart color 53 $ color = array (); 54 foreach ($ this-> color as $ col) {55 $ col = substr ($ col, 1, strlen ($ col)-1 ); 56 $ red = hexdec (substr ($ col,); 57 $ green = hexdec (substr ($ col )); 58 $ blue = hexdec (substr ($ col, 4, 2); 59 $ color [] = imagecolorallocate ($ this-> image, $ red, $ green, $ blue ); 60} 61 62 // set the line segment color, font color, and font path 63 $ lineColor = imagecolorallocate ($ t His-> image, 0xcc, 0xcc, 0xcc); 64 $ fontColor = imagecolorallocate ($ this-> image, 0x95, 0x8f, 0x8f ); 65 $ fontPath = 'font/simsun. ttc '; 66 67 imagefill ($ this-> image, 0, 0, $ this-> bgcolor ); // painting background 68 69 // the short-term and left-side edges of the painting graph 70 for ($ I = 0; $ I <6; $ I ++) {71 imageline ($ this-> image, $ chartLeft-10, $ barY-$ barHeightMulti * $ max/5/$ multi * $ I, $ this-> width, $ barY-$ barHeightMulti * $ max/5/$ multi * $ I, $ lineColor); 72 imagestring ($ This-> image, $ barY-$ barHeightMulti * $ max/5/$ multi * $ I-8, floor ($ max/5 * $ I), $ fontColor ); 73} 74 imageline ($ this-> image, $ chartLeft-10, 30, $ chartLeft-10, $ barY, $ lineColor); 75 imageline ($ this-> image, $ this-> width-1, 30, $ this-> width-1, $ barY, $ lineColor ); 76 77 // The 78 foreach ($ this-> ydata as $ key => $ val) {79 if ($ ydataNum = 1) {80 // 81 $ barX = $ chartLeft + 3 + ($ barWidth + $ barSpace) * $ key; 82 imagefilledrectangle ($ this-> image, $ barX, $ barY-$ barHeightMulti * $ val/$ multi, $ barX + $ barWidth, $ barY, $ color [$ key % count ($ this-> color)]); 83} elseif ($ ydataNum> 1) {84 // when data of multiple generations is 85 $ cbarSpace = $ barSpace + $ barWidth * ($ ydataNum-1); 86 foreach ($ val as $ ckey => $ cval) {87 $ barX = $ chartLeft + 3 + $ barWidth * $ key + $ ckey * ($ cbarSpace + $ barWidth); 88 imagefilledrectangle ($ this-> image, $ barX, $ barY-$ barHeigh TMulti * $ cval/$ multi, $ barX + $ barWidth, $ barY, $ color [$ key % count ($ this-> color)]); 89} 90} 91 92} 93 94 // the x coordinate value of the bar chart is 95 foreach ($ this-> xdata as $ key => $ val) {96 $ barX = $ chartLeft + ($ ydataNum * $ barWidth + $ barSpace) * $ key + $ ydataNum * $ barWidth/3; 97 imagettftext ($ this-> image, 10,-45, $ barX, $ barY + 15, $ fontColor, $ fontPath, $ this-> xdata [$ key]); 98} 99 100 // painting title 101 $ titleStart = ($ this-> width-5.5 * strle N ($ this-> title)/2; 102 imagettftext ($ this-> image, 11, 0, $ titleStart, 20, $ fontColor, $ fontPath, $ this-> title); 103 104 // output image 105 header ("Content-Type: image/png"); 106 imagepng ($ this-> image ); 107} 108 109/* 110 * Private method. when the array is a binary array, the length of the statistical Array is 111 * Array arr. the Array to be counted is 112 */113 private function arrayNum ($ arr) {114 $ num = 0; 115 if (is_array ($ arr )) {116 $ num ++; 117 for ($ I = 0; $ I <count ($ arr); $ I ++) {118 if (is_array ($ Arr [$ I]) {119 $ num = count ($ arr); 120 break; 121} 122} 123} 124 return $ num; 125} 126 127/* 128 * private method, calculate the depth of the Array 129 * Array arr Array 130 */131 private function arrayDepth ($ arr) {132 $ num = 0; 133 if (is_array ($ arr) {134 $ num ++; 135 for ($ I = 0; $ I <count ($ arr); $ I ++) {136 if (is_array ($ arr [$ I]) {137 $ num + = $ this-> arrayDepth ($ arr [$ I]); 138 break; 139} 140} 141} 142 return $ num; 143} 144 145/* 146 * Private method, find a group Maximum value 147 * Array arr numeric Array 148 */149 private function arrayMax ($ arr) {150 $ depth = $ this-> arrayDepth ($ arr); 151 $ max = 0; 152 if ($ depth = 1) {153 rsort ($ arr); 154 $ max = $ arr [0]; 155} elseif ($ depth> 1) {156 foreach ($ arr as $ val) {157 if (is_array ($ val) {158 if ($ this-> arrayMax ($ val)> $ max) {159 $ max = $ this-> arrayMax ($ val); 160} 161} else {162 if ($ val >$ max) {163 $ max = $ val; 164} 165} 166} 167} 168 Return $ max; 169} 170 171 function arrayAver ($ arr) {172 $ aver = array (); 173 foreach ($ arr as $ val) {174 if (is_array ($ val) {175 $ aver = array_merge ($ aver, $ val); 176} else {177 $ aver [] = $ val; 178} 179} 180 return array_sum ($ aver)/count ($ aver); 181 182} 183 // destructor 184 function _ destruct () {185 imagedestroy ($ this-> image); 186} 187} 188?>

This class can be used to draw a series of column charts and multiple series of column charts, as shown below:

Column charts of a series

















Column charts of multiple series

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.