BarGraph::BarLabel PHP Method

BarLabel() protected method

Text labels in or above the bar
protected BarLabel ( $value, &$bar, $offset_y = null )
    protected function BarLabel($value, &$bar, $offset_y = null)
    {
        $font_size = $this->bar_label_font_size;
        $space = $this->bar_label_space;
        $x = $bar['x'] + $bar['width'] / 2;
        $colour = $this->bar_label_colour;
        $acolour = $this->bar_label_colour_above;
        if (!is_null($offset_y)) {
            $y = $bar['y'] + $offset_y;
        } else {
            // find positions
            $pos = $this->bar_label_position;
            if (empty($pos)) {
                $pos = 'top';
            }
            $top = $bar['y'] + $font_size + $space;
            $bottom = $bar['y'] + $bar['height'] - $space;
            if ($top > $bottom) {
                $pos = 'above';
            }
            $swap = $bar['y'] >= $this->height - $this->pad_bottom - $this->y0;
            switch ($pos) {
                case 'above':
                    $y = $swap ? $bar['y'] + $bar['height'] + $font_size + $space : $bar['y'] - $space;
                    if (!empty($acolour)) {
                        $colour = $acolour;
                    }
                    break;
                case 'bottom':
                    $y = $swap ? $top : $bottom;
                    break;
                case 'centre':
                    $y = $bar['y'] + ($bar['height'] + $font_size) / 2;
                    break;
                case 'top':
                default:
                    $y = $swap ? $bottom : $top;
                    break;
            }
        }
        $text = array('x' => $x, 'y' => $y, 'text-anchor' => 'middle', '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(&$item, &$bar, $offset_y = null)
 {
     $font_size = $this->bar_label_font_size;
     $space = $this->bar_label_space;
     if ($offset_y) {
         // bar too small, would be above
         if ($bar['height'] < $font_size + 2 * $space) {
             return parent::BarLabel($item, $bar, ($bar['height'] + $font_size) / 2);
         }
         // option set to above
         if ($this->bar_label_position == 'above') {
             $this->bar_label_position = 'top';
             $label = parent::BarLabel($item, $bar);
             $this->bar_label_position = 'above';
             return $label;
         }
     }
     return parent::BarLabel($item, $bar);
 }