Пример #1
0
 public function draw(awDriver $driver)
 {
     if ($this->hide) {
         return;
     }
     $count = $this->count();
     // No legend to print
     if ($count === 0) {
         return;
     }
     // Get text widths and heights of each element of the legend
     $widths = array();
     $heights = array();
     $texts = array();
     for ($i = 0; $i < $count; $i++) {
         list(, $title, ) = $this->legends[$i];
         $text = new awText($title, $this->textFont, $this->textColor, 0);
         //			$font = $text->getFont();
         $widths[$i] = $driver->getTextWidth($text) + $this->textMargin->left + $this->textMargin->right;
         $heights[$i] = $driver->getTextHeight($text);
         $texts[$i] = $text;
     }
     // Maximum height of the font used
     $heightMax = array_max($heights);
     // Get number of columns
     if ($this->columns !== NULL) {
         $columns = $this->columns;
     } else {
         if ($this->rows !== NULL) {
             $columns = ceil($count / $this->rows);
         } else {
             $columns = $count;
         }
     }
     // Number of  rows
     $rows = (int) ceil($count / $columns);
     // Get maximum with of each column
     $widthMax = array();
     for ($i = 0; $i < $count; $i++) {
         // Get column width
         $column = $i % $columns;
         if (array_key_exists($column, $widthMax) === FALSE) {
             $widthMax[$column] = $widths[$i];
         } else {
             $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 = new awPoint($this->position->x * $x, $this->position->y * $y);
     switch ($this->hAlign) {
         case awLegend::CENTER:
             $p->x -= $width / 2;
             break;
         case awLegend::RIGHT:
             $p->x -= $width;
             break;
     }
     switch ($this->vAlign) {
         case awLegend::MIDDLE:
             $p->y -= $height / 2;
             break;
         case awLegend::BOTTOM:
             $p->y -= $height;
             break;
     }
     // Draw legend shadow
     $this->shadow->draw($driver, $p, $p->move($width, $height), awShadow::OUT);
     // Draw legends base
     $this->drawBase($driver, $p, $width, $height);
     // Draw each legend
     for ($i = 0; $i < $count; $i++) {
         list($component, $title, $type) = $this->legends[$i];
         $column = $i % $columns;
         $row = (int) floor($i / $columns);
         // Get width of all previous columns
         $previousColumns = 0;
         for ($j = 0; $j < $column; $j++) {
             $previousColumns += $this->space + 10 + 5 + $widthMax[$j];
         }
         // Draw legend text
         $driver->string($texts[$i], $p->move($this->padding[0] + $previousColumns + 10 + 5 + $this->textMargin->left, $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $heights[$i] / 2));
         // Draw legend icon
         switch ($type) {
             case awLegend::LINE:
             case awLegend::MARK:
             case awLegend::MARKONLY:
                 // Get vertical position
                 $x = $this->padding[0] + $previousColumns;
                 $y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - $component->getLegendLineThickness();
                 // Draw two lines
                 if ($component->getLegendLineColor() !== NULL) {
                     $color = $component->getLegendLineColor();
                     if ($color instanceof awColor and $type !== awLegend::MARKONLY) {
                         $driver->line($color, new awLine($p->move($x, $y + $component->getLegendLineThickness() / 2), $p->move($x + 10, $y + $component->getLegendLineThickness() / 2), $component->getLegendLineStyle(), $component->getLegendLineThickness()));
                         unset($color);
                     }
                 }
                 if ($type === awLegend::MARK or $type === awLegend::MARKONLY) {
                     $mark = $component->getLegendMark();
                     if ($mark !== NULL) {
                         $mark->draw($driver, $p->move($x + 5.5, $y + $component->getLegendLineThickness() / 2));
                     }
                     unset($mark);
                 }
                 break;
             case awLegend::BACKGROUND:
                 // Get vertical position
                 $x = $this->padding[0] + $previousColumns;
                 $y = $this->padding[2] + $row * ($heightMax + $this->space) + $heightMax / 2 - 5;
                 $from = $p->move($x, $y);
                 $to = $p->move($x + 10, $y + 10);
                 $background = $component->getLegendBackground();
                 if ($background !== NULL) {
                     $driver->filledRectangle($component->getLegendBackground(), new awLine($from, $to));
                     // Draw rectangle border
                     $this->border->rectangle($driver, $from->move(0, 0), $to->move(0, 0));
                 }
                 unset($background, $from, $to);
                 break;
         }
     }
 }
Пример #2
0
 /**
  * Draw the label
  *
  * @param awDriver $driver
  * @param awPoint $p Label center
  * @param int $key Text position in the array of texts (default to zero)
  */
 public function draw(awDriver $driver, awPoint $p, $key = 0)
 {
     if ($key % $this->interval !== 0) {
         return;
     }
     // Hide all labels
     if ($this->hide) {
         return;
     }
     // Key is hidden
     if (array_key_exists($key, $this->hideKey)) {
         return;
     }
     // Hide first label
     if ($key === 0 and $this->hideFirst) {
         return;
     }
     // Hide last label
     if ($key === count($this->texts) - 1 and $this->hideLast) {
         return;
     }
     $text = $this->getText($key);
     if ($text !== NULL) {
         // Value must be hidden
         if (in_array($text->getText(), $this->hideValue)) {
             return;
         }
         $x = $p->x;
         $y = $p->y;
         // Get padding
         list($left, $right, $top, $bottom) = $text->getPadding();
         //			$font = $text->getFont();
         $width = $driver->getTextWidth($text);
         $height = $driver->getTextHeight($text);
         switch ($this->hAlign) {
             case awLabel::RIGHT:
                 $x -= $width + $right;
                 break;
             case awLabel::CENTER:
                 $x -= ($width - $left + $right) / 2;
                 break;
             case awLabel::LEFT:
                 $x += $left;
                 break;
         }
         switch ($this->vAlign) {
             case awLabel::TOP:
                 $y -= $height + $bottom;
                 break;
             case awLabel::MIDDLE:
                 $y -= ($height - $top + $bottom) / 2;
                 break;
             case awLabel::BOTTOM:
                 $y += $top;
                 break;
         }
         $driver->string($text, $this->move->move($x, $y));
     }
 }