Pie chart
Copy Code code as follows:
<?php
//+------------------------+
//| Pie3dfun. php//Common Functions |
//+------------------------+
Define ("Angle_step", 3); Defines the angle step when drawing an elliptical arc
Define ("font_used", "C:\WINDOWS\Fonts\simhei.ttf"); The font file location to use
function Draw_getdarkcolor ($img, $CLR)//$CLR the corresponding dark color
{
$rgb = Imagecolorsforindex ($img, $CLR);
return Array ($rgb ["Red"]/2, $rgb ["Green"]/2, $rgb ["Blue"]/2);
}
function draw_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 Draw_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) = 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, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
$x 0 = $x;
$y 0 = $y;
}
}
function Draw_sector ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR)//painting slices
{
$n = Ceil (($ed-$SD)/angle_step);
$d = $SD;
List ($x 0, $y 0) = draw_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) = Draw_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) = 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 slices
{
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)//RBG index Color
{
$R = ($clr >>16) & 0xFF;
$G = ($clr >>8) & 0xFF;
$B = ($CLR) & 0xFF;
Return Imagecolorallocate ($img, $R, $G, $B);
}
Drawing main function, and output picture
$datLst is an array of data, $datLst as an array of labels, $datLst as an array of colors
The dimensions of the above three arrays should be equal
function draw_img ($datLst, $labLst, $clrLst, $a =200, $b =90, $v =20, $font =10)
{
$ox = 5+ $a;
$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);
Turn 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 background color
Imagefill ($img, 0, 0, $CLRBK);
Sum
$tot = 0;
for ($i =0; $i < $n; $i + +)
$tot + + $datLst [$i];
$SD = 0;
$ed = 0;
$ly = 10+ $b *2+ $v;
for ($i =0; $i < $n; $i + +)
{
$SD = $ed;
$ed + + $datLst [$i]/$tot *360;
Draw a round cake
Draw_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);
$str = Iconv ("GB2312", "UTF-8", $labLst [$i]);
Imagettftext ($img, $font, 0, 5+2* $FW, $ly +13, $CLRT, font_used, $str. ":". $datLst [$i]. " (". (Round (10000* ($datLst [$i]/$tot)/100). "%)");
$ly + + $fh +2;
}
Output graphics
Header ("Content-type:image/png");
Output generated pictures
Imagepng ($IMG);
}
$datLst = Array (30, 20, 20, 20, 10, 20, 10, 20); Data
$labLst = Array ("Zhejiang province", "Guangdong province", "Shanghai City", "Beijing", "Fujian province", "Jiangsu province", "Hubei province", "Anhui province"); Label
$clrLst = Array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999);
Drawing
Draw_img ($datLst, $labLst, $clrLst);
?>