<?php /* -*- C -*- */
/*
** $Id: piechart.phl,v 1.2 1998/02/03 16:55:06 borud Exp $
**
** PHP Class for creating pie charts using the GD library functions.
**
** There is a bug in the GD library somewhere that seems to kick in
** when you try to return images that are larger than 4K. We probably
** need a workaround for this...
**
** Pie charts look a bit shabby. There seems to be one or more
** roundoff errors lurking about making life hard for us. To fix this
** we should perhaps investigate how the Arc-drawing thingey works and
** try to find out how it gets the endpoints. Also the flood-filler
** doesn't quite cope with filling the pieces very well.
**
** Authors: Bj?Borud, Guardian Networks AS, <borud@guardian.no>
*/
/* {{{ piechart */
/*
** This is a class for creating pie charts. Generally you just have
** to instantiate it, and then make a call to the "init" method to
** set the size and transfer the data.
**
** The data is an array of arrays that consist the following data:
** o numeric value
** o value legend
** o red \
** o green > the RGB values for the color of the slice/legend
** o blue /
**
*/
class piechart {
/* {{{ attributes */
var $im;
var $width, $height;
var $data;
var $colors;
var $angles;
var $left=10;
var $right=200;
var $top=100;
var $bottom=10;
var $head_top=10;
var $head_space=5;
var $legend_left=20;
var $center_x;
var $center_y;
var $diameter;
/* sum of values */
var $sum;
/* font sizes */
var $fx, $fy;
var $legend_num = "";
/* }}} */
/* {{{ constants */
var $PI = 3.1415926535897931;
/* }}} */
/* {{{ roundoff */
/*
** PHP has no function for rounding off doubles to the nearest
** integer so we have to roll our own.
*/
function roundoff ($v) {
if ( $v - floor($v) >= 0.5) {
return(ceil($v));
} else {
return(floor($v));
}
}
/* }}} */
/* {{{ deg2rad */
/*
** The built-in trig functions use radians and there's no
** function in PHP to convert between degrees and radians
*/
/*
** Initialize the object and draw the pie. This would be the
** constructor in an ordinary OO scenario -- just that we haven't
** got constructors in PHP, now do we? ;-)
*/
/* utility function to set an attribute so we display percentages */
function set_legend_percent () {
$this->legend_num = "p";
}
/* }}} */
/* {{{ set_legend_value */
/* utility function to set an attribute so we display values */
function set_legend_value () {
$this->legend_num = "v";
}
/* }}} */
/* {{{ draw_point */
/*
** This function is just here for debugging purposes. It is
** sometimes very useful to be able to draw an X to check
** coordinates.
*/
function draw_point($x, $y) {
ImageLine($this->im, $x-4, $y-4, $x+4, $y+4, $this->black);
ImageLine($this->im, $x-4, $y+4, $x+4, $y-4, $this->black);
}
/* }}} */
/* {{{ draw_margins */
/*
** Also a debugging function to show where the margins are at
*/
function draw_margins () {
/*
** Draw legends at the right side of the pie chart. This function
** accepts a fontsize and gathers all the other information from
** the multilevel data array
*/
function draw_legends ($fontsize) {
$n = count($this->data);
/*
** This function accepts an array of arrays containing (in order):
** o The text of the heading as a string
** o The fontsize as an integer
** o The justification ("c"=center)
**
*/
function draw_heading($head_data) {
$n = count($head_data);
/*
** This function draws a piece of pie centered at x,y starting at
** "from" degrees and ending at "to" degrees using the specified color.
*/
function draw_slice ($x, $y, $from, $to, $color) {