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

# -----------------------------------------------------------------------------
#
# The base class of all the graphs
#
# -----------------------------------------------------------------------------
#
# Copyright (C) 2003 Christian Eheim and Alex Pachikov
#
# 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 $
#
# -----------------------------------------------------------------------------

include("Text.php");
include("RGB.php");
include("Image.php");
include("Axes.php");
include("Legend.php");

class Graph {
	
	var $_width;		// The width of the image
	var $_height;		// The height of the image

	var $_showLabels;	// A boolean to show or hide the data labels
	var $_showValues;	// A boolean to show or hide the data values
	var $_labelType;	// 'value' or 'percent'
	var $_showLegend;	// A boolean to show or hide the data legend

	var $_labelFont;

	var $_xTicks;		// The tick or grid spacing in the x direction
	var $_yTicks;		// The tick or grid spacing in the y direction
						// 0 for no ticks or grid
	var $_data;			// A hash of labels and values

	# -------------------------------------------
	# Private member variables
	# -------------------------------------------
	var $_image;		// The image to print to
	var $_Axes;
	var $_legend;		// The legend object
	
	# The size of the content area
	var $_contentTop;
	var $_contentLeft;
	var $_contentWidth;
	var $_contentHeight;

	function Graph($width=300,$height=100,$title=null) {
		$this->_width		= $width;
		$this->_height		= $height;

		$this->_data		= array ();

		$this->_showLabels	= true;
		$this->_showValues	= true;
		$this->_showLegend	= true;
		$this->_labelType	= 'value';
		$this->_labelPos	= 'inside';

		$this->_labelFont	= 4;

		$this->_showXAxis	= true;
		$this->_showYAxis	= true;

		$this->_image		= new Image($this->_width,$this->_height,$title);

		$this->_legend		= new Legend($this->_image);

		$this->_Axes		= new Axes($this->_image);
	}

	#------------------------------------------------------------------
	# Public functions
	#------------------------------------------------------------------
	function add_data($data) {
		foreach ($data as $label => $value)
			$this->_data{$label} = $value;
	}
	function set_data($data) {
		$this->_data = array();
		$this->add_data($data);
	}
	function add_data_value($label,$value) {
		$this->_data{$label} = $value;
	}
	

	// Axis properties
	function set_label_font($font) {
		$this->_labelFont = $font;
	}
	function show_labels($bool) {
		$this->_showLabels = $bool;
	}
	function set_label_type($type) {
		$this->_labelType = $type; // 'value' or 'percent'
	}
	function set_label_position($pos) {
		$this->_labelPos = $pos; // 'inside' or 'outside'
	}
	// Set the image properties
	function set_bgcolor($color) {
		$this->_image->set_bgcolor($color);
	}
	function set_type($type) {
		$this->_image->set_type($type);
	}
	function set_border($bool,$width=1,$color="#000000") {
		$this->_image->set_border($bool,$width,$color);
	}
	function set_margin($top,$right,$bottom,$left) {
		$this->_image->set_margin($top,$right,$bottom,$left);
	}
	function set_title_color($color) {
		$this->_image->set_title_color($color);
	}
	function set_title_font($font) {
		$this->_image->set_title_font($font);
	}
	function set_title_margin($margin) {
		$this->_image->set_title_margin($margin);
	}
	// Set the legend properties
	function legend_show($bool) {
		$this->_legend->set_show($bool);
	}
	function set_legend_position($pos) {
		$this->_legend->set_position($pos);
	}
	function set_legend_bgcolor($color) {
		$this->_legend->set_bgcolor($color);
	}
	function set_legend_font($font) {
		$this->_legend->set_font($font);
	}
	function set_legend_margin($margin) {
		$this->_legend->set_margin($margin);
	}
	function set_legend_padding($padding) {
		$this->_legend->set_padding($padding);
	}
	function legend_show_border($bool) {
		$this->_legend->set_show_border($bool);
	}
	function set_legend_border_color($color) {
		$this->_legend->set_border_color($color);
	}
	function legend_show_shadow($bool) {
		$this->_legend->set_shadow($bool);
	}
	function set_legend_shadow_color($color) {
		$this->_legend->set_shadow_color($color);
	}
	function set_legend_shadow_width($width) {
		$this->_legend->set_shadow_width($width);
	}
	function set_legend_alignment($align) {
		$this->_legend->set_alignment($align);
	}
	// Set the axis properties
	function set_plot_bgcolor($color) {
		$this->_Axes->_bgcolor = $color;
	}
	function x_axis_show($bool=true) {
		$this->_Axes->set_x_show($bool);	
	}
	function y_axis_show($bool=true) {
		$this->_Axes->set_y_show($bool);	
	}
	function set_x_axis($title,$pos="bottom",$color="#555555") {
		$this->_Axes->set_x_title($title);
		if ($pos == "top" || $pos == "bottom")
			$this->_Axes->set_x_position($pos);
		$this->_Axes->set_x_color($color);
	}
	function set_y_axis($title,$pos="left",$color="#555555") {
		$this->_Axes->set_y_title($title);
		if ($pos == "left" || $pos == "right")
			$this->_Axes->set_y_position($pos);
		$this->_Axes->set_y_color($color);
	}
	function set_x_axis_title($title) {
		$this->_Axes->set_x_title($title);
	}
	function set_y_axis_title($title) {
		$this->_Axes->set_y_title($title);
	}
	function set_x_axis_color($color) {
		$this->_Axes->set_x_color($color);
	}
	function set_y_axis_color($color) {
		$this->_Axes->set_y_color($color);
	}
	function set_x_axis_position($pos) {
		if ($pos == "top" || $pos == "bottom")
			$this->_Axes->set_x_position($pos);
	}
	function set_y_axis_position($pos) {
		if ($pos == "left" || $pos == "right")
			$this->_Axes->set_y_position($pos);
	}
	function set_x_axis_font($font) {
		$this->_Axes->set_x_font($font);
	}
	function set_y_axis_font($font) {
		$this->_Axes->set_y_font($font);
	}
	function set_x_axis_min_value($value) {
		$this->_Axes->set_x_min_value($value);
	}
	function set_y_axis_min_value($value) {
		$this->_Axes->set_y_min_value($value);
	}
	function set_x_axis_max_value($value) {
		$this->_Axes->set_x_max_value($value);
	}
	function set_y_axis_max_value($value) {
		$this->_Axes->set_y_max_value($value);
	}
	function get_x_axis_min_value() {
		return $this->_Axes->get_x_min_value();
	}
	function get_x_axis_max_value() {
		return $this->_Axes->get_x_max_value();
	}
	function get_y_axis_min_value() {
		return $this->_Axes->get_y_min_value();
	}
	function get_y_axis_max_value() {
		return $this->_Axes->get_y_max_value();
	}
	function set_x_axis_title_margin($value) {
		$this->_Axes->set_x_title_margin($value);
	}
	function set_y_axis_title_margin($value) {
		$this->_Axes->set_y_title_margin($value);
	}
	function set_x_axis_label_up($bool) {
		$this->_Axes->set_x_label_up($bool);
	}
	function set_y_axis_label_up($bool) {
		$this->_Axes->set_y_label_up($bool);
	}
	function set_x_axis_label_font($font) {
		$this->_Axes->set_x_label_font($font);
	}
	function set_y_axis_label_font($font) {
		$this->_Axes->set_y_label_font($font);
	}
	function set_x_axis_num_ticks($num) {
		$this->_Axes->set_x_num_ticks($num);
	}
	function set_y_axis_num_ticks($num) {
		$this->_Axes->set_y_num_ticks($num);
	}
	function get_x_axis_num_ticks() {
		return $this->_Axes->get_x_num_ticks();
	}
	function get_y_axis_num_ticks() {
		return $this->_Axes->get_y_num_ticks();
	}
	function set_x_axis_longest($lab) {
		$this->_Axes->set_x_longest($lab);
	}
	function set_y_axis_longest($lab) {
		$this->_Axes->set_y_longest($lab);
	}
	function set_x_axis_show_grid($bool) {
		$this->_Axes->set_x_show_grid($bool);
	}
	function set_y_axis_show_grid($bool) {
		$this->_Axes->set_y_show_grid($bool);
	}
	function set_x_axis_labels($labels) {
		$this->_Axes->set_x_labels($labels);
	}
	function set_y_axis_labels($labels) {
		$this->_Axes->set_y_labels($labels);
	}
	

	# --------------------------------------------------------------------
	# Private functions
	# --------------------------------------------------------------------

	# Sort the array from the lowest up
	function sort_up() {
		asort($this->_data);
	}
	# Sort the array from the highest down
	function sort_down() {
		arsort($this->_data);
	}
	# Get the sum of all the values
	function sum() {
		return array_sum($this->_data);
	}
	# Find the min and the max of the data values
	function minmax() {
		$tmpData = $this->_data;
		asort($tmpData);
		reset($tmpData);
		$min = &$tmpData[key($tmpData)];
		end($tmpData);
		$max = &$tmpData[key($tmpData)];
		return array($min,$max);
	}
	# Returns the longest label in the data set
	function get_longest() {
		$str = "";
		$val = "";
		foreach ($this->_data as $label => $value) {
			if (strlen($label) > strlen($str)) $str = $label;
			if (strlen($value) > strlen($val)) $val = $value;
		}
		return array($str,$val);
	}
	#------------------------------------------------------------------
	# Create the image
	# This funcion is implemented in the extended classes
	#------------------------------------------------------------------
	function create_bg() {
		$c = $this->_image->_rgb->allocate_color("#FFFFFF");
		ImageFilledRectangle(
                $this->_image->_image,
                $this->_plotLeft, $this->_plotTop,
                $this->_plotLeft + $this->_plotWidth-1,
                $this->_plotTop + $this->_plotHeight-1,
                $c
		);

	}

	#------------------------------------------------------------------
	# Draw the image
	#------------------------------------------------------------------
	function draw() {
		# Create the image
		$this->_image->init();

		# Genereate the legend and draw it
		$this->generate_legend();
		$this->_legend->draw();


		# Get the plot area without the legend
		# Just temp !!!!!!!!!!!!!!!!!!!
		list(
			$this->_plotLeft, $this->_plotTop,
			$this->_plotWidth, $this->_plotHeight
		) = $this->_image->get_content_area();

#		$this->create_bg();
		# Generate the axesa and draw them
		$this->generate_axes();
		$this->_Axes->draw();
		# Create the plot 
		$this->create_graph();

		# Draw the image
		$this->_image->draw();
	}
}

?>
Return current item: TVEz - Media Library