Using PHP to draw a pie chart implementation code _php tutorial

Source: Internet
Author: User
The Drawpieimg () function consists of 8 parameters, $title as the title of the pie chart, $dataArr as an array of data to be displayed, an array of labels for the corresponding data, and $COLORARR as an array of drawing colors for the corresponding data, which are required for the 4 parameters. For different system applications, the corresponding parameters can be passed. The next 4 parameters, which are responsible for setting the size of the pie chart to be generated, use the system default if not set. The program is drawn from 0 degrees according to the size of the bed base group data, and the direction is drawn in clockwise direction to the size of the slice that the corresponding data occupies.
Copy CodeThe code is as follows:
Variable definition, angle size when drawing an elliptical arc
Define ("Anglelength", 3);
/**
* Draw Pictures
* @param $title title of 3D chart
* @param array of data $dataArr displayed
* @param $labelArr array of label categories for the corresponding data
* @param $colorArr An array of drawing colors
* @param the base width of the $a canvas
* @param the base height of the $b 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 = $a
$h = $b *2+ $v + ($fh +2) * $n;
Create Artboards
$img = Imagecreate ($w, $h);
Turn RGB to indexed color
for ($i =0; $i < $n; $i + +)
$COLORARR [$i] = Drawindexcolor ($img, $COLORARR [$i]);//assigning colors 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 angle size occupied by each category
$ed = 0;
$ly = $b *2+ $v;
for ($i =0; $i < $n; $i + +) {
$SD = $ed;
$ed + = $DATAARR [$i]/$tot *360;
Draw 3d Slices
Draw3dsector ($img, $ox, $oy +20, $a, $b, $v, $SD, $ed, $COLORARR [$i]);
Draw Labels
Imagefilledrectangle ($img, 5, $ly, 5+ $fw, $ly + $fh, $COLORARR [$i]);
Imagerectangle ($img, 5, $ly, 5+ $fw, $ly + $fh, $CLRT);
Chinese transcoding
$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 the resulting picture
$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 Elliptical arcs
*/
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 shadow Color of corresponding column according to $CLR color
* @param $img Images
* @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);
}
/**
* Point coordinates on the ellipse corresponding to the angle $d
*
* @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 example
$title = "Distribution of animal species in zoos";
$DATAARR = Array (20, 10, 20, 20, 10, 20, 30, 10); Array of test data
$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 "";
?>

http://www.bkjia.com/PHPjc/327499.html www.bkjia.com true http://www.bkjia.com/PHPjc/327499.html techarticle The drawpieimg () function contains 8 parameters, $title the title of the pie chart, $dataArr as an array of data to be displayed , an array of labels for the corresponding data, and a $COLORARR for the corresponding data.

  • 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.