Graph::RelativePosition PHP Method

RelativePosition() public static method

Returns the [x,y] position that is $pos relative to the top, left, bottom and right. When $text is true, returns [x,y,align right]
public static RelativePosition ( $pos, $top, $left, $bottom, $right, $width, $height, $pad, $text = false )
    public static function RelativePosition($pos, $top, $left, $bottom, $right, $width, $height, $pad, $text = false)
    {
        $offset_x = $offset_y = 0;
        $inside = $atop = $aleft = true;
        $parts = preg_split('/\\s+/', $pos);
        while (count($parts)) {
            $part = array_shift($parts);
            switch ($part) {
                case 'outside':
                    $inside = false;
                    break;
                case 'inside':
                    $inside = true;
                    break;
                case 'top':
                    $atop = true;
                    break;
                case 'bottom':
                    $atop = false;
                    break;
                case 'left':
                    $aleft = true;
                    break;
                case 'right':
                    $aleft = false;
                    break;
                default:
                    if (is_numeric($part)) {
                        $offset_x = $part;
                        if (count($parts) && is_numeric($parts[0])) {
                            $offset_y = array_shift($parts);
                        }
                    }
            }
        }
        $edge = $atop ? $top : $bottom;
        $fit = $inside && $bottom - $top >= $pad + $height;
        // padding +ve if both fitting in at top, or outside at bottom
        $distance = $atop == $fit ? $pad : -($pad + $height);
        $y = $edge + $distance;
        $edge = $aleft ? $left : $right;
        $fit = $inside && $right - $left >= $pad + $width;
        $distance = $aleft == $fit ? $pad : ($text ? -$pad : -($pad + $width));
        $x = $edge + $distance;
        $y += $offset_y;
        $x += $offset_x;
        // third return value is whether text should be right-aligned
        $text_right = $text && $aleft != $fit;
        return array($x, $y, $text_right);
    }

Usage Example

Example #1
0
 /**
  * Parses a position string, returning x and y coordinates
  */
 protected function ParsePosition($pos, $w = 0, $h = 0, $pad = 0)
 {
     $inner = true;
     $parts = preg_split('/\\s+/', $pos);
     if (count($parts)) {
         // if 'outer' is found after 'inner', it takes precedence
         $parts = array_reverse($parts);
         $inner_at = array_search('inner', $parts);
         $outer_at = array_search('outer', $parts);
         if ($outer_at !== false && ($inner_at === false || $inner_at < $outer_at)) {
             $inner = false;
         }
     }
     if ($inner) {
         $t = $this->pad_top;
         $l = $this->pad_left;
         $b = $this->height - $this->pad_bottom;
         $r = $this->width - $this->pad_right;
         // make sure it fits to keep RelativePosition happy
         if ($w > $r - $l) {
             $w = $r - $l;
         }
         if ($h > $b - $t) {
             $h = $b - $t;
         }
     } else {
         $t = $l = 0;
         $b = $this->height;
         $r = $this->width;
     }
     // ParsePosition is always inside canvas or graph
     $pos .= ' inside';
     return Graph::RelativePosition($pos, $t, $l, $b, $r, $w, $h, $pad);
 }
All Usage Examples Of Graph::RelativePosition