Location: PHPKode > projects > TVEz - Media Library > tvez-0.2.1/html/shared/Graph/PieChart3d.php
<?php

# -----------------------------------------------------------------------------
#
# Plots a 3D pie chart
#
# -----------------------------------------------------------------------------
#
# Copyright (C) 2004 Christian Eheim
#
# This file is part of TVEz (tvez.sourceforge.net).
#
# TVEz is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# TVEz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TVEz; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# -----------------------------------------------------------------------------
#
# Created on 02/01/2004 by Christian Eheim (hide@address.com)
#
# LAST MODIFIED:
# $Date: 2004/02/03 00:02:17 $
# $Revision: 1.2 $
# $Author: eheim $
#
# -----------------------------------------------------------------------------

/*
-------------------------------------------------------------------------------
Has all functioanality of the 2D Pie Chart and
in addition has an eccentricity
-> set_eccentricity(fraction)
-------------------------------------------------------------------------------
*/

include("PieChart.php");

class PieChart3d extends PieChart {
	
	var $_ecc;	// Eccentricity of the ellipse

	function PieChart3d($width,$height,$title=null) {
		# Instantiate the base class
		$this->PieChart($width,$height,$title);

		$this->_ecc = 0.7;
	}

	#
	# Set the eccentricity
	#
	function set_eccentricity($ecc) {
		$this->_ecc	= $ecc;
	}
	#
	# The Pie Chart graphing function
	#
	function create_graph() {

		list($min,$max) = $this->minmax();
		
		# Compute the center and the size
		$this->compute();

		$black = $this->_image->_rgb->allocate_color("black");
		$colors = array_keys($this->_image->_rgb->_colorTable);
		$n = count($this->_themes[$this->_theme][0]);
		$height = $this->_ecc*$this->_diameter;
		$count = count($this->_data);
		#
		# Allocate the colors
		#
		$i=0;
		foreach ($this->_data as $label => $value) {
			$color[$i] = $this->_image->_rgb->allocate_color($colors[$this->_themes{$this->_theme}[1][$i%$n]]);
			$i++;
		}
		#
		# Draw the 3D effect
		#
		for($j=round($this->_radius*0.2); $j>0; $j--) {
			$start = 270;
			$i = 0;
			foreach ($this->_data as $label => $value) {
				$end = (round($value/$this->_sum*360)+$start)%360;
				# Hack for only one slice !!!
				if ($count == 1) $end -= 2;
				ImageFilledArc(
					$this->_image->_image,
					$this->_xCenter, $this->_yCenter+$j,
					$this->_diameter, $height,
					$start, $end,
					$color[$i], IMG_ARC_PIE
				);
				$start = $end;
				$i++;
			}
		}
		#
		# Draw the slices
		#
		$start = 270;
		$i = 0;
		foreach ($this->_data as $label => $value) {
			# Allocate a color for this slice
			$color = $this->_image->_rgb->allocate_color($colors[$this->_themes{$this->_theme}[0][$i%$n]]);
			# Compute the percentage
			$percent = sprintf("%.1f",$value/$this->_sum*100);
			# Compute the end angle
			$end = (round($value/$this->_sum*360)+$start)%360;
			#
			# Fill the slice
			#
			# Hack for only one slice !!!
			if ($count == 1) {
				ImageFilledArc(
					$this->_image->_image,
					$this->_xCenter, $this->_yCenter,
					$this->_diameter, $height,
					268, 270,
					$color, IMG_ARC_PIE
				);
				$end -= 2;
			}
			ImageFilledArc(
				$this->_image->_image,
				$this->_xCenter, $this->_yCenter,
				$this->_diameter, $height,
				$start, $end,
				$color, IMG_ARC_PIE
			);
			#
			# If the slice is big enough, write the percentage inside
			# let's assume 10 degrees
			#
			if ( $this->_showLabels && ($end-$start+360)%360 > 10 ) {
				if ($this->_labelType == "percent")
					$pt = new Text($percent."%",$this->_labelFont);
				else
					$pt = new Text("$value",$this->_labelFont);
				$tw = round($pt->get_width()/2);
				$th = round($pt->get_height()/2);
				$pt->set_color("black");
				list($dx,$dy) = $this->compute_label_pos(
					$start,$end);
				$pt->set_pos(
					$this->_xCenter+$dx-$tw,
					$this->_yCenter+$dy-$th
				);
				$pt->draw($this->_image);
			}
			#
			# Draw the slice border
			#
			if ($this->_sliceBorder) {
				ImageFilledArc(
					$this->_image->_image,
					$this->_xCenter, $this->_yCenter,
					$this->_diameter+1, $height+1,
					$start, $end,
					$black, IMG_ARC_EDGED|IMG_ARC_NOFILL
				);
			}
			# Make the new starting angle the end of this one
			$start = $end;
			$i++;
		}
		#
		# Draw a border around the entire pie
		#
		if ($this->_pieBorder) {
			ImageEllipse(
				$this->_image->_image,
				$this->_xCenter, $this->_yCenter,
				$this->_diameter+1, $height+1,
				$black
			);
		}
	}
}

?>
Return current item: TVEz - Media Library