In PHP, use GD to draw a pie chart and gd to draw a pie _ PHP Tutorial

Source: Internet
Author: User
Use GD to draw a pie chart and gd to draw a pie chart. Use GD to draw a pie chart in PHP and gd to draw a pie chart in PHP. for the class to be drawn, see the code: 1 ClassChart {2 private $ image; define image 3 private $ title; define Title 4 priv PHP use GD to draw a pie chart and gd to draw a pie chart

Use GD to draw a pie chart in PHP. for the class to be drawn, see the code:

1 Class Chart {2 private $ image; // defines the image 3 private $ title; // defines the title 4 private $ ydata; // defines the y axis data 5 private $ xdata; // define the x-axis data 6 private $ color; // define the bar chart color 7 private $ bgcolor; // define the image background color 8 private $ width; // define the image width 9 private $ height; // define the image length 10 11/* 12 * constructor 13 * String title image title 14 * Array xdata index Array, X axis data 15 * Array ydata index Array, number Array, Y axis data 16 */17 function _ construct ($ title, $ xdata, $ ydata) {18 $ t His-> title = $ title; 19 $ this-> xdata = $ xdata; 20 $ this-> ydata = $ ydata; 21 $ this-> color = array ('#058DC7', '#50B432', '# ED561B', '# DDDF00', '#24CBE5', '#64E572 ', '# ff9655',' # FFF263 ',' #6AF9C4 '); 22} 23 24/* 25 * public method, set the bar chart color 26 * Array color Array, the element value is '# 058dc7' in this form 27 */28 function setBarColor ($ color) {29 $ this-> color = $ color; 30} 31 32/* 33 * create a pie chart 34 */35 function mkPieChart () {36 $ sum = Array_sum ($ this-> ydata); // Obtain the sum of all ydata elements 37 $ start = 0; // arc start angle 38 $ end = 0; // arc end angle 39 $ pieWidth = 300; // The long axis of the elliptic 40 $ pieHeight = 220; // the short axis of the elliptic 41 $ space = 40; // The distance between the ellipse and the small rectangle is 42 $ margin = 20; // The margin of the image is 43 $ recWidth = 20; // The width of the small rectangle is 44 $ recHeight = 15; // the height of the small rectangle is 45 $ titleHeight = 50; // Title area height 46 // Image adaptive width and height 47 $ this-> width = $ pieWidth + $ this-> arrayLengthMax ($ this-> xdata) * 10*4/3 + $ space + $ recW Idth + $ margin; 48 $ this-> height = ($ pieHeight> count ($ this-> xdata) * 25 )? $ PieHeight: count ($ this-> xdata) * 25) + $ titleHeight; 49 // coordinates of the elliptical center 50 $ cx = $ pieWidth/2 + $ margin; 51 $ cy = $ pieHeight/2 + $ titleHeight; 52 53 $ this-> image = imagecreatetruecolor ($ this-> width, $ this-> height ); // prepare the canvas 54 $ this-> bgcolor = imagecolorallocate ($ this-> image, 255,255,255); // The background color of the image 55 imagefill ($ this-> image, $ this-> bgcolor); // fill in the background 56 57 // set the bar chart color 58 $ color = array (); 59 foreach ($ this -> Color as $ col) {60 $ col = substr ($ col, 1, strlen ($ col)-1); 61 $ red = hexdec (substr ($ col, ); 62 $ green = hexdec (substr ($ col,); 63 $ blue = hexdec (substr ($ col )); 64 $ color [] = imagecolorallocate ($ this-> image, $ red, $ green, $ blue ); 65} 66 67 // set the line segment color, font color, and font path 68 $ lineColor = imagecolorallocate ($ this-> image, 0xcc, 0xcc, 0xcc ); 69 $ fontColor = imagecolorallocate ($ this-> image, 0x95, 0x8f, 0 X8f); 70 $ fontPath = 'font/simsun. ttc '; 71 72 // draw the slice arc 73 for ($ I = 0; $ I <10; $ I ++) {74 foreach ($ this-> ydata as $ key => $ val) {75 $ end + = 360 * $ val/$ sum; 76 imagefilledarc ($ this-> image, $ cx, $ cy-$ I, $ pieWidth, $ pieHeight, $ start, $ end, $ color [$ key % count ($ this-> color)], IMG_ARC_PIE ); 77 $ start = $ end; 78} 79} 80 81 // draw a small rectangle and text description 82 $ x1 = $ pieWidth + $ space; 83 $ y1 = $ titleHeight; 84 foreach ($ this-> yd Ata as $ key => $ val) {85 imagefilledrectangle ($ this-> image, $ x1, $ y1, $ x1 + $ recWidth, $ y1 + $ recHeight, $ color [$ key % count ($ this-> color)]); 86 imagettftext ($ this-> image, 10, 0, $ x1 + $ recWidth + 5, $ y1 + $ recHeight-2, $ fontColor, $ fontPath, $ this-> xdata [$ key]); 87 $ y1 + = $ recHeight + 10; 88} 89 90 // painting title 91 $ titleStart = ($ this-> width-5.5 * strlen ($ this-> title)/2; 92 imagettftext ($ this-> image, 11,0, $ titleStart, 20, $ f OntColor, $ fontPath, $ this-> title); 93 94 // output image 95 header ("Content-Type: image/png "); 96 imagepng ($ this-> image); 97} 98 99/* 100 * Private method, evaluate the maximum element length value in the Array: 101 * Array arr string Array, must be a Chinese character 102 */103 private function arrayLengthMax ($ arr) {104 $ length = 0; 105 foreach ($ arr as $ val) {106 $ length = strlen ($ val)> $ length? Strlen ($ val): $ length; 107} 108 return $ length/3; 109} 110 111 // destructor 112 function _ destruct () {113 imagedestroy ($ this-> image); 114} 115}

The test code is as follows:

1 $ xdata = array ('test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6 ', 'test 7', 'Test 8', 'Test 9'); 2 $ ydata = array (89,90, 90,23, 35,45, 56,23, 56 ); 3 $ Img = new Chart ($ title, $ xdata, $ ydata); 4 $ Img-> mkPieChart ();

As follows:

Use GD in PHP to draw a pie Chart. for the Class to be drawn, see the code: 1 Class Chart {2 private $ image; // define image 3 private $ title; // define the title 4 priv...

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.