) Generate a pie chart in PHP

Source: Internet
Author: User

First, copy php_iconv.dll and inconv. DLL to c: \ winnt \ system32,
<?
Define ("angle_step", 5); // defines the angle step when an elliptical arc is drawn.

Function draw_getdarkcolor ($ IMG, $ CLR) // evaluate the dark color of $ CLR
{
$ RGB = imagecolorsforindex ($ IMG, $ CLR );
Return array ($ RGB ["red"]/2, $ RGB ["green"]/2, $ RGB ["blue"]/2 );
}

Function draw_getexy ($ A, $ B, $ d) // calculates the coordinate of the point on the elliptic corresponding to the angle $ D.
{
$ D = deg 2rad ($ 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 ($ x0, $ y0) = 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, $ x0 + $ ox, $ y0 + $ Oy, $ x + $ ox, $ Y + $ Oy, $ CLR );
$ X0 = $ X;
$ Y0 = $ Y;
}
}

Function draw_sector ($ IMG, $ ox, $ Oy, $ A, $ B, $ SD, $ ed, $ CLR) // draw a fan
{
$ N = Ceil ($ ed-$ SD)/angle_step );
$ D = $ SD;
List ($ x0, $ y0) = draw_getexy ($ A, $ B, $ D );
Imageline ($ IMG, $ x0 + $ ox, $ y0 + $ 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, $ 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) = 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 surface
{
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) // convert rbg to index color

{
$ R = ($ CLR> 16) & 0xff;
$ G = ($ CLR> 8) & 0xff;
$ B = ($ CLR) & 0xff;
Return imagecolorallocate ($ IMG, $ R, $ g, $ B );
}

// Plot the main function and output the image
// $ Datlst is the data array, $ datlst is the tag array, and $ datlst is the color array.
// The dimensions of the preceding three arrays should be equal.
Function draw_img ($ datlst, $ lablst, $ clrlst, $ A = 250, $ B = 120, $ v = 20, $ font = 10)
{
$ Ox = 5 + $;
$ 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 );

// Convert 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 in the 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 pie
Draw_sector3d ($ IMG, $ ox, $ Oy, $ A, $ B, $ V, $ SD, $ ed, $ clrlst [$ I]); // $ SD, $ ed, $ clrlst [$ I]);

// Draw tags
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, "C: \ winnt \ fonts \ simsun. TTC ", $ Str. ":". $ datlst [$ I]. "(". (round (10000 * ($ datlst [$ I]/$ ToT)/100 ). "% )");
$ Ly + = $ FH + 2;
}

// Output image
Header ("Content-Type: image/PNG ");

// Output the generated image
$ Imgfilename = "../temp/". Time (). ". PNG ";
Imagepng ($ IMG, $ imgfilename );

}

$ Datlst = array (30, 10, 20, 20, 10, 20, 10, 20); // data
$ Lablst = array (" Emy of Science and Technology of China", "Anhui University of Science and Technology", "Tsinghua University", "Peking University", "Nanjing University", "Shanghai University ", "Hehai University", "Sun Yat-sen University"); // tag
$ Clrlst = array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999 );

// Draw
Draw_img ($ datlst, $ lablst, $ clrlst );
?>

Note that the font c: \ winnt \ fonts \ simsun. ttc in the imagettftext Function

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.