|
|
|
|
|
<?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);
?>
|
|
| Display a bar chart based on random values. Categories : Graphics, PHP, GD image library, Charts and Graphs | | | Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs | | | PHP Email image generator - hide your email from bots - using the GD Library Categories : PHP, Graphics, GD image library, Beginner Guides | | | HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout. Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs | | | How to create charts for php using Rchart Categories : PHP, Java, JSP, Graphics, Charts and Graphs | | | 3dLib - a class for drawing in 3D space. Supported functions: Line, SetPixel, Polygon, FilledPolygon, etc. 3dChart() function has been added for one-call drawing of 3d charts. Support of mostly used 3d-transformations. Categories : Graphics, Math., PHP Classes, PHP, Charts and Graphs | | | A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library | | | Advanced Image WaterMarker Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented | | | Simple PHP Bar Graph using GD library Categories : PHP, GD image library, Graphics, Arrays | | | EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly. Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library | | | PHP Image Compression using GD library Categories : PHP, Compression, GD image library, Graphics | | | PHP interface class to the eBusiness Charts generatation remote service. Categories : PHP, PHP Classes, Graphics, Charts and Graphs | | | Image Generation Class ( PNG Format ) Categories : PHP, GD image library, PHP Classes, Graphics | | | CAPTCHA[Image verification] Categories : PHP, Security, GD image library, Graphics, Sessions | | | HTML_Graphs provides a simple PHP interface for
creating pure HTML charts. Categories : Graphics, PHP, PHP Classes, Charts and Graphs | |
| |
| |
|