// + ------------------------ + // | Pie3dfun. PHP // public function | // + ------------------------ + Define ("ANGLE_STEP", 5); // defines the angle step when an elliptical arc is drawn. Function draw_getdarkcolor ($ img, $ clr) // Evaluate the dark color of $ clr { $ Rgb = imagecolorsforindex ($ img, $ clr ); Return array ($ rgb ["red"]/2, $ rgb ["green"]/2, $ rgb ["blue"]/2 ); } Function draw_getexy ($ a, $ B, $ d) // calculates the coordinate of the point on the elliptic corresponding to the angle $ d. { $ D = deg 2rad ($ d ); Return array (round ($ a * Cos ($ d), round ($ B * Sin ($ d ))); } Function draw_arc ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) // elliptical arc function { $ N = ceil ($ ed-$ sd)/ANGLE_STEP ); $ D = $ sd; List ($ x0, $ y0) = draw_getexy ($ a, $ B, $ d ); For ($ I = 0; $ I <$ n; $ I ++) { $ D = ($ d + ANGLE_STEP)> $ ed? $ Ed :( $ d + ANGLE_STEP ); List ($ x, $ y) = draw_getexy ($ a, $ B, $ d ); Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr ); $ X0 = $ x; $ Y0 = $ y; } } Function draw_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) // draw a fan { $ N = ceil ($ ed-$ sd)/ANGLE_STEP ); $ D = $ sd; List ($ x0, $ y0) = draw_getexy ($ a, $ B, $ d ); Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr ); For ($ I = 0; $ I <$ n; $ I ++) { $ D = ($ d + ANGLE_STEP)> $ ed? $ Ed :( $ d + ANGLE_STEP ); List ($ x, $ y) = draw_getexy ($ a, $ B, $ d ); Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr ); $ X0 = $ x; $ Y0 = $ y; } Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr ); List ($ x, $ y) = draw_getexy ($ a/2, $ B/2, ($ d + $ sd)/2 ); Imagefill ($ img, $ x + $ ox, $ y + $ oy, $ clr ); } Function draw_sector3d ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clr) // 3d fan surface { Draw_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr ); If ($ sd <180) { List ($ R, $ G, $ B) = draw_getdarkcolor ($ img, $ clr ); $ Clr = imagecolorallocate ($ img, $ R, $ G, $ B ); If ($ ed> 180) $ ed = 180; List ($ sx, $ sy) = draw_getexy ($ a, $ B, $ sd ); $ Sx + = $ ox; $ Sy + = $ oy; List ($ ex, $ ey) = draw_getexy ($ a, $ B, $ ed ); $ Ex + = $ ox; $ Ey + = $ oy; Imageline ($ img, $ sx, $ sy, $ sx, $ sy + $ v, $ clr ); Imageline ($ img, $ ex, $ ey, $ ex, $ ey + $ v, $ clr ); Draw_arc ($ img, $ ox, $ oy + $ v, $ a, $ B, $ sd, $ ed, $ clr ); List ($ sx, $ sy) = draw_getexy ($ a, $ B, ($ sd + $ ed)/2 ); $ Sy + = $ oy + $ v/2; $ Sx + = $ ox; Imagefill ($ img, $ sx, $ sy, $ clr ); } } Function draw_getindexcolor ($ img, $ clr) // Convert RBG to index color { $ R = ($ clr> 16) & 0xff; $ G = ($ clr> 8) & 0xff; $ B = ($ clr) & 0xff; Return imagecolorallocate ($ img, $ R, $ G, $ B ); } // Plot the main function and output the image // $ DatLst is the data array, $ datLst is the tag array, and $ datLst is the color array. // The dimensions of the preceding three arrays should be equal. Function draw_img ($ datLst, $ labLst, $ clrLst, $ a = 250, $ B = 120, $ v = 20, $ font = 10) { $ Ox = 5 + $; $ Oy = 5 + $ B; $ Fw = imagefontwidth ($ font ); $ Fh = imagefontheight ($ font ); $ N = count ($ datLst); // number of data items $ W = 10 + $ a * 2; $ H = 10 + $ B * 2 + $ v + ($ fh + 2) * $ n; $ Img = imagecreate ($ w, $ h ); // Convert RGB to index color For ($ I = 0; $ I <$ n; $ I ++) $ ClrLst [$ I] = draw_getindexcolor ($ img, $ clrLst [$ I]); $ Clrbk = imagecolorallocate ($ img, 0xff, 0xff, 0xff ); $ Clrt = imagecolorallocate ($ img, 0x00, 0x00, 0x00 ); // Fill in the background color Imagefill ($ img, 0, 0, $ clrbk ); // Sum $ Tot = 0; For ($ I = 0; $ I <$ n; $ I ++) $ Tot + = $ datLst [$ I]; $ Sd = 0; $ Ed = 0; 333 $ Ly = 10 + $ B * 2 + $ v; For ($ I = 0; $ I <$ n; $ I ++) { $ Sd = $ ed; $ Ed + = $ datLst [$ I]/$ tot * 360; // Draw a pie Draw_sector3d ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clrLst [$ I]); // $ sd, $ ed, $ clrLst [$ I]); // Draw tags Imagefilledrectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ clrLst [$ I]); Imagerectangle ($ img, 5, $ ly, 5 + $ fw, $ ly + $ fh, $ clrt ); // Imagestring ($ img, $ font, 5 + 2 * $ fw, $ ly, $ labLst [$ I]. ":". $ datLst [$ I]. "(". (round (10000 * ($ datLst [$ I]/$ tot)/100 ). "%)", $ clrt ); $ Str = iconv ("GB2312", "UTF-8", $ labLst [$ I]); ImageTTFText ($ img, $ font, 0, 5 + 2 * $ fw, $ ly + 13, $ clrt ,". /simsun. ttf ", $ str. ":". $ datLst [$ I]. "(". (round (10000 * ($ datLst [$ I]/$ tot)/100 ). "% )"); $ Ly + = $ fh + 2; } // Output image Header ("Content-type: image/png "); // Output the generated image $ ImgFileName = "../temp/". time (). ". png "; Imagepng ($ img, $ imgFileName ); Echo ''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''' } $ DatLst = array (30, 10, 20, 20, 10, 20, 10, 20); // data $ LabLst = array (" Emy of Science and Technology of China", "Anhui University of Science and Technology", "Tsinghua University", "Peking University", "Nanjing University", "Shanghai University ", "Hehai University", "Sun Yat-sen University"); // tag $ ClrLst = array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999 ); // Draw Draw_img ($ datLst, $ labLst, $ clrLst ); ?> |