- <?php
-
- /*
- **************************************************
- Class: Chart.php
- **************************************************
- Author: Tsiavos Chris <jaames@freemail.gr>
- Date: October 2004
- **************************************************/
-
- error_reporting(E_ALL ^ E_NOTICE);
-
- /**
- *Includes the Rectangle Class for in-class scope operations
- */
- require_once("Rectangle.php");
-
- /**
- *Includes the Line Class for in-class scope operations
- */
- require_once("Line.php");
-
- /**
- *Base class for all chart types.
- *
- *Base class for all chart types.Subclasses must implement
- *the abstract function Chart::draw() to gain the parent class functionality
- *@abstract
- *@author Tsiavos Chris <jaames@freemail.gr>
- *@license http://opensource.org/licenses/gpl-license.php GNU Public License
- */
- abstract class Chart {
-
- /**
- *Holds the Chart Image Handler
- * @access protected
- */
- protected $Chart_Image;
-
- /**
- *Holds the xsize value of the charting area
- * @access protected
- * @var integer
- */
- protected $Area_xsize;
-
- /**
- *Holds the ysize value of the charting area
- *@access protected
- * @var integer
- */
- protected $Area_ysize;
-
- /**
- *Holds the yscale value of the chart
- *@access protected
- *@var integer
- */
- protected $Chart_Yscale;
-
- /**
- *Indicates the grid position with value 0
- *@access protected
- *@var integer
- */
- protected $Chart_ZeroPos;
- /**
- *Holds a reference to the ConfigParser instance
- * @access protected
- * @var ConfigParser
- */
- protected $ConfigParser_;
-
- /**
- *Holds a reference to the DataParser instance
- *@access protected
- *@var DataParser
- */
- protected $DataParser_;
-
- /**
- *Holds a reference to the ColorAllocator instance
- *@access protected
- *@var ColorAllocator
- */
- protected $ColorAllocator_;
-
- /**
- *Holds a reference to the selected caching strategy
- *@access protected
- */
- protected $Caching_Strategy_;
-
- /**
- *Holds a reference to the Font instance
- *@access protected
- */
- protected $Font_;
-
- /**
- *Chart Constructor
- */
- function __construct() { }
-
- /**
- *Specifies the DataParser Instance for in-class scope operations
- *@access public
- *@return void
- *@param DataParser $DataParser
- */
- public function set_DataParser(DataParser &$DataParser) {
- $this->DataParser_=$DataParser;
- }
-
- /**
- *Specifies the ConfigParser Instance for in-class scope operations
- *@access public
- *@return void
- *@param ConfigParser $ConfigParser
- */
- public function set_ConfigParser(ConfigParser &$ConfigParser) {
- $this->ConfigParser_=$ConfigParser;
- }
-
- /**
- *Specifies the ColorAllocator instance to use for in-class scope operations
- *@access public
- *@return void
- *@param ColorAllocator $ColorAllocator
- */
- public function set_ColorAllocator(ColorAllocator &$ColorAllocator) {
- $this->ColorAllocator_=$ColorAllocator;
- }
-
- /**
- *Specifies the Font instance to use for in-class scope operations
- *@access public
- *@return void
- *@param Font $Font
- */
- public function set_Font(Font &$Font) {
- $this->Font_=$Font;
- }
-
- /**
- *Specifies the Caching Strategy the class will follow. Caching Strategy objects provide
- *a common interface to the class for supporting multiple methods of caching the generated images.
- *<br>
- *The available Caching Strategy Objects are:
- *<ul>
- *<li>NullCaching_Strategy. Provides no caching strategy to the class. It should be used when we dont
- *want to cache the generated image</li>
- *<li>CacheToFile_Strategy. Caches the generated image in a file</li>
- *<li>CacheToPEARDB_Strategy. Caches the generated image in a database</li>
- *</ul>
- *For more info read the Strategy Pattern
- *@link http://c2.com/cgi/wiki?StrategyPattern
- *@return void
- *@param mixed $CachingStrategy_Object
- *@param integer $CacheForMinutes Specifies the validity period of the cached image
- *@param mixed $Params Specifies an arbitrary number of parameters, passed to the selected CachingStrategy object
- *@access public
- */
-
- public function set_CachingStrategy(&$CachingStrategy_Object,$CacheForMinutes=NULL,$Params=NULL) {
- $this->Caching_Strategy_=$CachingStrategy_Object;
- ($ImageExists=$this->Caching_Strategy_->Initialize($this->ConfigParser_->get_ImageOutputType(),$CacheForMinutes,$Params)) && exit();
- }
-
- /**
- *Colorizes the image and the calculated chart area with the appropriate colors
- *@return void
- *@access protected
- */
- protected function Colorize_ChartArea() {
-
- //Colorize Image Area
- $ImageColorStart=$this->ConfigParser_->get_ImageColor_Start();
- $ImageColorFinish=$this->ConfigParser_->get_ImageColor_Finish();
- $ImageColorAlpha=$this->ConfigParser_->get_ImageColor_Alpha();
-
- $ImageArea_XSize=$this->ConfigParser_->get_ImageWidth();
- $ImageArea_YSize=$this->ConfigParser_->get_ImageHeight();
- $ImageArea=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),0,0,$ImageArea_XSize,$ImageArea_YSize);
- $ImageArea->draw_Filled($ImageColorStart,$ImageColorFinish,$ImageColorAlpha);
-
- //Colorize Chart Area
- $Chart_ColorStart=$this->ConfigParser_->get_ChartColor_Start();
- $Chart_ColorFinish=$this->ConfigParser_->get_ChartColor_Finish();
- $Chart_ColorAlpha=$this->ConfigParser_->get_ChartColor_Alpha();
-
- $Chart_x_start=$this->ConfigParser_->get_ChartHmargin();
- $Chart_y_start=$this->ConfigParser_->get_ChartVmargin();
- $Chart_x_finish=$this->Area_xsize+$this->ConfigParser_->get_ChartHmargin();
- $Chart_y_finish=$this->Area_ysize+$this->ConfigParser_->get_ChartVmargin();
- $ChartTitle=$this->ConfigParser_->get_ChartTitle();
- $ChartTitleColor=$this->ConfigParser_->get_FontColor();
-
- $ChartArea=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),$Chart_x_start,$Chart_y_start,$Chart_x_finish,$Chart_y_finish);
- $ChartArea->set_Caption($this->Font_,$ChartTitle,$ChartTitleColor);
- $ChartArea->draw_Filled($Chart_ColorStart,$Chart_ColorFinish,$Chart_ColorAlpha);
- }
-
- /**
- *Initializes the Font instance
- *@return void
- *@access protected
- */
- protected function Initialize_Font() {
- $FontFileLocation=$this->ConfigParser_->get_FontFileLocation();
- $FontSize=$this->ConfigParser_->get_FontSize();
- $FontWidth=$this->ConfigParser_->get_FontWidth();
- $FontHeight=$this->ConfigParser_->get_FontHeight();
-
- $this->Font_->set_Properties($this->Chart_Image,$this->ColorAllocator_,$FontFileLocation,$FontSize,$FontWidth,$FontHeight);
- }
-
- /**
- *Calculates the dimensions of the chart drawing area
- *@return void
- *@access protected
- */
- protected function get_DrawingArea_Size() {
- $this->Area_xsize=$this->ConfigParser_->get_ImageWidth()-($this->ConfigParser_->get_ChartHmargin()+$this->ConfigParser_->get_ChartHmargin()/2);
-
- $FontMetrics=$this->Font_->get_FontMetrics("SampleText");
-
- $this->Area_ysize=$this->ConfigParser_->get_ImageHeight()-(2*$this->ConfigParser_->get_ChartVmargin());
- $this->Area_ysize-=$FontMetrics["FontHeight"]*$this->DataParser_->get_GroupItemsNum()+5; //plus 5 more pixels
- }
-
- /**
- *Destructor. Destroyes the Chart Image handler
- */
- function __destruct() {
- ImageDestroy($this->Chart_Image);
- }
-
- /**
- *Draws the legend
- *return void
- *@access protected
- */
- protected function draw_Legend() {
-
- $FontMetrics=$this->Font_->get_FontMetrics("SampleText");
- $FontColor=$this->ConfigParser_->get_FontColor();
-
- $Legend_YPos=2*$this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize; //+18;
- $Legend_XPos_Start=$this->ConfigParser_->get_ChartHmargin();
- $Legend_XPos_End=$this->ConfigParser_->get_ChartHmargin()+$this->Area_xsize;
-
- $Line=new Line($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias());
- $Line->add_Point("$Legend_XPos_Start,$Legend_YPos");
- $Line->add_Point("$Legend_XPos_End,$Legend_YPos");
- $Line->draw($FontColor,0);
-
- $Legend_Rec_Start=$this->ConfigParser_->get_ChartHmargin()/2;
-
- $LegendItems=$this->DataParser_->get_GroupItemsName();
- $LegendColors=$this->ConfigParser_->get_LegendColors();
-
- for($LegendItem=0;$LegendItem<count($LegendItems);$LegendItem++) {
-
- if ($this->ConfigParser_->get_FontFileLocation()!=NULL)
- $Legend_Rec_YStart=$Legend_YPos+($FontMetrics["FontHeight"]/2)+($FontMetrics["FontHeight"]*$LegendItem)+2; //plus 2 pixels from legend line
- else
- $Legend_Rec_YStart=$Legend_YPos+($FontMetrics["FontHeight"]/2-2)+($FontMetrics["FontHeight"]*$LegendItem)+2; //plus 2 pixels from legend line
-
- $Legend_TXT_Pos=$Legend_YPos+($LegendItem*$FontMetrics["FontHeight"])+2;
-
- $Rectangle_Colors=explode(",",$LegendColors[$LegendItem]);
- $Rectangle=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),$Legend_Rec_Start,$Legend_Rec_YStart,$Legend_Rec_Start+4,$Legend_Rec_YStart+4);
- $Rectangle->draw_Filled($Rectangle_Colors[0],$Rectangle_Colors[1],0);
-
- $this->Font_->draw_String($this->ConfigParser_->get_ChartHmargin(),$Legend_TXT_Pos,$LegendItems[$LegendItem],$FontColor);
- }
- }
-
- /**
- *Calls Chart::Colorize_ChartArea(),Chart::get_DrawingArea_Size(),Chart::draw_Grids(),
- *Chart::draw_Legend and Chart::draw() to draw the complete chart image
- *@return void
- *@access public
- */
- public function draw_Chart() {
-
- header("Content-Type: image/".$this->ConfigParser_->get_ImageOutputType());
- header("Cache-Control: no-cache");
-
- switch ($this->ConfigParser_->get_ImageOutputType()) {
-
- case "gif":
-
- if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
- $this->Chart_Image=ImageCreateFromGIF($this->ConfigParser_->get_ChartBgImage());
- else
- $this->Chart_Image=ImageCreate($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
-
- break;
-
- case "jpeg":
-
- if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
- $this->Chart_Image=ImageCreateFromJPEG($this->ConfigParser_->get_ChartBgImage());
- else
- $this->Chart_Image=ImageCreateTrueColor($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
-
- break;
-
- case "png":
-
- if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
- $this->Chart_Image=ImageCreateFromPNG($this->ConfigParser_->get_ChartBgImage());
- else
- $this->Chart_Image=ImageCreateTrueColor($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
-
- }
-
- if ($this->ConfigParser_->get_ImageOutputType()=="png" || $this->ConfigParser_->get_ImageOutputType()=="jpeg") {
-
- if ($this->ConfigParser_->get_ChartUseBlending()=="Yes")
- ImageAlphaBlending($this->Chart_Image,TRUE);
- else
- ImageAlphaBlending($this->Chart_Image,FALSE);
- }
-
- $this->Initialize_Font();
- $this->get_DrawingArea_Size();
- $this->Colorize_ChartArea();
- $this->draw_Grids();
- $this->draw_Legend();
- $this->draw();
-
- switch ($this->ConfigParser_->get_ImageOutputType()) {
- case "png":
- ImagePNG($this->Chart_Image);
-
- break;
-
- case "jpeg":
- ImageJPEG($this->Chart_Image,"",75);
-
- break;
-
- case "gif":
- ImageGIF($this->Chart_Image);
- }
-
- $this->Caching_Strategy_->CacheImage();
-
- }
-
- /**
- *Abstract function,overriden by subclasses to draw the selected chart type (Bar, Line,Area,Pie)
- *@return void
- *@access protected
- *@abstract
- */
- protected abstract function draw();
-
- /**
- /**
- *Draws the grids in the calculated chart area
- *@return void
- *@access protected
- */
- protected function draw_Grids() {
- $Dydat=$this->ConfigParser_->get_GridMaxValue()/$this->ConfigParser_->get_GridNum();
-
- $Gridarea=$this->Area_ysize-15;
- if ($this->ConfigParser_->get_GridMinValue()<0) $Gridarea-=15;
-
- if ($this->ConfigParser_->get_GridMinValue()<0) {
- $Total_Grids=$this->ConfigParser_->get_GridNum()*2;
-
- for ($Grid=0;$Grid<$Total_Grids;$Grid++) {
- $Grid_Val[$Grid]=(int)($this->ConfigParser_->get_GridMinValue()+($Grid*$Dydat));
- $Grid_Val[$this->ConfigParser_->get_GridNum()*2-$Grid]=-$Grid_Val[$Grid];
- }
- $Grid_Val[$this->ConfigParser_->get_GridNum()]=0;
- }
- else
- {
- $Total_Grids=$this->ConfigParser_->get_GridNum();
-
- $Grid_Val[0]=0;
-
- for ($Grid=1;$Grid<=$Total_Grids;$Grid++)
- $Grid_Val[$Grid]=$this->ConfigParser_->get_GridMinValue()+($Grid*$Dydat);
- }
-
- $Dypix=($Gridarea/$Total_Grids);
- $this->Chart_Yscale=$Dypix/$Dydat;
-
- $Grid_StartXPos=$this->ConfigParser_->get_ChartHmargin()-2;
- $Grid_FinishXPos=$this->ConfigParser_->get_ChartHmargin()+$this->Area_xsize;
- $Grid_Color=$this->ConfigParser_->get_GridColor();
- $Grid_FontColor=$this->ConfigParser_->get_FontColor();
-
- for ($Grid=0;$Grid<=$Total_Grids;$Grid++) {
-
- $Grid_Value=(int)($Grid_Val[$Grid]);
- $Grid_Value_Size=$this->Font_->get_FontMetrics($Grid_Value);
- $Grid_Value_XPos=(int)(($this->ConfigParser_->get_ChartHmargin()-$Grid_Value_Size["FontWidth"])/2);
-
- if ($this->ConfigParser_->get_GridMinValue()<0)
- $Grid_YPos=($this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize-15)-($Grid*$Dypix);
- else
- $Grid_YPos=($this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize)-($Grid*$Dypix);
-
- if ($Grid_Value==0) $this->Chart_ZeroPos=$Grid_YPos;
-
- $Grid_Value_YPos=$Grid_YPos-($Grid_Value_Size["FontHeight"]/2);
- $this->Font_->draw_String($Grid_Value_XPos,$Grid_Value_YPos,$Grid_Value,$Grid_FontColor);
-
- $Line=new Line($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias());
- $Line->add_Point("$Grid_StartXPos,$Grid_YPos");
- $Line->add_Point("$Grid_FinishXPos,$Grid_YPos");
- $Line->draw($Grid_Color,0);
- }
-
- }
-
- }
-
- ?>