summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Chart/Primitive/Rect.php
blob: 0145e0748acd8e520cdc99dabd786dce883b13a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Chart\Primitive;

use DomElement;
use Icinga\Chart\Render\RenderContext;
use Icinga\Chart\Format;

/**
 * Drawable representing the SVG rect element
 */
class Rect extends Animatable implements Drawable
{
    /**
     * The x position
     *
     * @var int
     */
    private $x;

    /**
     * The y position
     *
     * @var int
     */
    private $y;

    /**
     * The width of this rect
     *
     * @var int
     */
    private $width;

    /**
     * The height of this rect
     *
     * @var int
     */
    private $height;

    /**
     * Whether to keep the ratio
     *
     * @var bool
     */
    private $keepRatio = false;

    /**
     * Create this rect
     *
     * @param int $x        The x position of the rect
     * @param int $y        The y position of the rectangle
     * @param int $width    The width of the rectangle
     * @param int $height   The height of the rectangle
     */
    public function __construct($x, $y, $width, $height)
    {
        $this->x = $x;
        $this->y = $y;
        $this->width = $width;
        $this->height = $height;
    }

    /**
     * Call to let the rectangle keep the ratio
     */
    public function keepRatio()
    {
        $this->keepRatio = true;
    }

    /**
     * Create the SVG representation from this Drawable
     *
     * @param   RenderContext $ctx  The context to use for rendering
     *
     * @return  DOMElement          The SVG Element
     */
    public function toSvg(RenderContext $ctx)
    {
        $doc = $ctx->getDocument();
        $rect = $doc->createElement('rect');

        list($x, $y) = $ctx->toAbsolute($this->x, $this->y);
        if ($this->keepRatio) {
            $ctx->keepRatio();
        }
        list($width, $height) = $ctx->toAbsolute($this->width, $this->height);
        if ($this->keepRatio) {
            $ctx->ignoreRatio();
        }
        $rect->setAttribute('x', Format::formatSVGNumber($x));
        $rect->setAttribute('y', Format::formatSVGNumber($y));
        $rect->setAttribute('width', Format::formatSVGNumber($width));
        $rect->setAttribute('height', Format::formatSVGNumber($height));
        $rect->setAttribute('style', $this->getStyle());

        $this->applyAttributes($rect);
        $this->appendAnimation($rect, $ctx);

        return $rect;
    }
}