<?php
# -----------------------------------------------------------------------------
#
# The legend class for all the graphs
#
# -----------------------------------------------------------------------------
#
# 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 $
#
# -----------------------------------------------------------------------------
class Legend {
var $_show;
var $_pos; // left or right
var $_title; // The title of the legend
var $_font;
var $_fontColor;
var $_top;
var $_left;
var $_width;
var $_height;
var $_textHeight;
var $_textWidth;
var $_align;
var $_margin; // Margin
var $_padding; // The padding inside of the lagend area
var $_bgColor; // The background color of the legend
var $_showBorder;
var $_boderColor;
var $_showShadow;
var $_shadowColor;
var $_shadowWidth;
# The size of the contentting area
var $_contentTop;
var $_contentLeft;
var $_contentWidth;
var $_contentHeight;
var $_items;
function Legend(&$image) {
$this->_image = &$image;
$this->_title = new Text("Legend", 2);
$this->_show = false;
$this->_bgColor = "#dddddd";
$this->_pos = "right";
$this->_showBorder = true;
$this->_borderColor = "black";
$this->_showShadow = true;
$this->_shadowColor = "#aaaaaa";
$this->_shadowWidth = 2;
$this->_margin = 3;
$this->_padding = 3;
$this->_items = array();
$this->_font = 1;
$this->_fontColor = "black";
$this->_align = "middle";
}
function set_show($bool) {
$this->_show = $bool;
}
function set_position($pos) {
$this->_pos = $pos;
}
function set_bgcolor($color) {
$this->_bgColor = $color;
}
function set_show_border($bool) {
$this->_showBorder = $bool;
}
function set_border_color($color) {
$this->_borderColor = $color;
}
function set_shadow($bool) {
$this->_showShadow = $bool;
}
function set_shadow_color($color) {
$this->_shadowColor = $color;
}
function set_shadow_width($width) {
$this->_shadowWidth = $width;
}
function set_margin($margin) {
$this->_margin = $margin;
}
function set_padding($padding) {
$this->_padding = $padding;
}
function set_font($font) {
$this->_font = $font;
}
function set_alignment($align) {
$this->_align = $align;
}
function add($label,$color) {
$this->_items{$label} = $color;
}
function draw() {
# Get the content area of the image
list(
$this->_contentLeft, $this->_contentTop,
$this->_contentWidth, $this->_contentHeight
) = $this->_image->get_content_area();
$this->compute();
$black = $this->_image->_rgb->allocate_color("black");
#
# Legend Background
#
if ($this->_bgColor) {
$bgc = $this->_image->_rgb->allocate_color($this->_bgColor);
ImageFilledRectangle (
$this->_image->_image,
$this->_left, $this->_top,
$this->_left+$this->_width-1, $this->_top+$this->_height-1,
$bgc
);
}
#
# Legend Border
#
if ($this->_showBorder) {
$bgc = $this->_image->_rgb->allocate_color($this->_borderColor);
ImageRectangle (
$this->_image->_image,
$this->_left, $this->_top,
$this->_left+$this->_width-1, $this->_top+$this->_height-1,
$bgc
);
}
#
# Legend Shadow
#
if ($this->_showShadow) {
$sh = $this->_image->_rgb->allocate_color($this->_shadowColor);
# Drop Shadow
ImageFilledRectangle (
$this->_image->_image,
$this->_left+$this->_width, $this->_top+$this->_shadowWidth,
$this->_left+$this->_width+$this->_shadowWidth-1, $this->_top+$this->_height+$this->_shadowWidth-1,
$sh
);
ImageFilledRectangle (
$this->_image->_image,
$this->_left+$this->_shadowWidth, $this->_top+$this->_height,
$this->_left+$this->_width+$this->_shadowWidth-1, $this->_top+$this->_height+$this->_shadowWidth-1,
$sh
);
}
#
# Draw the legend items
#
$top = $this->_top + $this->_padding + 1;
foreach ($this->_items as $label => $color) {
$text = new Text($label,$this->_font);
$c = $this->_image->_rgb->allocate_color($color);
ImageFilledRectangle (
$this->_image->_image,
$this->_left+2+$this->_padding, $top+1,
$this->_left+12+$this->_padding, $top+$this->_textHeight,
$c
);
ImageRectangle (
$this->_image->_image,
$this->_left+1+$this->_padding, $top,
$this->_left+13+$this->_padding, $top+$this->_textHeight+1,
$black
);
$text->set_pos($this->_left+13+2*$this->_padding,$top);
$text->set_color("black");
$text->draw($this->_image);
$top += $this->_textHeight+$this->_padding+2;
}
}
function compute() {
# If we don;t show the legend, the content area is the plot area
if (! $this->_show) {
$this->_image->set_plot_area(
$this->_contentLeft, $this->_contentTop,
$this->_contentWidth, $this->_contentHeight
);
return;
}
# Compute the width and the height of the legend
$str = "";
$num = count($this->_items);
foreach ($this->_items as $label => $color)
if (strlen($str) < strlen($label)) $str = $label;
$dummy = new Text("$str",$this->_font);
# Compute the text height and the width of the longest entry
$this->_textHeight = $dummy->get_height();
$this->_textWidth = $dummy->get_width();
# The legend with:
# text + 2*border + box+2*border + 3*padding
$this->_width = $this->_textWidth + 11 + 2 + 3*$this->_padding;
$this->_height = $num*($this->_textHeight + 2 + $this->_padding)+2*$this->_padding;
# Compute the legend position and size
if ($this->_pos == "left") {
$this->_left = $this->_contentLeft + $this->_margin;
$pl = $this->_contentLeft + $this->_width + 2*$this->_margin + 3;
}
else {
$this->_left = $this->_contentLeft + $this->_contentWidth-1 - $this->_width - $this->_shadowWidth - $this->_margin;
$pl = $this->_contentLeft;
}
# Alignment
if ($this->_align == "bottom")
$this->_top = $this->_contentTop+$this->_contentHeight-$this->_height;
elseif ($this->_align == "middle")
$this->_top = $this->_contentTop+round(($this->_contentHeight-$this->_height)/2);
else
$this->_top = $this->_contentTop;
$pw = $this->_contentWidth - $this->_width-3 - 2*$this->_margin;
$this->_image->set_plot_area(
$pl, $this->_contentTop, $pw, $this->_contentHeight
);
}
}