<?php
class drpChart
{	
	var $pbChartType		= "";			// [number] 1:Pie, 2:Bar, 3:Line
 	var $pbChartWidth		= "";			// [number] Width of Chart
 	var $pbChartHeight		= "";			// [number] Height of Chart
 	var $pbChartShadow		= "";			// [booleen] 0:No shadow, 1:Shadow
 	var $pbChartTitle		= "";			// [string] Title Name of Chart
 	var $pbChartBgColor		= "";			// [string RGB] Background Chart
 	
 	var $pbLegendFontSize	= "";			// [number] Legend font size
 	var $pbLegendFontName	= "";			// [string] Legend font name
 	
 	var $pbTitleBgColor		= "";			// [string RGB] Title BgColor
 	var $pbTitleFontName	= "";			// [string] Title Font name
 	var $pbTitleFontSize	= "";			// [number] Title Font size
 	var $pbTitleBorderWidth	= "";			// [number] Title Border width
 	var $pbTitleShadow		= "";			// [booleen] 0:NotShow, 1:Show
 	
 	var $pbRowMember		= "";			// [number] How many row of array
 	var $pbArrMember		= "";			// [array] Data Array(n, 3) {FORMAT: (LegendCode[n,0], LegendName[n,1], value[n,2], url[n,3])}
 	
	function debug()
	{
		return $this->CalcTotal();
	}
	
	function drpChart()
	{
		$this->pbChartType 			= 1;
  		$this->pbChartWidth 		= 180;
 	 	$this->pbChartHeight 		= 180;
  		$this->pbChartShadow 		= 1;
  		$this->pbChartTitle 		= "drp Chart";
  		$this->pbChartBgColor 		= "#FFFFFF";   		
  		$this->pbTitleBgColor 		= "#ECE9D8";
  		$this->pbTitleFontName 		= "Vernada";
  		$this->pbTitleFontSize 		= 12;
	}
	
  	function  PropInit()
  	{
		if(empty($this->pbChartShadow))
		{
    		$p_btnChartShadow = "True";
		}else
		{
    		$p_btnChartShadow = $this->pbChartShadow;
		}
    
    	if(is_null($this->pbChartBgColor))
    	{
    		$p_strChartBgColor = "#FFFFFF";
    	}else
    	{
    		$p_strChartBgColor = $this->pbChartBgColor;    		
    	}
    
		return sizeof($this->pbArrMember) + 1;  		
  	}
  	  	
  	function CalcTotal()
  	{
  		$mTotalTemp = 0;
		for($i=1 ; $i <= sizeof($this->pbArrMember) ; $i++)
		{
			$mTotalTemp += $this->pbArrMember[$i-1][2];
		}
		return $mTotalTemp;
  	}
  	
  	function ClearValue()
  	{
    	$this->p_strChart = "";    
	}
       
    function GenVML()
    {
    	$mStrColor	= array("#468DF6","#00C7CE","#BFD7FB");
    	
    	$p_RowMember 	= $this->PropInit();		// Check properties
    	$p_TotalValue 	= $this->CalcTotal();		// collect total value of array
    	    	
    	switch($this->pbChartType)
    	{
    		case "1" :
		 			$mPie = 3.141592;		// Pie value
					
					$mIndentTitle		= 30;		// indent from Top of Border
					$mIndentUnderTitle  = 50;		// indent from under Title 
					$mIndentLeft 		= 70;		// indent from left border for render label		
					
		       		if($this->pbChartHeight < 300)
		       		{
						$mIndentTitle 	= 60;
						$mIndentLeft 	= 60;
					}		
					
					if($this->pbChartWidth > $this->pbChartHeight)
					{
						$mPieSize	= $this->pbChartHeight * 0.2;
					}else
					{
						$mPieSize	= $this->pbChartWidth * 0.2;
					}
		
					// retrive mPieSize and plus align left(X), top(Y)
					$mMoveToX	= $mPieSize + $mIndentLeft;
					$mMoveToY	= $mPieSize + ($mIndentTitle + $mIndentUnderTitle);
				
					// create area by v:group
					$p_strChart .= "<v:group style='antialias:true; position:relative; width:" . $this->pbChartWidth. "px; height:".$this->pbChartHeight."px; left:0px; top:0px;' coordsize='".$this->pbChartWidth.", ".$this->pbChartHeight."'>\n";
					
					$mLineNavPx	= 30;
					$mRunning = 0;
					
					for($i = 1 ; $i < $p_RowMember ; $i++)					
					{
						$mStartAngle	= 360 * ($mRunning / $p_TotalValue);
						$mRunning	   += $this->pbArrMember[$i-1][2];
						$mEndAngle		= (360 * ($mRunning / $p_TotalValue)) - $mStartAngle;
			
						$mAngleCenter	= $mStartAngle + (($mEndAngle) / 2);
			
						$mStartValue	= (integer)($mStartAngle * pow(2,16));
						$mEndValue		= (integer)($mEndAngle * pow(2,16));
									
			
						$p_strChart .= "<v:shape onMouseOver=\"this.strokecolor='#FF0000'; this.strokeweight='1px';\" OnMouseOut=\"this.strokecolor='White';this.strokeweight='0.25px';\" ";
						
						if($this->pbArrMember[$i-1][3] != "")
						{
							$p_strChart .= "OnClick=\"alert(this.title);\" ";
						}
						
						$p_strChart .= "title='".$this->pbArrMember[$i-1][0].": ".number_format($this->pbArrMember[$i-1][2], 0, ".","")." MB";
						$p_strChart .= " [" .number_format(100*($this->pbArrMember[$i-1][2]/$p_TotalValue), 1, ".", "")."%]' ";
						$p_strChart .= "id='" . $this->pbArrMember[$i-1][0] . "' style='cursor:hand; width:".$this->pbChartWidth;
						$p_strChart .= "px; height:" . $this->pbChartHeight . "px;' strokecolor='white' strokeweight='.25px' ";
						$p_strChart .= "path='M " . $mMoveToX . " " . $mMoveToY . " AE " . $mMoveToX . " " . $mMoveToY;
						$p_strChart .= " " . $mPieSize . " " . $mPieSize . " " . $mStartValue . " " . $mEndValue . " X E'>\n";
						
						$p_strChart .= "<v:fill type='gradient' color='" . $mStrColor[$i-1]. "' color2='";
						$p_strChart .= $mStrColor[$i-1]."' angel='20' opacity='50%' />\n";
						$p_strChart .= "</v:shape>\n";
						
						if($mAngleCenter >=0 && $mAngleCenter <=90)
						{
							// Top Right quadrant
							$iStartY		= -1 * ($mPieSize * sin($mAngleCenter * ($mPie / 180)));
							$iStartX		= 1 * ($mPieSize * cos($mAngleCenter * ($mPie / 180)));
							$iEndY 			= -1 * (($mPieSize+$mLineNavPx) * sin($mAngleCenter * ($mPie / 180)));
							$iEndX 			= 1 * (($mPieSize+$mLineNavPx) * cos($mAngleCenter * ($mPie / 180)));
							$sBoxPos 		= "position:absolute;left:". ($mMoveToX + $iEndX) - 5 . ";top:".($mMoveToY + $iEndY) - 10  . ";text-align:left;width:120;height:20;";
						}else if($mAngleCenter > 90 && $mAngleCenter <= 180)
						{
							// Top Left quadrant
							$mAngleCenter 	= 180 - $mAngleCenter;
							$iStartY 		= -1 * ($mPieSize * sin($mAngleCenter * ($mPie / 180)));
							$iStartX 		= -1 * ($mPieSize * cos($mAngleCenter * ($mPie / 180)));
							$iEndY 			= -1 * (($mPieSize+$mLineNavPx) * sin($mAngleCenter * ($mPie / 180)));
							$iEndX 			= -1 * (($mPieSize+$mLineNavPx) * cos($mAngleCenter * ($mPie / 180)));							
							$sBoxPos 		= "position:absolute;left:" .($mMoveToX + $iEndX) - 120 . ";top:".($mMoveToY + $iEndY) - 10 . ";text-align:right;width:120;height:20;";
						}else if($mAngleCenter > 180 && $mAngleCenter <= 270)
						{
							// Bottom Left quadrant
							$mAngleCenter 	= 180 + $mAngleCenter;
							$iStartY 		= 1 * ($mPieSize * sin($mAngleCenter * ($mPie / 180)));
							$iStartX 		= -1 * ($mPieSize * cos($mAngleCenter * ($mPie / 180)));
							$iEndY 			= 1 * (($mPieSize+$mLineNavPx) * sin($mAngleCenter * ($mPie / 180)));
							$iEndX 			= -1 * (($mPieSize+$mLineNavPx) * cos($mAngleCenter * ($mPie / 180)));							
							$sBoxPos 		= "position:absolute;left:" . ($mMoveToX + $iEndX) - 120 . ";top:".($mMoveToY + $iEndY) + 5 . ";text-align:right;width:120;height:20;";
						}else if($mAngleCenter > 270 && $mAngleCenter <= 360)
						{
							// Bottom Right quadrant
							$mAngleCenter 	= 360 - $mAngleCenter;
							$iStartY 		= 1 * ($mPieSize * sin($mAngleCenter * ($mPie / 180)));
							$iStartX 		= 1 * ($mPieSize * cos($mAngleCenter * ($mPie / 180)));
							$iEndY 			= 1 * (($mPieSize+$mLineNavPx) * sin($mAngleCenter * ($mPie / 180)));
							$iEndX 			= 1 * (($mPieSize+$mLineNavPx) * cos($mAngleCenter * ($mPie / 180)));
							$sBoxPos 		= "position:absolute;left:" . ($mMoveToX + $iEndX) . ";top:".($mMoveToY + $iEndY) + 5  . ";text-align:left;width:120;height:20;";
						}
					}	
    				break;
    		default :
    				$p_strChart .= "Case Else";
    				break;
    	}
    	
    	$p_strChart .= "</v:group>";
       	
       	echo $p_strChart;
       	$this->ClearValue();
    }
}
?>