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

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

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

/**
*Wrapper around gd's Image(Filled)Rectangle functions 
*Class used to draw rectangles in the chart image
*This class is utilized by:
*<ul>
*<li>Chart class for drawing the legend rectangles</li>
*<li>BarChart class for drawing the chart bars</li>  
*</ul>
*@author Tsiavos Chris <hide@address.com> 
*@license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class Rectangle extends Shape {
	
	/**
	*x value of the upper left coordinate of the rectangle
	*@access private
	*@var integer
	*/
	private $X_Start;
	
	/**
	*x value of the bottom right coordinate of the rectangle
	*@access private
	*@var integer
	*/
	private $X_Finish;
	
	/**
	*y value of the upper left coordinate of the rectangle
	*@access private
	*@var integer
	*/
	private $Y_Start;
	
	/**
	*y value of the bottom right coordinate of the rectangle
	*@access private
	*@var integer
	*/
	private $Y_Finish;
	
	/**
	*the caption of the rectangle. (This property is mostly used for the status-indication
	*property of the chart)
	*@access private
	*@var string
	*/
	private $Caption;
	
	/**
	*the color of the rectangle's caption
	*@access private
	*@var string
	*/
	private $CaptionColor;
	
	/**
	*the font instance used for drawing the Rectangle's caption
	*@access private
	*@var Font
	*/
	private $Font;
	
	/**
	*Constructor
	*@param mixed $Canvas reference to the image identifier the rectangle will be drawn in
	*@param ColorAllocator $ColorAllocator reference to the ColorAllocator the class will use for allocating the rectangle color
	*@param string $UseAntialias whether antialias functions should be used or not when drawing the rectangle
	*@param integer $X_Start x value of the upper left coordinate of the rectangle
	*@param integer $Y_Start y value of the upper left coordinate of the rectangle
	*@param integer $X_Finish x value of the bottom right coordinate of the rectangle
	*@param integer $Y_Finish y value of the bottom right coordinate of the rectangle
	*@param string $Caption the caption of the rectangle
	*/
	function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias,$X_Start,$Y_Start,$X_Finish,$Y_Finish) {
		
		Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
		$this->X_Start=$X_Start;
		$this->Y_Start=$Y_Start;
		$this->X_Finish=$X_Finish;
		$this->Y_Finish=$Y_Finish;
		
	}
	
	/**
	*Specifies the caption properties of the rectangle
	*@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;
	}
	
	/**
	*Draws the border of the rectangle
	*@param string $BorderColor
	*@param integer $Alpha the alpha value of the border color
	*@return void
	*@access public
	*/
	public function draw($BorderColor,$Alpha) {
		if ($this->UseAntialias=="Yes")
			ImageAntialias($this->Canvas,1);	
	
		$this->draw_Caption();
	
		$ColorRange=$this->Y_Finish-$this->Y_Start;
		$ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$BorderColor,$BorderColor,$Alpha,$ColorRange); 		
		ImageRectangle($this->Canvas,$this->X_Start,$this->Y_Start,$this->X_Finish,$this->Y_Finish,$ColorHandler);
	
		if ($this->UseAntialias=="Yes") 
			ImageAntialias($this->Canvas,0);
	}

	/**
	*Draws a filled rectangle
	*@param string $StartColor specifies the starting color of the filled rectangle
	*@param string $FinishColor specifies the finish color of the filled rectangle
	*If the $StartColor and $FinishColor are different then the rectangle will be filled 
	*with gradient color
	*@param integer $Alpha the alpha value of the rectangle's color
	*@return void
	*@access public
	*/
	public function draw_Filled($StartColor,$FinishColor,$Alpha) { 	
		$this->draw_Caption();	
	
		$ColorRange=abs($this->Y_Finish-$this->Y_Start);
		$ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,$Alpha,$ColorRange); 	
		
		if (is_array($ColorHandler))
			$this->draw_GradientRectangle($ColorHandler);
		else
			$this->draw_NormalRectangle($ColorHandler);
	}
	
	/**
	*Draws a uniform color filled rectangle
	*@param mixed $ColorHandler the color indentifier which will be used to fill the rectangle 
	*@return void
	*@access private
	*/
	private function draw_NormalRectangle($ColorHandler) {
		ImageFilledRectangle($this->Canvas,$this->X_Start,$this->Y_Start,$this->X_Finish,$this->Y_Finish,$ColorHandler);
	}
	
	/**
	*Draws a gradient color filled rectangle
	*@param mixed $ColorHandler the color indentifier which will be used to fill the rectangle 
	*@return void
	*@access private
	*/
	private function draw_GradientRectangle($ColorHandler) {
		for ($i=0;$i<abs($this->Y_Finish-$this->Y_Start);$i++)
				ImageLine($this->Canvas,$this->X_Start,$this->Y_Start+$i,$this->X_Finish,$this->Y_Start+$i,$ColorHandler[$i]);
	}
	
	/**
	*Draws the rectangle's caption
	*@access private
	*@return void
	*/
	private function draw_Caption() {
		if (!($this->Font instanceof Font)) return;
		
		$FontMetrics=$this->Font->get_FontMetrics($this->Caption);
		
		$CenterPos=(int)(abs($this->X_Finish-$this->X_Start)-$FontMetrics["FontWidth"])/2;
		$Caption_XPos=$this->X_Start+$CenterPos;
		$Caption_YPos=$this->Y_Start-$FontMetrics["FontHeight"]-5;
		
		$this->Font->draw_String($Caption_XPos,$Caption_YPos,$this->Caption,$this->CaptionColor);
	}
}

?>
Return current item: phpchartPlus