Code for drawing a pie chart using php

Source: Internet
Author: User
This article describes how to use php to draw a pie chart. For more information, see the drawPieImg () function, which contains 8 parameters. $ title is the title of the pie chart; $ dataArr is the data array to be displayed; $ labelArr is the label classification array of the corresponding data; $ colorArr is the drawing color array of the corresponding data. these four parameters are required, for different system applications, pass the corresponding parameters. The following four parameters are used to set the size of the pie chart to be generated. If this parameter is not set, the default value is used. The program draws from 0 degrees based on the size of the array data at the bottom of the bed, and draws the fan size occupied by the corresponding data in clockwise direction.

The code is as follows:


// Variable definition: the angle of the elliptical arc
Define ("ANGLELENGTH", 3 );
/**
* Draw an image
* @ Param $ title the title of a 3D image
* @ Param $ data array displayed by dataArr
* @ Param $ labelArr refers to the tag classification array of the corresponding data.
* @ Param $ colorArr: array of the drawing color
* @ Param $ a baseline width of the canvas
* @ Param $ B baseline height of the canvas
* @ Param $ v height of the 3D column
* @ Param $ font size
* @ Return indicates the access path of the successfully drawn image.
*/
Function drawPieImg ($ title, $ dataArr, $ labelArr, $ colorArr, $ a = 250, $ B = 120, $ v = 20, $ font = 10 ){
$ Ox = 5 + $;
$ Oy = 5 + $ B;
$ Fw = imagefontwidth ($ font );
$ Fh = imagefontheight ($ font );
$ N = count ($ dataArr); // calculates the array length.
$ W = 10 + $ a * 2;
$ H = 10 + $ B * 2 + $ v + ($ fh + 2) * $ n;
// Create a canvas
$ Img = imagecreate ($ w, $ h );
// Convert RGB to index color
For ($ I = 0; $ I <$ n; $ I ++)
$ ColorArr [$ I] = drawIndexColor ($ img, $ colorArr [$ I]); // assign a color to the image $ img
$ Clrbk = imagecolorallocate ($ img, 0xff, 0xff, 0xff );
$ Clrt = imagecolorallocate ($ img, 0x00, 0x00, 0x00 );
// Fill in the background color
Imagefill ($ img, 0, 0, $ clrbk );
// Sum
$ Tot = 0;
For ($ I = 0; $ I <$ n; $ I ++)
$ Tot + = $ dataArr [$ I];
// Start angle of each category
$ Sd = 0;
// The angle of each category
$ Ed = 0;
$ Ly = 10 + $ B * 2 + $ v;
For ($ I = 0; $ I <$ n; $ I ++ ){
$ Sd = $ ed;
$ Ed + = $ dataArr [$ I]/$ tot * 360;
// Draw a 3d fan surface
Draw3DSector ($ img, $ ox, $ oy + 20, $ a, $ B, $ v, $ sd, $ ed, $ colorArr [$ I]);
// Draw tags
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 the image title
Imagettftext ($ img, 15, 0, 5, 15, $ clrt, "D:/wamp/www/source/font/simhei. ttf ", iconv (" GB2312 "," UTF-8 ", $ title ));
// Output image
Header ("Content-type: image/png ");
// Output the generated image
$ ImgFileName = "./". time (). ". png ";
Imagepng ($ img, $ imgFileName );
Return $ imgFileName;
}
/**
* Draw a 3d fan surface
*/
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 );
// Assign color to the image
$ 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 ($ x0, $ y0) = 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, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
$ X0 = $ x;
$ Y0 = $ y;
}
}
/**
* Draw fan surfaces
*/
Function drawSector ($ img, $ ox, $ oy, $ a, $ B, $ sd, $ ed, $ clr ){
$ N = ANGLELENGTH> 0? Ceil ($ ed-$ sd)/ANGLELENGTH):-1;
$ D = $ sd;
List ($ x0, $ y0) = getExy ($ a, $ B, $ d );
Imageline ($ img, $ x0 + $ ox, $ y0 + $ 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, $ x0 + $ ox, $ y0 + $ oy, $ x + $ ox, $ y + $ oy, $ clr );
$ X0 = $ x;
$ Y0 = $ y;
}
Imageline ($ img, $ x0 + $ ox, $ y0 + $ oy, $ ox, $ oy, $ clr );
List ($ x, $ y) = getExy ($ a/2, $ B/2, ($ d + $ sd)/2 );
Imagefill ($ img, $ x + $ ox, $ y + $ oy, $ clr );
}
/**
* Obtain the shadow color of the corresponding column based on 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 );
}
/**
* Calculate the coordinate of the point on the elliptic corresponding to the angle $ d
*
* @ Param $ a abscissa
* @ Param $ B y coordinate
* @ Param $ d angle
* @ Return corresponds to the coordinate of the elliptical dot.
*/
Function getExy ($ a, $ B, $ d ){
$ D = deg 2rad ($ d );
Return array (round ($ a * cos ($ d), round ($ B * sin ($ d )));
}
/**
* Assign an RGB index color to the image
*/
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 the zoo ";
$ DataArr = array (20, 10, 20, 20, 10, 20, 30, 10); // test data array
$ LabelArr = array ("Elephant", "giraffe", "Crocodile", "ostrich", "Tiger", "Lion", "Monkey", "zebra "); // tag
$ ColorArr = array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); // 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.