PHP three-dimensional pie chart implementation code _php instance
Last Update:2017-01-18
Source: Internet
Author: User
After the efforts Pie3d finished, good things to share with you. But the younger brother is the PHP novice, the code may not be refined, hoped that everybody advises together to consummate this procedure. Remember to let me know (estorm@yeah.net)
+------------------------+
| pie3dfun.php//Common Functions |
+------------------------+
Define ("Angle_step", 5);//define the angle step when drawing an elliptical arc
function Chx_getdarkcolor ($img, $CLR) {//$clr the corresponding dark color
$rgb =imagecolorsforindex ($img, $CLR);
return Array ($rgb ["Red"]/2, $rgb ["Green"]/2, $rgb ["Blue"]/2);
}
function chx_getexy ($a, $b, $d) {//$d point coordinates on the corresponding ellipse
$d =deg2rad ($d);
Return Array (round ($a *cos ($d)), round ($b *sin ($d)));
}
function Chx_arc ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR) {//Elliptic arc functions
$n =ceil (($ed-$SD)/angle_step);
$d = $SD;
List ($x 0, $y 0) =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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
$x 0= $x;
$y 0= $y;
}
}
function Chx_sector ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR) {//painting slices
$n =ceil (($ed-$SD)/angle_step);
$d = $SD;
List ($x 0, $y 0) =chx_getexy ($a, $b, $d);
Imageline ($img, $x 0+ $ox, $y 0+ $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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
$x 0= $x;
$y 0= $y;
}
Imageline ($img, $x 0+ $ox, $y 0+ $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
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) {//RBG index color
$R = ($clr >>16) & 0xFF;
$G = ($clr >>8) & 0xFF;
$B = ($CLR) & 0xFF;
Return Imagecolorallocate ($img, $R, $G, $B);
}
?>
+--------------------------+
| pie3d.php//three-dimensional pie chart File |
+--------------------------+
Require ("pie3dfun.php");
$a =150;//elliptic long half axis
$b =50;//elliptic Segment Half axis
$v =20;//The height of the pie
$font =5;//Font
$ox =5+ $a;
$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");/tags
$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);
Turn RGB to index color
for ($i =0; $i
$CLRBK =imagecolorallocate ($img, 0xff,0xff,0xff);
$CLRT =imagecolorallocate ($img, 0x00,0x00,0x00);
Fill 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 round cake
Chx_sector3d ($img, $ox, $oy, $a, $b, $v, $SD, $ed, $clrLst [$i]);/$SD, $ed, $clrLst [$i]);
Draw a label
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 graphics
Header ("Content-type:image/gif");
Imagegif ($IMG);
?>