Graphics
<?php
Class gd{
/*
Function description
$data: Y-axis data (array)
$graphdata: Y-axis data--percent (array)
$label: X-axis data (array)
$height: Image Height
$width: Image width
$font: Font Size
$dot: Determining the size of a point
$BG: Background color
$line: Line Color
$text: Text color
$dotcolor: Point Color
$file: Output image file name
*/
Functions of linear graphs
function Qximage ($data, $graphdata, $label, $height, $width, $font, $dot, $BG, $line, $text
{
$JC = $height/100;
$fontwidth = Imagefontwidth ($font);
$fontheight =imagefontheight ($font);
$image = Imagecreate ($width, $height +20);
$BG = Imagecolorallocate ($image, $BG [0], $BG [1], $BG [2]);
$line =imagecolorallocate ($image, $line [0], $line [1], $line [2]);
$text =imagecolorallocate ($image, $text [0], $text [1], $text [2]);
$dotcolor =imagecolorallocate ($image, $dotcolor [0], $dotcolor [1],$ $dotcolor [2]);
Imageline ($image, 0,0,0, $height, $line);
Imageline ($image, 0, $height, $width, $height, $line);
For ($i =1 $i <11; $i + +)
{
Imagedashedline ($image, 0, $height-$JC * $i *10, $width, $height-$JC * $i *10, $line);
Imagestring ($image, $font, 0, $height-$JC * $i *10, $i *10, $text);
}
for ($i =0; $i {
#echo $tmp. " ";
$x 1= (($width -50)/count ($data)) * ($i) +40;
#echo $x 1. " ";
$y 1= $height-$graphdata [$i]* $JC;
$x 2= $x 1;
$y 2= $y 1+ $graphdata [$i]* $JC;
#echo $y 1. " ";
Imagestring ($image, $font, $x 1, $y 1-2* $fontheight, $graphdata [$i]. "% (". $data [$i]. ")", $text);
Imagearc ($image, $x 1, $y 1, $dot, $dot, 0,360, $dotcolor);
Imagefilltoborder ($image, $x 1, $y 1, $dotcolor, $dotcolor);
Imagestring ($image, $font, $x 1, $y 2, $label [$i], $text);
if ($i >0)
{
Imageline ($image, $tmpx 1, $tmpy 1, $x 1, $y 1, $line);
}
$TMPX 1= $x 1; $tmpy 1= $y 1;
}
Imagegif ($image, $file);
}
For convenience, I did a function to make a column chart.
/* Parameter Description:
$graphdata: Percent data (Y axis)
$label: X Axis Title
$data: Actual data (Y axis)
$graphwidth: Image width
$graphheight: Image Height
$graphscale: Height factor (for $graphheight/100)
$graphfont: Font number
$BG; background color value
$text: Text color values
$grid: Edge Color values
$bar: Color values for columns
$BZ: Remarks (not supported in Chinese)
*/
function Timage (
$graphdata, $label, $data,
$graphwidth, $graphheight, $graphscale, $graphfont,
$BG, $text, $grid, $bar, $BZ)
{
header ("Content-type:image/gif");
$image =imagecreate ($graphwidth +50, $graphheight +50);
$bgcolor = Imagecolorallocate ($image, $BG [0], $BG [1], $BG [2]);
$textcolor = Imagecolorallocate ($image, $text [0], $text [1], $text [2]);
$gridcolor =imagecolorallocate ($image, $grid [0], $grid [1], $grid [2]);
$barcolor =imagecolorallocate ($image, $bar [0], $bar [1], $bar [2]);
$gridabelwidth =imagefontwidth ($graphfont) *3+1;
$gridableheight = Imagefontheight ($graphfont);
imageline ($image, $gridlabelwidth, 0, $gridlabelwidth, $graphheight-1, $gridcolor);
imageline ($image, 0, $graphheight-1, $graphwidth-1, $graphheight-1, $gridcolor);
for ($i =0 $i < $graphheight $i + = $graphheight/10)
{
imagedashedline ($image, 0, $i, $ Graphwidth-1, $i, $gridcolor);
imagestring ($image, $graphfont, 0, $i, round ($graphheight-$i)/$graphscale), $textcolor);
}
$barwidth = (($graphwidth-$gridlabelwidth)/count ($graphdata)) -30;#¿øöæöùµäxü¿í¶è
for ($i =0; $i {
$BARTOPX = $gridlabelwidth + (($i + 1) *20) + ($i * $barwidth); #¿øöæöù¿¿xóµä¾ààë
$BARBOTTOMX = $bartopx + $barwidth; $barbottomy = $graphheight -1;#¿øöæöùµäïâ±ß½ç
$bartopy = $barbottomy-($graphdata [$i]*$ Graphscale);
Imagefilledrectangle ($image, $bartopx, $bartopy, $barbottomx, $barbottomy, $barcolor);
$LABELX 1= $bartopx;
$labely 1= $bartopy-15;
$LABELX 2= $bartopx;
$labely 2= $graphheight;
imagestring ($image, $graphfont, $labelx 1, $labely 1, "$graphdata [$i]". " % ", $textcolor);
imagestring ($image, $graphfont, $labelx 2, $labely 2, "$label [$i]", $textcolor);
Imagestringup ($image, $graphfont, $labelx 1+10, $labely 1-$gridableheight, "$data [$i]", $textcolor);
}
imagestring ($image, $graphfont, 1, $graphheight +30, $BZ, $textcolor);
imagegif ($image);
}
PHP Data pie chart
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 corresponding ellipse
{
$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)//painting 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 slices
{
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 index Color
{
$R = ($clr >>16) & 0xFF;
$G = ($clr >>8) & 0xFF;
$B = ($CLR) & 0xFF;
Return Imagecolorallocate ($img, $R, $G, $B);
}
Drawing main function, and output picture
$datLst is 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 = 10+ $a *2;
$h = 10+ $b *2+ $v + ($fh +2) * $n;
$img = Imagecreate ($w, $h);
Turn 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 background color
Imagefill ($img, 0, 0, $CLRBK);
Sum
$tot = 0;
for ($i =0; $i < $n; $i + +)
$tot + + $datLst [$i];
$SD = 0;
$ed = 0; 333
$ly = 10+ $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 a label
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 generated pictures
$imgFileName = ". /temp/". Time ().". PNG ";
Imagepng ($img, $imgFileName);
Echo ' ;
}
$datLst = Array (30, 10, 20, 20, 10, 20, 10, 20); Data
$labLst = Array ("China University of Science and Technology", "Anhui Polytechnic University", "Tsinghua University", "Beijing University", "Nanjing Universities", "Shanghai University", "Hohai University", "Sun Yat-sen"); Label
$clrLst = Array (0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999);
Drawing
Draw_img ($datLst, $labLst, $clrLst);
?>
?>