Source for file Legend.class.php
Documentation is available at Legend.class.php
* This work is hereby released into the Public Domain.
* To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
require_once dirname(__FILE__ ). "/../Graph.class.php";
define("LEGEND_BACKGROUND", 2);
define("LEGEND_MODEL_RIGHT", 1);
define("LEGEND_MODEL_BOTTOM", 2);
* @package linea21.externals
class awLegend implements awPositionable {
* Background color or gradient
* Space between each legend
* @var array Array for left, right, top and bottom margins
* Color/Gradient background legend
* Use marks and line as legend
* @param int $model Legend model
public function __construct($model = awLegend::MODEL_RIGHT) {
* Set a predefined model for the legend
* @param bool $hide TRUE to hide legend, FALSE otherwise
public function hide($hide = TRUE) {
$this->hide = (bool) $hide;
public function show($show = TRUE) {
$this->hide = (bool) !$show;
* Add a Legendable object to the legend
* @param awLegendable $legendable
* @param string $title Legend title
* @param int $type Legend type (default to awLegend::LINE)
public function add(awLegendable $legendable, $title, $type = awLegend::LINE) {
$legend = array($legendable, $title, $type);
public function setPadding($left, $right, $top, $bottom) {
$this->padding = array((int) $left, (int) $right, (int) $top, (int) $bottom);
* Change space between each legend
$this->space = (int) $space;
* @param int $h Horizontal alignment
* @param int $v Vertical alignment
public function setAlign($h = NULL, $v = NULL) {
* Change number of columns
$this->rows = (int) $rows;
* X and Y positions must be between 0 and 1.
* @param mixed $background
* Change background color
* Change background gradient
* @param awGradient $gradient
* Count the number of Legendable objects in the legend
public function count() {
public function draw(awDriver $driver) {
// Get text widths and heights of each element of the legend
for($i = 0; $i < $count; $i++ ) {
list (, $title, ) = $this->legends[$i];
// $font = $text->getFont();
$heights[$i] = $driver->getTextHeight($text);
// Maximum height of the font used
} else if($this->rows !== NULL) {
$rows = (int) ceil($count / $columns);
// Get maximum with of each column
for($i = 0; $i < $count; $i++ ) {
$widthMax[$column] = $widths[$i];
$widthMax[$column] = max($widthMax[$column], $widths[$i]);
$width = $this->padding[0] + $this->padding[1] - $this->space;
for($i = 0; $i < $columns; $i++ ) {
$width += $this->space + 5 + 10 + $widthMax[$i];
$height = ($heightMax + $this->space) * $rows - $this->space + $this->padding[2] + $this->padding[3];
// Look for legends position
list ($x, $y) = $driver->getSize();
$p->move($width, $height),
$this->drawBase($driver, $p, $width, $height);
for($i = 0; $i < $count; $i++ ) {
list ($component, $title, $type) = $this->legends[$i];
$row = (int) floor($i / $columns);
// Get width of all previous columns
for($j = 0; $j < $column; $j++ ) {
$previousColumns += $this->space + 10 + 5 + $widthMax[$j];
$this->padding[0] + $previousColumns + 10 + 5 + $this->textMargin->left,
$this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $heights[$i] / 2
$x = $this->padding[0] + $previousColumns;
$y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $component->getLegendLineThickness();
if($component->getLegendLineColor() !== NULL) {
$color = $component->getLegendLineColor();
$y + $component->getLegendLineThickness() / 2
$y + $component->getLegendLineThickness() / 2
$component->getLegendLineStyle(),
$component->getLegendLineThickness()
$mark = $component->getLegendMark();
$y + $component->getLegendLineThickness() / 2
$x = $this->padding[0] + $previousColumns;
$y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - 5;
$background = $component->getLegendBackground();
if($background !== NULL) {
$driver->filledRectangle(
$component->getLegendBackground(),
unset ($background, $from, $to);
private function drawBase(awDriver $driver, awPoint $p, $width, $height) {
$p->move($width, $height)
$size = $this->border->visible() ? 1 : 0;
$driver->filledRectangle(
$p->move($width - $size, $height - $size)
* You can add a legend to components which implements this interface
* @package linea21.externals
* Get the background color or gradient of an element of the component
* @return Color, Gradient
|