HorizontalBarGraph::BarLabel PHP Method

BarLabel() protected method

Text labels in or above the bar
protected BarLabel ( $value, &$bar, $offset_x = null )
    protected function BarLabel($value, &$bar, $offset_x = null)
    {
        $font_size = $this->bar_label_font_size;
        list($text_size) = $this->TextSize(strlen($value), $this->bar_label_font_size, $this->bar_label_font_adjust);
        $space = $this->bar_label_space;
        $y = $bar['y'] + ($bar['height'] + $font_size) / 2 - $font_size / 8;
        $colour = $this->bar_label_colour;
        $acolour = $this->bar_label_colour_above;
        $anchor = 'end';
        if (!is_null($offset_x)) {
            $x = $bar['x'] + $bar['width'] - $offset_x;
            $anchor = 'start';
        } else {
            // find positions - if $top > $bottom, text will not fit
            $pos = $this->bar_label_position;
            if (empty($pos)) {
                $pos = 'top';
            }
            $top = $bar['x'] + $bar['width'] - $space;
            $bottom = $bar['x'] + $space;
            if ($top - $text_size < $bottom) {
                $pos = 'above';
            }
            $swap = $bar['x'] + $bar['width'] <= $this->pad_left + $this->x0;
            switch ($pos) {
                case 'above':
                    $x = $swap ? $bottom - $space * 2 : $top + $space * 2;
                    $anchor = $swap ? 'end' : 'start';
                    if (!empty($acolour)) {
                        $colour = $acolour;
                    }
                    break;
                case 'bottom':
                    $x = $swap ? $top : $bottom;
                    $anchor = $swap ? 'end' : 'start';
                    break;
                case 'centre':
                    $x = $bar['x'] + $bar['width'] / 2;
                    $anchor = 'middle';
                    break;
                case 'top':
                default:
                    $x = $swap ? $bottom : $top;
                    $anchor = $swap ? 'start' : 'end';
                    break;
            }
        }
        $text = array('x' => $x, 'y' => $y, 'text-anchor' => $anchor, 'font-family' => $this->bar_label_font, 'font-size' => $font_size, 'fill' => $colour);
        if ($this->bar_label_font_weight != 'normal') {
            $text['font-weight'] = $this->bar_label_font_weight;
        }
        return $this->Element('text', $text, NULL, $value);
    }

Usage Example

 /**
  * Overridden to prevent drawing behind higher bars
  * $offset_y should be true for inner bars
  */
 protected function BarLabel($value, &$bar, $offset_x = null)
 {
     list($text_size) = $this->TextSize(strlen($value), $this->bar_label_font_size, $this->bar_label_font_adjust);
     $space = $this->bar_label_space;
     if ($offset_x) {
         // bar too small, would be above
         if ($bar['width'] < $text_size + 2 * $space) {
             return parent::BarLabel($value, $bar, ($bar['width'] + $text_size) / 2);
         }
         // option set to above
         if ($this->bar_label_position == 'above') {
             $this->bar_label_position = 'top';
             $label = parent::BarLabel($value, $bar);
             $this->bar_label_position = 'above';
             return $label;
         }
     }
     return parent::BarLabel($value, $bar);
 }
All Usage Examples Of HorizontalBarGraph::BarLabel