Location: PHPKode > scripts > phpchartPlus > phpchartplus/Examples/Example.php
<?php

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

/*
****************************************************************
*CHOOSE FROM EXAMPLES 0 TO 3 AND SPECIFY THE NEEDED PARAMETERS TO HAVE
*A WORKING EXAMPLE
****************************************************************
*EXAMPLE=0
****************************************************************
*Strategy Objects used: NullCache_Strategy ->No caching for the generated image
*			          TXTData_Strategy ->Imports chart data from TXT Files
*				  PHPConfig_Strategy ->Imports configuration data from PHP Arrays
*Chart Type: BarChart
*Font: ActionMan.ttf
****************************************************************
*EXAMPLE=1
****************************************************************
*Strategy Objects used: CacheToFile_Strategy ->Caches the generated image in a file
				  PEARDBData_Strategy ->Imports chart data from Databases
				  XMLConfig_Strategy ->Imports configuration data from XML 
				  files
*Chart Type: LineChart
*Font: Aristocat.ttf
****************************************************************
*EXAMPLE=2
****************************************************************
*Strategy Objects used: CacheToPEARDB_Strategy ->Caches the generated image in a 
				  Database
				  XMLData_Strategy -> Imports chart data from XML Files
				  PHPConfig_Strategy ->Imports configuration data from PHP Arrays
*Chart Type: AreaChart
*Font: Built-in Font
*Background: bgimage.png
****************************************************************
*EXAMPLE=3
****************************************************************
*Strategy Objects used: NullCache_Strategy ->No caching for the generated image
				  PHPData_Strategy ->Imports chart data from PHP Arrays
				  PHPConfig_Strategy ->Imports configuration data from PHP Arrays
*Chart Type: PieChart
*Font: ActionMan.ttf
****************************************************************				  
*/

define("EXAMPLE",0);

switch (EXAMPLE) {

	case 0:
	
	include("../Sources/phpchartPlus.php");
	include("../Sources/NullCache_Strategy.php");
	include("../Sources/TXTData_Strategy.php");
	include("../Sources/PHPConfig_Strategy.php");
	
	$ConfigParams=array("ImageWidth"=>420,
                   	              "ImageHeight"=>300,
                    	              "ImageOutputType"=>"png",
                    		      "ChartTitle"=>"Web Servers Usage",
                    		      "ChartType"=>"Bar",
                    		      "ChartUseBlending"=>"Yes",
                    		      "ChartUseAntialias"=>"Yes",
                    		      "ChartHmargin"=>30,
                    		      "ChartVmargin"=>30,
                    		      "ChartUseStatus"=>"Yes",
                    		      "ImageColor_Start"=>"DimGray",
                    		      "ImageColor_Finish"=>"DimGray",
                    		      "ImageColor_Alpha"=>0,
                    		      "ChartColor_Start"=>"DimGray",
                    		      "ChartColor_Finish"=>"FloralWhite",
                    		      "ChartColor_Alpha"=>0,
                    		      "FontColor"=>"White",
                    		      "FontWidth"=>3,
                    		      "FontHeight"=>3,
                    		      "FontFileLocation"=>"../Fonts/ActionMan.ttf",
                   		      "FontSize"=>9,
                    		      "LegendColors"=>array("DarkRed,Yellow","Black,DarkGreen"),
                    		      "LegendColors_Alpha"=>array(0,0),
                    		      "GridNum"=>4,
                    		      "GridMinValue"=>0,
                    		      "GridMaxValue"=>100,
                    		      "GridColor"=>"Black");
	
	$DataParams=array("filename"=>"../Data/ChartData.txt");
	
	$DataStrategy=new TXTData_Strategy();
	$ConfigStrategy=new PHPConfig_Strategy();
	$CacheStrategy=new NullCache_Strategy();

	$im=new phpchartPlus();
	
	try 
	{
		$im->DataParser_->strategize($DataStrategy,$DataParams);
		$im->DataParser_->parse();
	}
		catch (DataParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (DataStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}
	
	try
	{
		$im->ConfigParser_->strategize($ConfigStrategy);
		$im->ConfigParser_->parse($ConfigParams);
	}
		catch (ConfigParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (ConfigStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}
	
	try
	{	
		$im->get_Instance();	
	}
		catch (ImageSupport_Exception $e)
		{
			print $e->getMessage();
			exit();
		}
		catch (FontSupport_Exception $f)
		{
			print $f->getMessage();
			exit();
		}
	
	$im->Chart_->set_CachingStrategy($CacheStrategy);	
	$im->Chart_->draw_Chart();
		
	break;
	
	case 1:
	
	include("../Sources/phpchartPlus.php");
	include("../Sources/CacheToFile_Strategy.php");
	include("../Sources/PEARDBData_Strategy.php");
	include("../Sources/XMLConfig_Strategy.php");
	
	$DataParams=array("DB_Type"=>"",
			      	    "DB_Username"=>"",
			            "DB_Passwd"=>"",
			            "DB_Host"=>"",
			            "DB_Name"=>"",
			            "Field_GroupName"=>"",
			            "Field_ItemName"=>"",
			            "Field_ItemValue"=>"");
				    
	$ConfigParams=array("filename"=>"../Data/phpchartplus.conf.xml");			    	
	
	$DataStrategy=new PEARDBData_Strategy();
	$ConfigStrategy=new XMLConfig_Strategy();
	$CacheStrategy=new CacheToFile_Strategy();

	$im=new phpchartPlus();
	
	try
	{
		$im->DataParser_->strategize($DataStrategy,$DataParams);
		$im->DataParser_->parse(array("Query"=>"SELECT * FROM my_table"));
	}
		catch (DataParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (DataStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}
	
	try
	{
		$im->ConfigParser_->strategize($ConfigStrategy,$ConfigParams);
		$im->ConfigParser_->parse();
	}
		catch (ConfigParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (ConfigStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}	
	
	try
	{
		$im->get_Instance();
	}
		catch (ImageSupport_Exception $e)
		{
			print $e->getMessage();
			exit();
		}
		catch (FontSupport_Exception $f)
		{
			print $f->getMessage();
			exit();
		}
		
	$im->Chart_->set_CachingStrategy($CacheStrategy,2); //Cache for 2 minutes	
	$im->Chart_->draw_Chart();
	
	break;
	
	case 2:
	
	include("../Sources/phpchartPlus.php");
	include("../Sources/CacheToPEARDB_Strategy.php");
	include("../Sources/XMLData_Strategy.php");
	include("../Sources/PHPConfig_Strategy.php");
	
	$CacheParams=array("DB_Type"=>"",
			      	      "DB_Username"=>"",
			      	      "DB_Passwd"=>"",
			      	      "DB_Host"=>"",
			      	      "DB_Name"=>"",
			      	      "DB_Table"=>"",
			      	      "Field_ImageName"=>"",
			      	      "Field_ImageData"=>"",
			      	      "Field_ImageModification"=>"");
	
	$DataParams=array("filename"=>"../Data/ChartData.xml");
	
	$ConfigParams=array("ImageWidth"=>420,
                   	              "ImageHeight"=>300,
                    	              "ImageOutputType"=>"png",
                    		      "ChartTitle"=>"Web Servers Usage",
                    		      "ChartType"=>"Area",
                    		      "ChartUseBlending"=>"Yes",
                    		      "ChartUseAntialias"=>"Yes",
                    		      "ChartHmargin"=>30,
                    		      "ChartVmargin"=>25,
                    		      "ChartUseStatus"=>"Yes",
				      "ChartBgImage"=>"../Data/bgimage.png",
                    		      "ImageColor_Start"=>"DimGray",
                    		      "ImageColor_Finish"=>"DimGray",
                    		      "ImageColor_Alpha"=>70,
                    		      "ChartColor_Start"=>"DimGray",
                    		      "ChartColor_Finish"=>"FloralWhite",
                    		      "ChartColor_Alpha"=>50,
                    		      "FontColor"=>"White",
                    		      "FontWidth"=>3,
                    		      "FontHeight"=>3,
                    		      "LegendColors"=>array("DarkRed,Yellow","Black,DarkGreen"),
                    		      "LegendColors_Alpha"=>array(30,60),
                    		      "GridNum"=>4,
                    		      "GridMinValue"=>0,
                    		      "GridMaxValue"=>100,
                    		      "GridColor"=>"Black");
	
	$DataStrategy=new XMLData_Strategy();
	$ConfigStrategy=new PHPConfig_Strategy();
	$CacheStrategy=new CacheToPEARDB_Strategy();

	$im=new phpchartPlus();
	
	try
	{
		$im->DataParser_->strategize($DataStrategy,$DataParams);
		$im->DataParser_->parse();
	}
		catch (DataParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (DataStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}

	try
	{
		$im->ConfigParser_->strategize($ConfigStrategy);
		$im->ConfigParser_->parse($ConfigParams);
	}
		catch (ConfigParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (ConfigStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}	
			
	try
	{
		$im->get_Instance();
	}
		catch (ImageSupport_Exception $e)
		{
			print $e->getMessage();
			exit();
		}
		catch (FontSupport_Exception $f)
		{
			print $f->getMessage();
			exit();
		}
		
	try
	{
		$im->Chart_->set_CachingStrategy($CacheStrategy,2,$CacheParams); //Cache for 2 minutes
	}
		catch (CachingStrategy_Exception $e)
		{
			print $e->getMessage();
			exit();
		}
		
	$im->Chart_->draw_Chart();	
	
	break;		      		

	case 3:
	
	include("../Sources/phpchartPlus.php");
	include("../Sources/NullCache_Strategy.php");
	include("../Sources/PHPData_Strategy.php");
	include("../Sources/PHPConfig_Strategy.php");
	
	$Group1=array("GroupName"=>"12:00",
			      "Items"=>array(
							array("ItemName"=>"Talos","ItemValue"=>40),
							array("ItemName"=>"Dilos","ItemValue"=>60)
						    ));
							      
	$Group2=array("GroupName"=>"13:00",
			      "Items"=>array(
							array("ItemName"=>"Talos","ItemValue"=>30),
							array("ItemName"=>"Dilos","ItemValue"=>70)
					            ));
	
	$DataParams=array($Group1,$Group2);
	
	$ConfigParams=array("ImageWidth"=>420,
                   	              "ImageHeight"=>300,
                    	              "ImageOutputType"=>"png",
                    		      "ChartTitle"=>"Web Servers Usage",
                    		      "ChartType"=>"Pie",
                    		      "ChartUseBlending"=>"Yes",
                    		      "ChartUseAntialias"=>"Yes",
                    		      "ChartHmargin"=>30,
                    		      "ChartVmargin"=>30,
                    		      "ChartUseStatus"=>"Yes",
                    		      "ImageColor_Start"=>"DimGray",
                    		      "ImageColor_Finish"=>"DimGray",
                    		      "ImageColor_Alpha"=>0,
                    		      "ChartColor_Start"=>"DimGray",
                    		      "ChartColor_Finish"=>"FloralWhite",
                    		      "ChartColor_Alpha"=>0,
                    		      "FontColor"=>"White",
                    		      "FontWidth"=>3,
                    		      "FontHeight"=>3,
                    		      "FontFileLocation"=>"../Fonts/ActionMan.ttf",
                   		      "FontSize"=>9,
                    		      "LegendColors"=>array("DarkRed,Yellow","Black,DarkGreen"),
                    		      "LegendColors_Alpha"=>array(0,0),
                    		      "GridNum"=>4,
                    		      "GridMinValue"=>0,
                    		      "GridMaxValue"=>100,
                    		      "GridColor"=>"Black");
	
	$DataStrategy=new PHPData_Strategy();
	$ConfigStrategy=new PHPConfig_Strategy();
	$CacheStrategy=new NullCache_Strategy();

	$im=new phpchartPlus();

	$im->DataParser_->strategize($DataStrategy);
	$im->DataParser_->parse($DataParams);

	try
	{
		$im->ConfigParser_->strategize($ConfigStrategy);
		$im->ConfigParser_->parse($ConfigParams);
	}
		catch (ConfigParser_Exception $e) 
		{
			print $e->getMessage();
			exit();
		}
		catch (ConfigStrategy_Exception $f) 
		{
			print $f->getMessage();
			exit();
		}	
	
	try
	{		
		$im->get_Instance();
	}
		catch (ImageSupport_Exception $e)
		{
			print $e->getMessage();
			exit();
		}
		catch (FontSupport_Exception $f)
		{
			print $f->getMessage();
			exit();
		}
		
	$im->Chart_->set_CachingStrategy($CacheStrategy);
	$im->Chart_->draw_Chart();			      		
}

?>
Return current item: phpchartPlus