Php 3-dimensional pie chart implementation

Source: Internet
Author: User

I always wanted to post something. Recently, I just finished php_gd.dll in php4, so I can't wait to do some graphics program. I have seen many examples of php pie charts. I have looked at them all in two dimensions, so I want to make a three-dimensional one. After the efforts to complete pie3d, we will share some good things with you. However, the younger brother is a beginner in php and the Code may not be refined enough. I hope you will give me some advice on how to improve this program. Remember to inform me (estorm@yeah.net)

+ ------------------------ +
| Pie3dfun. php // public function |
+ ------------------------ +

Define ("ANGLE_STEP", 5); // defines the angle step when an elliptical arc is drawn.
Function chx_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 chx_getexy ($ a, $ B, $ d) {// calculates the coordinate of the point on the ellipse corresponding to the angle $ d
$ D = deg 2rad ($ d );
Return array (round ($ a * Cos ($ d), round ($ B * Sin ($ d )));
}

Function chx_arc ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) {// Elliptical Arc function
$ N = ceil ($ ed-$ sd)/ANGLE_STEP );
$ D = $ sd;
List ($ x0, $ y0) = chx_getexy ($ a, $ B, $ d );
For ($ I = 0; $ I <$ n; $ I ++ ){
$ D = ($ d + ANGLE_STEP)> $ ed? $ Ed :( $ d + ANGLE_STEP );
List ($ x, $ y) = chx_getexy ($ a, $ B, $ d );
Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
$ X0 = $ x;
$ Y0 = $ y;
}
}
Function chx_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr) {// draw a fan
$ N = ceil ($ ed-$ sd)/ANGLE_STEP );
$ D = $ sd;
List ($ x0, $ y0) = chx_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) = chx_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) = chx_getexy ($ a/2, $ B/2, ($ d + $ sd)/2 );
Imagefill ($ img, $ x + $ ox, $ y + $ oy, $ clr );
}

Function chx_sector3d ($ img, $ ox, $ oy, $ a, $ B, $ v, $ sd, $ ed, $ clr) {// 3d fan surface
Chx_sector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr );
If ($ sd <180 ){
List ($ R, $ G, $ B) = chx_getdarkcolor ($ img, $ clr );
$ Clr = imagecolorallocate ($ img, $ R, $ G, $ B );
If ($ ed> 180) $ ed = 180;
List ($ sx, $ sy) = chx_getexy ($ a, $ B, $ sd );
$ Sx + = $ ox;
$ Sy + = $ oy;
List ($ ex, $ ey) = chx_getexy ($ a, $ B, $ ed );
$ Ex + = $ ox;
$ Ey + = $ oy;
Imageline ($ img, $ sx, $ sy, $ sx, $ sy + $ v, $ clr );
Imageline ($ img, $ ex, $ ey, $ ex, $ ey + $ v, $ clr );
Chx_arc ($ img, $ ox, $ oy + $ v, $ a, $ B, $ sd, $ ed, $ clr );
List ($ sx, $ sy) = chx_getexy ($ a, $ B, ($ sd + $ ed)/2 );
$ Sy + = $ oy + $ v/2;
$ Sx + = $ ox;
Imagefill ($ img, $ sx, $ sy, $ clr );
}
}

Function chx_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 );
}

?>

+ -------------------------- +
| Pie3d. php // 3D pie chart file |
+ -------------------------- +

Require ("pie3dfun. php ");

$ A = 150; // long half-axis of the ellipse
$ B = 50; // Half Axis of the elliptical segment
$ V = 20; // pie height
$ Font = 5; // font

$ Ox = 5 + $;
$ Oy = 5 + $ B;
$ Fw = imagefontwidth ($ font );
$ Fh = imagefontheight ($ font );


$ DatLst = array (30, 10, 20, 20, 10, 20, 10, 20); // data
$ LabLst = array ("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8 "); // tag
$ ClrLst = array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999 );

$ W = 10 + $ a * 2;
$ H = 10 + $ B * 2 + $ v + ($ fh + 2) * count ($ datLst );

$ Img = imagecreate ($ w, $ h );

// Convert RGB to index color
For ($ I = 0; $ 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
$ Sd = 0;
$ Ed = 0;
$ Ly = 10 + $ B * 2 + $ v;
For ($ I = 0; $ I $ sd = $ ed;
$ Ed + = $ datLst [$ I]/$ tot * 360;
// Draw a pie
Chx_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 );
$ Ly + = $ fh + 2;
}
// Output image
Header ("Content-type: image/gif ");
Imagegif ($ img );

?>

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.