After the efforts to complete pie3d, we will share some good things with you. But the younger brother is new to PhP, Code It may not be refined enough. I hope you can help us 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 );
?>