<?php
class CreatPieChart
{
public function createChart($viewall)
{
// a simple pie-chart in php3 (with gd)
// (c)2000 hide@address.com
//
// usage: pie.php3?label=value&label=value&label=value&...
// ex.: pie.php3?label1=10&label2=15&label3=20
//
// code can be freely copied & modified
Header("Content-type: image/gif");
//init and create image:
$img_title = $_POST['inputdata'];
$img_diameter = $_POST['Diameter'];
$img_width = 600;
$img_height = 600;
$id = ImageCreate($img_width,$img_height);
$hcenter = ImageSX($id)/2;
$vcenter = ImageSY($id)/2;
//diameter:
//$d = 200;
$d = $img_diameter;
//define colors:
//there are 9 pie colors, same as in Excel
$blue = imagecolorallocate($id,144, 151, 255);
$black = ImageColorAllocate($id, 0, 0, 0);
$green = imagecolorallocate($id,0,255,0);
$white = ImageColorAllocate($id, 255, 255, 255);
$pie_color[1] = ImageColorAllocate($id, 144, 151, 255);
$pie_color[2] = ImageColorAllocate($id, 144, 48, 96);
$pie_color[3] = ImageColorAllocate($id, 255, 255, 192);
$pie_color[4] = ImageColorAllocate($id, 207, 255, 255);
$pie_color[5] = ImageColorAllocate($id, 95, 0, 95);
$pie_color[6] = ImageColorAllocate($id, 255, 127, 127);
$pie_color[7] = ImageColorAllocate($id, 207, 255, 255);
$pie_color[8] = ImageColorAllocate($id, 0, 96, 192);
$pie_color[9] = ImageColorAllocate($id, 207, 200, 255);
//the white background will be transparent:
ImageFill($id, 0, 0, $white);
ImageColorTransparent($id, $white);
/*if (empty($argv)) {
//if no agruments are given than show the empty chart:
ImageArc($id, $hcenter, $vcenter, $d, $d, 0, 360, $black);
$idf = 3;
$hfw = ImageFontWidth($idf);
$vfw = ImageFontHeight($idf);
$string = "no args!";
$hpos = $hcenter - $hfw*strlen($string)/2;
$vpos = $vcenter - $vfw/2;
ImageString($id, $idf, $hpos, $vpos, $string, $black);
$string = "ex.: pie.php3?label1=10&label2=15&label3=20";
$hpos = $hcenter - $hfw*strlen($string)/2;
$vpos = ImageSY($id) - $vfw;
ImageString($id, $idf, $hpos, $vpos, $string, $black);
}
else {
*/$total = 0;
$n = 0;
//read the arguments into different arrays:
//while (list($key, $val) = each($HTTP_GET_VARS)) {
//$array = array('cricket'=>10,'vollyball'=>20,'hocky'=>15,'tenis'=>20,'football'=>20,'KABBADI'=>10);
while (list($key, $val) = each($viewall)) {
$n++;
$label[$n] = $key;
$keys[$n] = $key;
$values[$n] = $val;
$value[$n] = $val;
//echo 'total';
$total += $val;
$arc_rad[$n] = $total*2*pi(void);
$arc_dec[$n] = $total*360;
}
//the base:
$arc_rad[0] = 0;
$arc_dec[0] = 0;
//count the labels:
for ($i = 1; $i <= $n; $i++) {
$idf = 4;
$hfw = ImageFontWidth($idf);
$vfw = ImageFontHeight($idf);
//calculate the percents:
$perc[$i] = $value[$i]/$total;
$percstr[$i] = (string) number_format($perc[$i]*100)."%";
//label with percentage:
$label[$i] = $label[$i]." (".$percstr[$i].")";
//calculate the arc and line positions:
$arc_rad[$i] = $arc_rad[$i]/$total;
$arc_dec[$i] = $arc_dec[$i]/$total;
$hpos = Round($hcenter + ($d/2)*Sin($arc_rad[$i]));
$vpos = Round($vcenter + ($d/2)*Cos($arc_rad[$i]));
ImageLine($id, $hcenter, $vcenter, $hpos, $vpos, $black);
ImageArc($id, $hcenter, $vcenter, $d, $d, $arc_dec[$i-1], $arc_dec[$i], $black);
//calculate the positions for the labels:
$arc_rad_label = $arc_rad[$i-1] + 0.5*$perc[$i]*2*pi(void);
$hpos = $hcenter + 1.1*($d/2)*Sin($arc_rad_label);
$vpos = $vcenter + 1.1*($d/2)*Cos($arc_rad_label);
if (($arc_rad_label > 0.5*pi(void)) && ($arc_rad_label < 1.5*pi(void))) {
$vpos = $vpos - $vfw;
}
if ($arc_rad_label > pi(void)) {
$hpos = $hpos - $hfw*strlen($label[$i]);
}
//display the labels:
ImageString($id, $idf, $hpos, $vpos, $label[$i], $black);
imagestring($id,8,260,0,$img_title,$blue);
imagerectangle($id,150,1,450,150,$green);
$x = 200;
$y = 40;
imagestring($id,8,$x,20,"Contents : values ",$blue);
for($j=1; $j<=count($viewall); $j++)
{
imagestring($id,8,$x,$y,$keys[$j]." : ".$values[$j],$blue);
//$x = $x + 10;
$y = $y + 15;
}
}
//imagealphablending($id,true);
//imagesettile($id,$img_title);
//fill the parts with their colors:
for ($i = 1; $i <= $n; $i++) {
$arc_rad_label = $arc_rad[$i-1] + 0.5*$perc[$i]*2*pi(void);
$hpos = $hcenter + 0.8*($d/2)*Sin($arc_rad_label);
$vpos = $vcenter + 0.8*($d/2)*Cos($arc_rad_label);
ImageFillToBorder($id, $hpos, $vpos, $black, $pie_color[$i]);
}
//}
//and finally:
ImageGif($id);
ImageDestroy($id);
}
}
?>