Creating Pie Graph - Any Ideas?
Posted: Mon May 22, 2006 7:46 pm
Hi,
Does any one knows how can I create pie graph?
THanks!
Does any one knows how can I create pie graph?
THanks!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
// a simple pie-chart in php3 (with gd)
// (c)2000 knik@crosswinds.net
//
// 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_width = 320;
$img_height = 192;
$id = ImageCreate($img_width,$img_height);
$hcenter = ImageSX($id)/2;
$vcenter = ImageSY($id)/2;
//diameter:
$d = 160;
//define colors:
//there are 9 pie colors, same as in Excel
$black = ImageColorAllocate($id, 0, 0, 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)) {
$n++;
$label[$n] = $key;
$value[$n] = $val;
$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 = 1;
$hfw = ImageFontWidth($idf);
$vfw = ImageFontHeight($idf);
//calculate the percents:
$perc[$i] = $value[$i]/$total;
$percstr[$i] = (string) number_format($perc[$i]*100,1)."%";
//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);
}
//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);
?>