Home >Backend Development >PHP Tutorial >PHP function to make pie chart_PHP tutorial

PHP function to make pie chart_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:20:40801browse

########
bimage.php3
########

/*
Function description
$chartdata: data, which is an array element
$chartfont: font size
$chartdiameter: determines the size of the pie (it depends on whether you are hungry, haha)
$chartlabel: title, also an array element
$colorslice: color array, such as $tmp =array255,255,255);$colorslic=array($tmp);
$colorborder: border color, array
$colortext: text color, array
$colorbody: background color, array
$file :Output image file name*/
function bimage($chartdata,
$chartfont,
$chartdiameter,
$chartlabel,
$colorslice,
$colorbody,
$ colorborder,
$colortext ,
$file
)
{
$chartdiameter=150;

$chartfontheight=imagefontheight($chartfont);
$d1= 10;$d2=20;$d3=30;$d4=40;$d5=50;
$chartdata=array($d1,$d2,$d3,$d4,$d5);
$ chartlabel=array("D1","D2","D3","D4","D5");
$chartwidth=$chartdiameter+20;
$chartheight=$chartdiameter+20+(($ chartfontheight+2)*count($chartdata));
header("content-type:image/gif");
$image=imagecreate($chartwidth,$chartheight);
$colorbody =imagecolorallocate ($image,$colorbody[0],$colorbody[1],$colorbody[2]);
$colortext =imagecolorallocate ($image,$colortext[0],$colortext[1],$colortext[2 ]);
$colorborder =imagecolorallocate ($image,$colorborder[0],$colorborder[1],$colorborder[2]);
for ($i=0;$i {
$t=imagecolorallocate($image,$colorslice[$i][0],$colorslice[$i][1],$colorslice[$i][2]);
$colorslice[$i] =$t;
}
for($i=0;$i {
$charttotal+=$chartdata[$i];
}
$chartcenterx=$chartdiameter/ 2+10;
$chartcentery=$chartdiameter/2+10;
$degrees=0;
for($i=0;$i {
$startdegrees=round($ degrees);
$degrees+=(($chartdata[$i]/$charttotal)*360);
$enddegrees=round($degrees);
$currentcolor=$colorslice[$i%( count($colorslice))];
imagearc($image,
$chartcenterx,
$chartcentery,
$chartdiameter,
$chartdiameter,
$startdegrees,
$ enddegrees,
$currentcolor);
list($arcx,$arcy)=circle_point($startdegrees,$chartdiameter);
imageline($image,
$chartcenterx,
$chartcentery,
floor($chartcenterx+$arcx),
floor($chartcentery+$arcy),
$currentcolor );
list($arcx,$arcy)=circle_point($enddegrees,$chartdiameter);

imageline($image,
$chartcenterx,
$chartcentery,
ceil($chartcenterx+$arcx),
ceil($chartcentery +$arcy),
$ currentcolor);

$midpoint=round((($enddegrees-$startdegrees)/2)+$startdegrees);
list($arcx,$arcy)= circle_point ( $midpoint, $chartdiameter/ 2);
imagefilltoborder($image,
floor($chartcenterx+$arcx),
floor($chartcentery+$arcy),
$currentcolor,
$currentcolor);
}
imagearc($image,
$chartcenterx,
$chartcentery,
$chartdiameter,
$chartdiameter,
0,360,
$colorborder);
imagefilltoborder ($ image,
floor($chartcenterx +( $chartdiameter /2)+2),
$chartcentery ,
$colorborder,
$colorborder );
for ($i=0;$ i {
$currentcolor=$colorslice[$i%(count($colorslice))];
$liney=$chartdiameter+20+($i*($chartfontheight+2));
imagerectangle ($image,
10,
$liney,
20+$chartfontheight,
$liney+$chartfontheight,
$colorbody);
imagefilltoborder($image,
12,
$liney+2,
$colorbody,
$currentcolor);
imagestring($image,
$chartfont,
40+$chartfontheight,
$liney,
"$chartlabel[$i]:$chartdata[$i]",
$colortext);

}

imagegif ($image,$file) ;

}

function radians($degrees)
{
return($degrees*(pi()/180.0));
}
function circle_point ($degrees,$diameter)
{
$x=cos(radians($degrees))*($diameter/2);
$y=sin(radians($degrees))*($ diameter/2);
return (array($x,$y));
}
?>

##########
This is an example of a call
##########


include("bfunc.php3");
$chartdiameter=250;
$ chartfont=5;
$d1=10;$d2=20;$d3=30;$d4=40;$d5=50;
$chartdata=array($d1,$d2,$d3,$ d4,$d5);
$chartlabel=array("D1","D2","D3","D4","D5");

$colorbody=array(0xff,0xff, 0xff);
$colorborder=array(0x00,0x00,0x00);
$colortext=array(0xff,0xff,0xff);

$color1=array(0xff,0x00,0x00) ;
$color2=array(0x00,0xff,0x00);
$color3=array(0x00,0x00,0xff);
$color4=array(0xff,0xff,0x00);
$ color5=array(0xff,0x00,0xff);
$colorslice=array($color1,$color2,$color3,$color4,$color5);
$file="tj.gif"
bimage ($chartdata,
$chartfont,
$chartdiameter,
$chartlabel,
$colorslice,
$colorbody,
$colorborder,
$colortext,
$ file )
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532564.htmlTechArticle######## bimage.php3 ######## /* Function Description$ chartdata: data, it is an array element $chartfont: font size $chartdiameter: determines the size of the pie (it depends on whether you are hungry, haha) $chartlabel...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn