Thread: GD help
View Single Post
  #2 (permalink)  
Old 03-17-2007, 03:41 PM
killerkev06 killerkev06 is offline
Persona Non Grata
 
Join Date: Mar 2007
Posts: 124
iTrader: (0)
killerkev06 is on a distinguished road
Default

Here is a code to create a simple pie-chart in php3 (with gd)

PHP Code:
<?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($id000);
$white ImageColorAllocate($id255255255);
$pie_color[1] = ImageColorAllocate($id144151255);
$pie_color[2] = ImageColorAllocate($id1444896);
$pie_color[3] = ImageColorAllocate($id255255192);
$pie_color[4] = ImageColorAllocate($id207255255);
$pie_color[5] = ImageColorAllocate($id95095);
$pie_color[6] = ImageColorAllocate($id255127127);
$pie_color[7] = ImageColorAllocate($id207255255);
$pie_color[8] = ImageColorAllocate($id096192);
$pie_color[9] = ImageColorAllocate($id207200255);

//the white background will be transparent:
ImageFill($id00$white);
ImageColorTransparent($id$white);

if (empty(
$argv)) {
//if no agruments are given than show the empty chart:
ImageArc($id$hcenter$vcenter$d$d0360$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);

?>
Reply With Quote
Sponsored Links
Register and sign in to hide this ad block