Using PHP to draw a pie chart implementation code _php skills

Source: Internet
Author: User
Tags array length
The drawpieimg () function contains 8 parameters, $title the title of the pie chart, $dataArr the array of data to display, $LABELARR the array of labels for the corresponding data, and $COLORARR the array of drawing colors for the corresponding data, which are required. For different system applications to pass the corresponding parameters can be. The next 4 parameters are responsible for setting the size of the pie chart to be generated, and using system defaults if not set. The program is drawn from 0 degrees according to the size of the bed base group data, and the direction is plotted in the clockwise direction to draw the corresponding slice size of the data occupied.
Copy Code code as follows:

<?php
Variable definition, angle size when drawing an elliptical arc
Define ("Anglelength", 3);
/**
* Drawing Pictures
* @param $title The title of the 3D diagram
* @param array of data $DATAARR display
* @param an array of label categories $LABELARR corresponding data
* @param $colorArr an array of corresponding drawing colors
* @param the base width of the $a canvas
* @param $b The base height of the canvas
* @param $v height of 3D column
* @param $font Font size
* @return Draw a successful picture access path
*/
function drawpieimg ($title, $DATAARR, $LABELARR, $COLORARR, $a =250, $b =120, $v =20, $font =10) {
$ox = 5+ $a;
$oy = 5+ $b;
$FW = Imagefontwidth ($font);
$fh = Imagefontheight ($font);
$n = count ($DATAARR);//Calculate array length
$w = 10+ $a *2;
$h = 10+ $b *2+ $v + ($fh +2) * $n;
Create Artboards
$img = Imagecreate ($w, $h);
Turn RGB to index color
for ($i =0; $i < $n; $i + +)
$COLORARR [$i] = Drawindexcolor ($img, $COLORARR [$i]);//Assign color to image $img
$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 + + $dataArr [$i];
Starting angle size for each category
$SD = 0;
The size of the angle each category occupies
$ed = 0;
$ly = 10+ $b *2+ $v;
for ($i =0; $i < $n; $i + +) {
$SD = $ed;
$ed + + $dataArr [$i]/$tot *360;
Painting 3d slices
Draw3dsector ($img, $ox, $oy +20, $a, $b, $v, $SD, $ed, $COLORARR [$i]);
Draw a label
Imagefilledrectangle ($img, 5, $ly, 5+ $fw, $ly + $fh, $COLORARR [$i]);
Imagerectangle ($img, 5, $ly, 5+ $fw, $ly + $fh, $CLRT);
Chinese transfer Code
$str = Iconv ("GB2312", "UTF-8", $labelArr [$i]);
Imagettftext ($img, $font, 0, 5+2* $FW, $ly +13, $CLRT, "D:/wamp/www/source/font/simhei.ttf", $str. ":" $dataArr [$i]. " (". (Round (10000* ($dataArr [$i]/$tot)/100). "%)");
$ly + + $fh +2;
}
Draw a picture title
Imagettftext ($img, 0, 5, $CLRT, "D:/wamp/www/source/font/simhei.ttf", Iconv ("GB2312", "UTF-8", $title));
Output graphics
Header ("Content-type:image/png");
Output generated pictures
$imgFileName = "./". Time (). ". PNG ";
Imagepng ($img, $imgFileName);
return $imgFileName;
}
/**
* Draw 3d Slices
*/
function Draw3dsector ($img, $ox, $oy, $a, $b, $v, $SD, $ed, $CLR) {
Drawsector ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR);
if ($sd <180) {
List ($red, $green, $blue) = Drawdarkcolor ($img, $CLR);
assigning colors to Images
$CLR =imagecolorallocate ($img, $red, $green, $blue);
if ($ed >180)
$ed = 180;
List ($SX, $sy) = Getexy ($a, $b, $SD);
$SX + + $ox;
$sy + + $oy;
List ($ex, $ey) = Getexy ($a, $b, $ed);
$ex + + $ox;
$ey + + $oy;
Imageline ($img, $SX, $sy, $SX, $sy + $v, $CLR);
Imageline ($img, $ex, $ey, $ex, $ey + $v, $CLR);
DrawArc ($img, $ox, $oy + $v, $a, $b, $SD, $ed, $CLR);
List ($SX, $sy) = Getexy ($a, $b, ($SD + $ed)/2);
$sy + + $oy + $v/2;
$SX + + $ox;
Imagefill ($img, $SX, $sy, $CLR);
}
}
/**
* Draw an elliptical arc
*/
function DrawArc ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR) {
$n = Anglelength >0? Ceil (($ed-$SD)/anglelength):-1;
$d = $SD;
List ($x 0, $y 0) = getexy ($a, $b, $d);
for ($i =0; $i < $n; $i + +) {
$d = ($d +anglelength) > $ed $ed:($d +anglelength);
List ($x, $y) = Getexy ($a, $b, $d);
Imageline ($img, $x 0+ $ox, $y 0+ $oy, $x + $ox, $y + $oy, $CLR);
$x 0 = $x;
$y 0 = $y;
}
}
/**
* Draw Slices
*/
function Drawsector ($img, $ox, $oy, $a, $b, $SD, $ed, $CLR) {
$n = anglelength > 0? Ceil (($ed-$SD)/anglelength):-1;
$d = $SD;
List ($x 0, $y 0) = getexy ($a, $b, $d);
Imageline ($img, $x 0+ $ox, $y 0+ $oy, $ox, $oy, $CLR);
for ($i =0; $i < $n; $i + +) {
$d = ($d +anglelength) > $ed $ed:($d +anglelength);
List ($x, $y) = 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) = Getexy ($a/2, $b/2, ($d + $sd)/2);
Imagefill ($img, $x + $ox, $y + $oy, $CLR);
}
/**
* Get the shadow Color of the corresponding column according to the $CLR color
* @param $img image
* @param $clr Color
* @return RGB Color Array
*/
function Drawdarkcolor ($img, $CLR) {
$rgb = Imagecolorsforindex ($img, $CLR);
return Array ($rgb ["Red"]/2, $rgb ["Green"]/2, $rgb ["Blue"]/2);
}
/**
* $d the point coordinates on the ellipse corresponding to the angle
*
* @param $a horizontal axis
* @param $b Ordinate
* @param $d Angle
* @return corresponding ellipse point coordinates
*/
function getexy ($a, $b, $d) {
$d = Deg2rad ($d);
Return Array (round ($a *cos ($d)), round ($b *sin ($d)));
}
/**
* Assign RGB index colors to images
*/
function Drawindexcolor ($img, $CLR) {
$red = ($clr >>16) & 0xFF;
$green = ($clr >>8) & 0xFF;
$blue = ($CLR) & 0xFF;
Return Imagecolorallocate ($img, $red, $green, $blue);
}
Test samples
$title = "Zoo animal species distribution";
$DATAARR = Array (20, 10, 20, 20, 10, 20, 30, 10); Test Data array
$LABELARR = Array ("Elephant", "giraffe", "crocodile", "ostrich", "Tiger", "Lion", "monkey", "zebra");/label
$COLORARR = Array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); corresponding color array
$result = Drawpieimg ($title, $DATAARR, $LABELARR, $COLORARR);
echo " ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.