Location: PHPKode > scripts > phpchartPlus > phpchartplus/Sources/Circle.php
<?php

/*
**************************************************
Class: Circle.php
**************************************************
Author: Tsiavos Chris <hide@address.com>
Date: October 2004
**************************************************/

/**
*Includes the Shape abstract class
*/
require_once("Shape.php");

/**
*Subsidiary class utilized by the PieChart class
*@author Tsiavos Chris <hide@address.com>
*@license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class Chunk {
	
	/**
	*the size of the chunk in degrees
	*@access private
	*@var integer
	*/
	private $Angle;

	/**
	*the caption of the chunk
	*@access private
	*@var string
	*/
	private $Caption;
	
	/**
	*@access private
	*@var string
	*/
	private $CaptionColor;
	
	/**
	*the starting color of the chunk
	*@access private
	*@var string
	*/
	private $StartColor;
	
	/**
	*the finishing color of the chunk. If $FinishColor
	*is different from $StartColor then a gradient color chunk 
	*will be drawn
	*@access private
	*@var string 
	*/
	private $FinishColor;
	
	/**
	*the Font instance used for drawing the chunk's caption
	*@access private
	*@var Font
	*/
	private $Font;
	
	/**
	*Sets the size of the chunk in degrees
	*@access public
	*@return void
	*@param integer $Degree 
	*/
	public function set_Angle($Degree) {
		$this->Angle=$Degree;
	}
	
	/**
	*Returns (in degrees) the size of the chunk
	*@access public
	*@return integer
	*/
	public function get_Angle() {
		return $this->Angle;
	}
	
	/**
	*Sets the caption of the chunk
	*@access public
	*@return void
	*@param Font &$Font
	*@param string $Caption
	*@param string $CaptionColor 
	*/
	public function set_Caption(Font &$Font,$Caption,$CaptionColor) {
		$this->Font=$Font;
		$this->Caption=$Caption;
		$this->CaptionColor=$CaptionColor;
	}
	
	/**
	*Returns the caption of the chunk
	*@access public
	*@return string
	*/
	public function get_Caption() {
		return $this->Caption;
	}
	
	/**
	*Returns the font color of the chunk's caption
	*@access public
	*@return string
	*/
	public function get_CaptionColor() {
		return $this->CaptionColor;
	}
	
	/**
	*Sets the color the chunk will be filled in.
	*@access public
	*@return void
	*@param string $StartColor The starting color of the chunk
	*@param string $FinishColor The finishing color of the chunk.If $StartColor 
	*is different from $FinishColor then a gradient color will be alllocated
	*/
	public function set_Color($StartColor,$FinishColor) {
		$this->StartColor=$StartColor;
		$this->FinishColor=$FinishColor;
	}
	
	/**
	*Returns the starting color of the chunk
	*@access public
	*@return string
	*/
	public function get_StartColor() {
		return $this->StartColor;
	}	
	
	/**
	*Returns the finishing color of the chunk
	*@access public
	*@return string
	*/
	public function get_FinishColor() {
		return $this->FinishColor;
	}
	
	/**
	*Returns the Font instance used for drawing the caption of the chunk
	*@access public
	*@var Font
	*/
	public function get_Font() {
		return $this->Font;
	}

}

/**
*Wrapper around gd's Image(Filled)Arc functions
*This class is utilized by the PieChart class for generating
*the pie chart type
*@author Tsiavos Chris <hide@address.com>
*@license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class Circle extends Shape {
	
	/**
	*x value of the pie's center position
	*@access private
	*@var integer
	*/
	private $Center_XPos;
	
	/**
	*y value of the pie's center position
	*@access private
	*@var integer
	*/
	private $Center_YPos;
	
	/**
	*pie's width
	*@access private
	*@return integer
	*/
	private $Width;
	
	/**
	*pie's height
	*@access private
	*@return integer
	*/
	private $Height;
	
	/**
	*Array holding the chunks composing the pie
	*@access private
	*@return Chunk[]
	*/
	private $Chunk=array();
	
	/**
	*@access private
	*@var integer
	*/
	private $Current_Degree;
	
	/**
	*Constructor 
	*@param mixed $Canvas Reference to the image handler the pie will be drawn in
	*@param ColorAllocator $ColorAllocator Reference to the ColorAllocator the class will use for allocating chunks' colors
	*@param integer $Center_XPos x value of the pie's center position
	*@param integer $Center_YPos y value of the pie's center position
	*@param integer $Width Pie's width
	*@param integer $Height Pie's height
	*/
	function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias,$Center_XPos,$Center_YPos,$Width,$Height) {
	
		Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
		$this->Center_XPos=$Center_XPos;
		$this->Center_YPos=$Center_YPos;
		$this->Width=$Width;
		$this->Height=$Height;
		$this->Current_Degree=-1;
	}
	
	/**
	*Adds a chunk to the pie
	*@access public
	*@return void
	*@param Chunk &$chunk 
	*/
	public function addChunk(Chunk $chunk) {
		array_push($this->Chunk,$chunk);
	}	

	/**
	*Draws a filled with color pie
	*@access public
	*@return void
	*/
	public function draw_Filled() {
		for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
		
			$ColorRange=$this->Chunk[$chunk]->get_Angle();	
			$StartColor=$this->Chunk[$chunk]->get_StartColor();
			$FinishColor=$this->Chunk[$chunk]->get_FinishColor();
			
			$ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,0,$ColorRange);		
			
			if (is_array($ColorHandler))
				$this->draw_GradientChunk($chunk,$ColorHandler);
			else 
				$this->draw_Chunk($chunk,$ColorHandler);
		}
		
		$this->draw_Caption();		
	}
	
	/**
	*Draws the border of the pie
	*@access public
	*@return void
	*@param string $BorderColor
	*/
	public function draw($BorderColor) {
		if ($this->UseAntialias=="Yes")
			ImageAntialias($this->Canvas,1);
		
		$ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$BorderColor,$BorderColor,0,1);
		
		ImageEllipse($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$ColorHandler);
		
		$this->Current_Degree=-1;
		
		for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
			$this->Current_Degree++;
			$Angle=($this->Current_Degree+$this->Chunk[$chunk]->get_Angle())*(-1);
		
			$Line_XPos=($this->Width/2)*cos(deg2rad($Angle+1));
			$Line_YPos=($this->Width/2)*sin(deg2rad($Angle+1));
			
			$LineXPos=$this->Center_XPos+$Line_XPos;
			$LineYPos=$this->Center_YPos+$Line_YPos*-1;
			
			ImageLine($this->Canvas,$LineXPos,$LineYPos,$this->Center_XPos,$this->Center_YPos,$ColorHandler);	
			
			$this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
		}
		
		if ($this->UseAntialias=="Yes")
			ImageAntialias($this->Canvas,0);			
	}
	
	/**
	*This function is called by Circle::draw() to draw a gradient color chunk if Chunk::$StartColor is different from Chunk::$FinishColor
	*@access private
	*@return void
	*/
	private function draw_GradientChunk($chunk,$ColorHandler) {
		$CurrentDegree=$this->Current_Degree++;
		
		for ($Degree=$CurrentDegree;$Degree<$CurrentDegree+$this->Chunk[$chunk]->get_Angle();$Degree++) 
			ImageFilledArc($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$Degree,$Degree+1,$ColorHandler[$Degree-$CurrentDegree],IMAGE_ARC_PIE);	
				
		$this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
	}
	
	/**
	*This function is called by Circle::draw() to draw a uniform color chunk if Chunk::$StartColor and Chunk::$FinishColor are the same colors
	*@access private
	*@return void
	*/
	private function draw_Chunk($chunk,$ColorHandler) {	
		$StartDegree=$this->Current_Degree++;
		$EndDegree=$this->Current_Degree+$this->Chunk[$chunk]->get_Angle();
		
		ImageFilledArc($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$StartDegree,$EndDegree,$ColorHandler,IMAGE_ARC_PIE);
	
		$this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
	}
	
	/**
	*Draws the caption of each chunk
	*@access private
	*@return void
	*/
	private function draw_Caption() {
		$this->Current_Degree=-1;
		
		for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
			$this->Current_Degree++;
			$Angle=($this->Current_Degree+$this->Chunk[$chunk]->get_Angle()/2)*(-1);
		
			$PreviousCaption_XPos=($this->Width/2)*cos(deg2rad($Angle));
			$PreviousCaption_YPos=($this->Width/2)*sin(deg2rad($Angle));	
			
			$Caption_XPos=$this->Center_XPos+$PreviousCaption_XPos/2;
			$Caption_YPos=$this->Center_YPos+($PreviousCaption_YPos/2)*-1;

			$Caption=$this->Chunk[$chunk]->get_Caption();
			$CaptionColor=$this->Chunk[$chunk]->get_CaptionColor();
		
			$this->Chunk[$chunk]->get_Font()->draw_String($Caption_XPos,$Caption_YPos,$Caption,$CaptionColor);
			
			$this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
		}
	}
}

?>
Return current item: phpchartPlus