- Define ("Angle_step", 5); Defines the angle step when drawing an elliptical arc
- 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 ellipse corresponding to the angle
- {
- $d = Deg2rad ($d);
- Return Array (round ($a *cos ($d)), round ($b *sin ($d)));
- }
- function Draw_arc ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR)//Elliptical arc function
- {
- $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)//Draw 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 fan
- {
- 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 goto Index Color
- {
- $R = ($clr >>16) & 0xFF;
- $G = ($clr >>8) & 0xFF;
- $B = ($CLR) & 0xFF;
- Return Imagecolorallocate ($img, $R, $G, $B);
- }
- Draw the main function and output the picture
- $datLst as 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 =250, $b =120, $v =20, $font =10)
- {
- $ox = 5+ $a;
- $oy = 5+ $b;
- $FW = Imagefontwidth ($font);
- $fh = Imagefontheight ($font);
- $n = count ($datLst);//number of data items
- $w = $a
- $h = $b *2+ $v + ($fh +2) * $n;
- $img = Imagecreate ($w, $h);
- Turn RGB to indexed 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 = $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 Labels
- 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, "./simsun.ttf", $str. ":". $datLst [$i]. " (". (Round (10000* ($datLst [$i]/$tot)/100) "%)");
- $ly + = $fh +2;
- }
- Output graphics
- Header ("Content-type:image/png");
- Output the resulting picture
- $imgFileName = "temp/". Time (). ". PNG ";
- Imagepng ($img, $imgFileName);
- Echo ';
- }
- ?>
Copy Code2. Call Method:
- Require_once ("piefunction.php");
- $datLst = Array (10,110,300); Data
- $labLst = Array ("Positive", "medium rating", "Bad Review"); Label
- $clrLst = Array (0x99ff00, 0xff6666, 0x0099ff);
- Draw_img ($datLst, $labLst, $clrLst);
- ?>
Copy CodeNote: As long as the number of data with the following label, the number of colors consistent can output the correct pie chart effect. |