<?php
# -----------------------------------------------------------------------------
#
# Creates a simple vertical bar graph
#
# -----------------------------------------------------------------------------
#
# 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 $
#
# -----------------------------------------------------------------------------
/*
-------------------------------------------------------------------------------
TODO:
- In the gradient, the bar spacing is off by a little bit
-------------------------------------------------------------------------------
*/
include("Graph.php");
class BarGraph extends Graph {
var $_sum;
var $_barSpacing;
var $_barBorder;
var $_gradient;
var $_graphLeft;
var $_graphTop;
var $_graphWidth;
var $_graphHeight;
function BarGraph($width,$height,$title=null) {
# Instantiate the base class
$this->Graph($width,$height,$title);
# Set the base class properties
$this->legend_show(false);
$this->set_label_type("percent");
$this->set_bgcolor("#dddddd");
$this->set_margin(1,5,1,1);
$this->_barSpacing = 5;
$this->_barBorder = true;
$this->_borderColor = "black";
$this->_gradient = true;
$this->set_x_axis_title_margin(3);
$this->set_y_axis_title_margin(3);
}
function set_bar_spacing($val) {
$this->_barSpacing = $val;
}
function set_bar_gradient($bool) {
$this->_gradient = $bool;
}
function set_bar_border_color($col) {
$this->_borderColor = $col;
}
function show_bar_border($bool) {
$this->_barBorder = $bool;
}
#
# Generate the legend items
# There is no legend for a bar graph
#
function generate_legend() {
;
}
#
# Generate the axes
#
function generate_axes() {
list($min,$max) = $this->minmax();
list($lx,$ly) = $this->get_longest();
if ($this->get_y_axis_min_value() === "unset")
$this->set_y_axis_min_value($min);
if ($this->get_y_axis_max_value() === "unset")
$this->set_y_axis_max_value($max);
if (! $this->get_x_axis_num_ticks()) {
$l = array();
foreach ($this->_data as $label => $value) {
array_push($l,$label);
}
$this->set_x_axis_labels($l);
}
else {
# This is not good
$lx = $this->get_x_axis_min_value();
}
$this->set_x_axis_longest($lx);
$this->set_y_axis_longest($ly);
}
#
# Draw the Bar Graph
#
function create_graph() {
$min = $this->get_y_axis_min_value();
$max = $this->get_y_axis_max_value();
$this->_sum = $this->sum();
$this->compute();
$count = count($this->_data);
# Compute the bar width
$bwidth = ($this->_graphWidth - ($count)*$this->_barSpacing) / $count;
$left = ($this->_barSpacing/2+$this->_graphLeft);
foreach ($this->_data as $label => $value) {
$top = $this->_graphTop + $this->_graphHeight
- round((($value-$min)/($max-$min))*$this->_graphHeight);
if ($top > $this->_graphTop+$this->_graphHeight-1)
$top = $this->_graphTop+$this->_graphHeight;
# The bar spacing is off a little bit???
if ($this->_gradient) { // Draw the bars with gradient
$dg = 255/($bwidth/3);
for ($i=0; $i<($bwidth/3); $i++) {
$shade = round($dg*$i);
$this->draw_line(
$shade, $shade, 255,
$left+$i,$top, $this->_graphTop+$this->_graphHeight-1);
}
$dg = 255/(2*$bwidth/3);
for ($i=$bwidth/3; $i<($bwidth-1); $i++) {
$shade = 255-round($dg*($i-$bwidth/3));
$this->draw_line(
$shade, $shade, 255,
$left+$i,$top, $this->_graphTop+$this->_graphHeight-1);
}
} else { // Solid
$this->draw_box(0,0,255,$left,$left+$bwidth-1,$top,$this->_graphTop+$this->_graphHeight-1);
}
if ($this->_barBorder) {
$this->draw_border(0,0,255,$left,$left+$bwidth-1,$top,$this->_graphTop+$this->_graphHeight-1);
}
$left += $bwidth+$this->_barSpacing;
}
}
# Draws a filled box
function draw_box($r,$g,$b,$left,$right,$top,$bottom) {
# If the color already exists, don;t allocate it again
$color = imagecolorexact($this->_image->_image,
$r, $g, $b);
if ($color==-1)
$color = $this->_image->_rgb->allocate_color(
array( $r, $g, $b ) );
ImageFilledRectangle(
$this->_image->_image,
$left, $top,
$right, $bottom,
$color
);
}
# Draws a border box
function draw_border($r,$g,$b,$left,$right,$top,$bottom) {
# If the bar does not have any height, return
if ($top == $this->_graphTop+$this->_graphHeight) return;
# If the color already exists, don;t allocate it again
$color = $this->_image->_rgb->allocate_color(
$this->_borderColor );
ImageLine(
$this->_image->_image,
$left, $top,
$left, $bottom,
$color
);
ImageLine(
$this->_image->_image,
$right, $top,
$right, $bottom,
$color
);
ImageLine(
$this->_image->_image,
$left, $top,
$right, $top,
$color
);
}
# Draws a vertical line
function draw_line($r,$g,$b,$left,$top,$bottom) {
# If the color already exists, don;t allocate it again
$color = imagecolorexact($this->_image->_image,
$r, $g, $b);
if ($color==-1)
$color = $this->_image->_rgb->allocate_color(
array( $r, $g, $b ) );
ImageFilledRectangle(
$this->_image->_image,
$left, $top,
$left+1, $bottom,
$color
);
}
#
# Compute the center and size
#
function compute() {
list(
$this->_graphLeft, $this->_graphTop,
$this->_graphWidth, $this->_graphHeight
) = $this->_image->get_graph_area();
}
}
?>