Implementation of the php3 pie chart. 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.
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 );
?>
Bytes. 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, and I have looked at them all in two dimensions...