<?php
# ------------------------------------------------------------------------------
#
# Draws the x- and y-axis
#
# ------------------------------------------------------------------------------
#
# 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/09/12 00:08:48 $
# $Revision: 1.3 $
# $Author: eheim $
#
# ------------------------------------------------------------------------------
include("Axis.php");
class Axes {
var $_image;
var $_xAxis;
var $_yAxis;
var $_tickSize;
var $_bgcolor;
# The size of the plot area
var $_plotTop;
var $_plotLeft;
var $_plotWidth;
var $_plotHeight;
# The size of the plot area
var $_graphTop;
var $_graphLeft;
var $_graphWidth;
var $_graphHeight;
var $_xLabelHeight;
var $_xLabelWidth;
var $_yLabelHeight;
var $_yLabelWidth;
function Axes(&$image) {
$this->_image = &$image;
$this->_xAxis = new Axis($this->_image,"X-Axis","bottom");
$this->_yAxis = new Axis($this->_image,"Y-Axis","left");
$this->_bgcolor = "#dddddd";
$this->_tickSize = 3;
}
function set_x_show($bool) {
$this->_xAxis->set_show($bool);
}
function set_y_show($bool) {
$this->_yAxis->set_show($bool);
}
function set_x_title($title) {
$this->_xAxis->set_title($title);
}
function set_y_title($title) {
$this->_yAxis->set_title($title);
}
function set_x_position($pos) {
$this->_xAxis->set_position($pos);
}
function set_y_position($pos) {
$this->_yAxis->set_position($pos);
}
function set_x_color($color) {
$this->_xAxis->set_color($color);
}
function set_y_color($color) {
$this->_yAxis->set_color($color);
}
function set_x_font($font) {
$this->_xAxis->set_font($font);
}
function set_y_font($font) {
$this->_yAxis->set_font($font);
}
function set_x_min_value($value) {
$this->_xAxis->set_min_value($value);
}
function set_y_min_value($value) {
$this->_yAxis->set_min_value($value);
}
function set_x_max_value($value) {
$this->_xAxis->set_max_value($value);
}
function set_y_max_value($value) {
$this->_yAxis->set_max_value($value);
}
function get_x_min_value() {
return $this->_xAxis->get_min_value();
}
function get_x_max_value() {
return $this->_xAxis->get_max_value();
}
function get_y_min_value() {
return $this->_yAxis->get_min_value();
}
function get_y_max_value() {
return $this->_yAxis->get_max_value();
}
function set_x_title_margin($value) {
$this->_xAxis->set_margin($value);
}
function set_y_title_margin($value) {
$this->_yAxis->set_margin($value);
}
function set_x_label_up($bool) {
$this->_xAxis->set_label_up($bool);
}
function set_y_label_up($bool) {
$this->_yAxis->set_label_up($bool);
}
function set_x_label_font($font) {
$this->_xAxis->set_label_font($font);
}
function set_y_label_font($font) {
$this->_yAxis->set_label_font($font);
}
function set_x_num_ticks($num) {
$this->_xAxis->set_num_ticks($num);
}
function set_y_num_ticks($num) {
$this->_yAxis->set_num_ticks($num);
}
function get_x_num_ticks() {
return $this->_xAxis->get_num_ticks();
}
function get_y_num_ticks() {
return $this->_yAxis->get_num_ticks();
}
function set_x_longest($lab) {
$this->_xAxis->set_longest($lab);
}
function set_y_longest($lab) {
$this->_yAxis->set_longest($lab);
}
function set_x_show_grid($bool) {
$this->_xAxis->set_show_grid($bool);
}
function set_y_show_grid($bool) {
$this->_yAxis->set_show_grid($bool);
}
function set_x_labels($labels) {
$this->_xAxis->set_labels($labels);
}
function set_y_labels($labels) {
$this->_yAxis->set_labels($labels);
}
function draw() {
$this->compute();
if ($this->_bgcolor)
$this->fill_graph();
$this->draw_x_axis();
$this->draw_y_axis();
}
function fill_graph() {
$color = $this->_image->_rgb->allocate_color($this->_bgcolor);
ImageFilledRectangle (
$this->_image->_image,
$this->_graphLeft, $this->_graphTop,
$this->_graphLeft+$this->_graphWidth-1, $this->_graphTop+$this->_graphHeight-1,
$color
);
}
function draw_x_axis() {
if (!$this->_xAxis->_show) return;
#
# Draw the axis title
#
$this->_xAxis->_title->set_pos(
$this->_xAxis->_xTitle, $this->_xAxis->_yTitle );
$this->_xAxis->_title->set_color($this->_xAxis->_color);
$this->_xAxis->_title->draw($this->_image);
#
# Draw the Axis
#
$color = $this->_image->_rgb->allocate_color($this->_xAxis->_color);
ImageLine(
$this->_image->_image,
$this->_xAxis->_xMin, $this->_xAxis->_yMin,
$this->_xAxis->_xMax, $this->_xAxis->_yMax,
$color
);
#
# Draw the Tick Marks
#
if (count($this->_xAxis->_labels)) {
$ts = count($this->_xAxis->_labels);
} elseif ($this->_xAxis->_tickSteps) {
$ts = $this->_xAxis->_tickSteps;
} else {
$ts = 10;
}
$range = $this->_xAxis->_maxValue - $this->_xAxis->_minValue;
if (!class_exists("bargraph")) {
$div = ( ($ts-1)==0 ) ? 1 : $ts-1;
$valStep = $range / ($div);
$step = ($this->_graphWidth) / ($div);
} else {
$div = ( ($ts-1)==0 ) ? 1 : $ts-1;
$valStep = $range / ($div);
$step = ($this->_graphWidth) / ($ts);
}
for ($i=0; $i<$ts; $i++) {
$tick = round($i*$step);
$value = round($i*$valStep)+$this->_xAxis->_minValue;
$tl = $this->_graphLeft+$tick;
if ($this->_yAxis->_pos != "right") $tl -= 1;
if ($this->_xAxis->_showGrid) {
$ymin = $this->_yAxis->_yMin;
$ymax = $this->_yAxis->_yMax;
} else {
if ($this->_xAxis->_pos == "top")
$ymin = $this->_yAxis->_yMin;
else
$ymin = $this->_yAxis->_yMax - 2*$this->_tickSize;
$ymax = $ymin + 2*$this->_tickSize;
}
# Tick
ImageLine(
$this->_image->_image,
$tl, $ymin,
$tl, $ymax,
$color
);
# Label
if (count($this->_xAxis->_labels))
$val = new Text($this->_xAxis->_labels[$i],$this->_xAxis->_labelFont);
else
$val = new Text("$value",$this->_xAxis->_labelFont);
if ($this->_xAxis->_pos == "top")
if ($this->_xAxis->_labelUp)
$ym = $this->_yAxis->_yMin;
else
$ym = $this->_yAxis->_yMin - $this->_xLabelHeight;
else
$ym = $this->_yAxis->_yMax;
if (class_exists("bargraph")) {
$ll = round($this->_graphLeft+$tick+.5*$step-.5*$this->_xLabelWidth);
} else {
$ll = $this->_graphLeft+$tick-0.5*$this->_xLabelWidth;
}
if ($this->_xAxis->_pos != "top" && $this->_xAxis->_labelUp)
$ym += $this->_xLabelHeight;
$val->set_pos( $ll, $ym );
$val->set_color($this->_xAxis->_color);
if ($this->_xAxis->_labelUp)
$val->draw($this->_image,true);
else
$val->draw($this->_image);
}
}
function draw_y_axis() {
if (!$this->_yAxis->_show) return;
#
# Draw the axis title
#
$this->_yAxis->_title->set_pos(
$this->_yAxis->_xTitle, $this->_yAxis->_yTitle );
$this->_yAxis->_title->set_color($this->_yAxis->_color);
$this->_yAxis->_title->draw($this->_image,true);
#
# Draw the Axis
#
$color = $this->_image->_rgb->allocate_color($this->_yAxis->_color);
ImageLine(
$this->_image->_image,
$this->_yAxis->_xMin, $this->_yAxis->_yMin,
$this->_yAxis->_xMax, $this->_yAxis->_yMax,
$color
);
#
# Draw the Tick Marks
#
$ts = ($this->_yAxis->_tickSteps) ? $this->_yAxis->_tickSteps : 10;
$step = ($this->_graphHeight) / ($ts);
$range = $this->_yAxis->_maxValue - $this->_yAxis->_minValue;
$valStep = $range / ($ts);
for ($i=0; $i<=$ts; $i++) {
$tick = round($i*$step);
$value = round($i*$valStep)+$this->_yAxis->_minValue;
$tt = $this->_graphTop+$this->_graphHeight-$tick;
if ($this->_xAxis->_pos == "top") $tt -= 1;
if ($this->_yAxis->_showGrid) {
$ymin = $this->_xAxis->_xMin;
$ymax = $this->_xAxis->_xMax;
} else {
if ($this->_yAxis->_pos == "right")
$ymin = $this->_xAxis->_xMax - 2*$this->_tickSize;
else
$ymin = $this->_xAxis->_xMin;
$ymax = $ymin + 2*$this->_tickSize;
}
# Tick
ImageLine(
$this->_image->_image,
$ymin, $tt,
$ymax, $tt,
$color
);
# Label
$val = new Text("$value",$this->_yAxis->_labelFont);
if ($this->_yAxis->_pos == "right")
$ym = $this->_xAxis->_xMax + $this->_tickSize;
else
$ym = $this->_xAxis->_xMin - $this->_yLabelWidth;
if ($this->_yAxis->_labelUp)
$ll = $this->_graphTop+$this->_graphHeight-$tick+0.5*$this->_yLabelHeight;
else
$ll = $this->_graphTop+$this->_graphHeight-$tick-0.5*$this->_yLabelHeight;
$val->set_pos( $ym, $ll );
$val->set_color($this->_yAxis->_color);
if ($this->_yAxis->_labelUp)
$val->draw($this->_image,true);
else
$val->draw($this->_image);
}
}
function compute() {
list(
$this->_plotLeft, $this->_plotTop,
$this->_plotWidth, $this->_plotHeight
) = $this->_image->get_plot_area();
$dummyX = new Text($this->_xAxis->_longest,$this->_xAxis->_labelFont);
$dummyY = new Text($this->_yAxis->_longest,$this->_yAxis->_labelFont);
if ($this->_xAxis->_labelUp) {
$this->_xLabelWidth = $dummyX->get_height();
$this->_xLabelHeight = $dummyX->get_width();
} else {
$this->_xLabelHeight = $dummyX->get_height();
$this->_xLabelWidth = $dummyX->get_width();
}
if ($this->_yAxis->_labelUp) {
$this->_yLabelWidth = $dummyY->get_height();
$this->_yLabelHeight = $dummyY->get_width();
} else {
$this->_yLabelHeight = $dummyY->get_height();
$this->_yLabelWidth = $dummyY->get_width();
}
$this->compute_height();
$this->compute_width();
$this->_image->set_graph_area(
$this->_graphLeft, $this->_graphTop,
$this->_graphWidth, $this->_graphHeight
);
$this->compute_x_axis();
$this->compute_y_axis();
}
function compute_height() {
if (!$this->_xAxis->_show) {
$this->_graphTop = $this->_plotTop;
$this->_graphHeight = $this->_plotHeight;
return;
}
if ($this->_xAxis->_title->_text)
$th = $this->_xAxis->_title->get_height();
else $th = 0;
if ($this->_xAxis->_pos == "top") {
# Set the graph area
$this->_graphTop = $this->_plotTop + $th + $this->_xLabelHeight + 1;
} else {
# Set the graph area
$this->_graphTop = $this->_plotTop;
}
$this->_graphHeight = $this->_plotHeight - $th - $this->_xLabelHeight - 1;
}
function compute_width() {
if (!$this->_yAxis->_show) {
$this->_graphLeft = $this->_plotLeft;
$this->_graphWidth = $this->_plotWidth;
return;
}
if ($this->_yAxis->_title->_text)
$th = $this->_yAxis->_title->get_height();
else $th = 0;
if ($this->_yAxis->_pos == "right") {
# Set the graph area
$this->_graphLeft = $this->_plotLeft;
} else {
# Set the graph area
$this->_graphLeft = $this->_plotLeft + $th + $this->_yLabelWidth + 1;
}
$this->_graphWidth = $this->_plotWidth - $th - $this->_yLabelWidth - 1;
}
function compute_x_axis() {
if ($this->_xAxis->_title->_text)
$th = $this->_xAxis->_title->get_height();
else $th = 0;
$tw = $this->_xAxis->_title->get_width();
$this->_xAxis->_xMin = $this->_graphLeft;
$this->_xAxis->_xMax = $this->_graphLeft + $this->_graphWidth - 1;
if ($this->_xAxis->_pos == "top") {
# Compute the axis position
$this->_xAxis->_yMin = $this->_graphTop - 1;
$this->_xAxis->_yMax = $this->_graphTop - 1;
# Compute the axis title position
$this->_xAxis->_xTitle = round(
$this->_graphLeft + ($this->_graphWidth - $tw)/2
);
$this->_xAxis->_yTitle = $this->_graphTop - $th - $this->_xLabelHeight;
} else {
# Compute the axis position
$this->_xAxis->_yMin = $this->_graphTop + $this->_graphHeight;
$this->_xAxis->_yMax = $this->_graphTop + $this->_graphHeight;
# Compute the axis title position
$this->_xAxis->_xTitle = round(
$this->_graphLeft + ($this->_graphWidth - $tw)/2
);
$this->_xAxis->_yTitle = $this->_graphTop + $this->_graphHeight + $this->_xLabelHeight + $this->_tickSize - 1;
}
if ($this->_yAxis->_pos == "right") {
$this->_xAxis->_xMax += $this->_tickSize + 1;
} else {
$this->_xAxis->_xMin -= $this->_tickSize + 1;
}
}
function compute_y_axis() {
if ($this->_yAxis->_title->_text)
$th = $this->_yAxis->_title->get_height();
else $th = 0;
$tw = $this->_yAxis->_title->get_width();
$this->_yAxis->_yMin = $this->_graphTop;
$this->_yAxis->_yMax = $this->_graphTop + $this->_graphHeight - 1;
if ($this->_yAxis->_pos == "right") {
# Compute the axis position
$this->_yAxis->_xMin = $this->_graphLeft + $this->_graphWidth;
$this->_yAxis->_xMax = $this->_graphLeft + $this->_graphWidth;
# Compute the axis title position
$this->_yAxis->_xTitle = $this->_graphLeft + $this->_graphWidth + $this->_yLabelWidth + $this->_tickSize;
$this->_yAxis->_yTitle = round(
$this->_graphTop + ($this->_graphHeight + $tw)/2
);
} else {
# Compute the axis position
$this->_yAxis->_xMin = $this->_graphLeft -1;
$this->_yAxis->_xMax = $this->_graphLeft -1;
# Compute the axis title position
$this->_yAxis->_xTitle = $this->_graphLeft - $th - $this->_yLabelWidth - 2;
$this->_yAxis->_yTitle = round(
$this->_graphTop + ($this->_graphHeight + $tw)/2
);
}
if ($this->_xAxis->_title->_text)
$th = $this->_xAxis->_title->get_height();
else $th = 0;
if ($this->_xAxis->_pos == "top") {
$this->_yAxis->_yMin -= $this->_tickSize + 1;
} else {
$this->_yAxis->_yMax += $this->_tickSize + 1;
}
}
}
?>