<?php

/*
* Pie chart class by Thomas Smit 06/02/2007 - 07/02/2007
*/

class piechart
{

    private 
$image;         // @var resource $image - image holder
    
private $dimensions;    // @var array $dimensions - image dimensions
    
private $data;          // @var array $data - data constituting the pie
    
private $colours;       // @var array $colours - the colours making the pie
    
private $border;        // @var array $border - border colour of the chart

    /*
    * piechart::__construct() - set up local variables
    * @param array $data - data to use for the pie in an associative array
    * @param array $colours - colours for each pie segment
    * @param array $border - pie border in array(R,G,B) format
    * @param array $bg - the background colour of the chart array(R,G,B format) (default white)
    * @param int $width - width of the image (default 200)
    * @param int $height - height of the image (default 200)
    * @return void
    */

    
public function __construct($data$colours$border$bg = array(255255255), $width 200$height 200)
    {
        
$this->image imagecreatetruecolor($width$height);
        
imagefill($this->image00imagecolorallocate($this->image$bg[0], $bg[1], $bg[2]));
        
$this->dimensions = array($width$height);
        
$this->data $this->createdata($data);
        
$this->colours $this->setcolours($colours);
        
$this->border imagecolorallocate($this->image$border[0], $border[1], $border[2]);
    }

    
/*
    * piechart::createdata() - express array values as angles
    * @param array $data - data to use
    * @return array
    */

    
private function createdata($data)
    {
        if(!
is_array($data))
            
trigger_error('Class data must be fed as an array'E_USER_ERROR);

        
$total 0;
        foreach(
$data as $val)
            
$total += (int) $val;

        foreach(
$data as $key => $val)
            
$data[$key] = ($val $total) * 360;

        return 
$data;
    }

    
/*
    * piechart::setcolours() - set up the colours for inclusion in the chart
    * @param array $colours - colours to use; array(R,G,B) format
    * @return array
    */

    
private function setcolours($colours)
    {
        if(!
is_array($colours))
            
trigger_error('Colour data must be fed as an array'E_USER_ERROR);

        
$setcolours = array();
        foreach(
$colours as $colour => $rgb)
            
$setcolours[] = imagecolorallocate($this->image$rgb[0], $rgb[1], $rgb[2]);

        return 
$setcolours;
    }

    
/*
    * piechart::render() - render the chart
    * @return void
    */

    
public function render()
    {

        
$offset 0;
        foreach(
$this->data as $item => $percent)
        {
            
imagefilledarc($this->image$this->dimensions[0] / 2$this->dimensions[1] / 2$this->dimensions[0] - 40$this->dimensions[1] - 40, ($item == 0) ? $offset $this->data[$item 1], $offset $this->data[$item 1] + $this->data[$item], $this->colours[$item], IMG_ARC_PIE);
            
$offset += $this->data[$item 1];
        }
        
imagefilledarc($this->image$this->dimensions[0] / 2$this->dimensions[1] / 2$this->dimensions[0] - 40$this->dimensions[1] - 400360$this->borderIMG_ARC_NOFILL);
        
imagepng($this->image);
    }

    
/*
    * piechart::__destruct() - free up memory associated with image
    * @return void
    */

    
public function __destruct()
    {
        
imagedestroy($this->image);
    }

}

?>