A simple pie chart generation script.
Displays the percentage at center arc and displays the Legends with random colors.
class simplepie
{
function __construct($width, $height, $dataArr)
{
$font = './verdana.ttf'; /** get it from c:/windows/fonts dir */
$this->image = imagecreate($width,$height);
$piewidth = $width * 0.70;/* pie area */
$x = round($piewidth/2);
$y = round($height/2);
$total = array_sum($dataArr);
$angle_start = 0;
$ylegend = 2;
imagefilledrectangle($this->image, 0, 0, $width, $piewidth, imagecolorallocate($this->image, 128, 128, 128));
foreach($dataArr as $label=>$value) {
$angle_done = ($value/$total) * 360; /** angle calculated for 360 degrees */
$perc = round(($value/$total) * 100, 1); /** percentage calculated */
$color = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagefilledarc($this->image, $x, $y, $piewidth, $height, $angle_start, $angle_done+= $angle_start, $color, IMG_ARC_PIE);
$xtext = $x + (cos(deg2rad(($angle_start+$angle_done)/2))*($piewidth/4));
$ytext = $y + (sin(deg2rad(($angle_start+$angle_done)/2))*($height/4));
imagettftext($this->image, 6, 0, $xtext, $ytext, imagecolorallocate($this->image, 0, 0, 0), $font, "$perc %");
imagefilledrectangle($this->image, $piewidth+2, $ylegend, $piewidth+20, $ylegend+=20, $color);
imagettftext($this->image, 8, 0, $piewidth+22, $ylegend, imagecolorallocate($this->image, 0, 0, 0), $font, $label);
$ylegend += 4;
$angle_start = $angle_done;
}
}
function render()
{
header('Content-type: image/png');
imagepng($this->image);
}
}
/** usage */
$dataArr = array(2001=>10, 2002=>30, 2003=>50, 2004=>10);
$width=600;
$height=480;
$pie = new simplepie($width, $height, $dataArr);
$pie->render();
----
Server IP: 69.147.83.197
Probable Submitter: 61.246.62.41
----
Manual Page -- http://www.php.net/manual/en/function.imagefilledarc.php
Edit -- https://master.php.net/note/edit/102337
Del: integrated -- https://master.php.net/note/delete/102337/integrated
Del: useless -- https://master.php.net/note/delete/102337/useless
Del: bad code -- https://master.php.net/note/delete/102337/bad+code
Del: spam -- https://master.php.net/note/delete/102337/spam
Del: non-english -- https://master.php.net/note/delete/102337/non-english
Del: in docs -- https://master.php.net/note/delete/102337/in+docs
Del: other reasons-- https://master.php.net/note/delete/102337
Reject -- https://master.php.net/note/reject/102337
Search -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.