summaryrefslogtreecommitdiffstats
path: root/src/interfaces/element.php
blob: 8aeb8bfe6813e6347d7681dca7fd1c678266b71c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
 * File containing the abstract ezcGraphChartElement class
 *
 * @package Graph
 * @version //autogentag//
 * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */
/**
 * 
 *
 * @package Graph
 */
abstract class ezcGraphChartElement extends ezcBaseOptions
{

    /**
     * Title of chart element
     * 
     * @var string
     */
    protected $title;

    /**
     * Background color of chart element 
     * 
     * @var ezcGraphColor
     */
    protected $background;

    /**
     * Boundings of this elements
     * 
     * @var ezcGraphBoundings
     */
    protected $boundings;

    /**
     * Border color of chart element 
     * 
     * @var ezcGraphColor
     */
    protected $border;

    /**
     * Distance between border and content of element
     * 
     * @var integer
     */
    protected $padding;

    /**
     * Distance between outer boundings and border of an element 
     * 
     * @var integer
     */
    protected $margin;

    /**
     * Border width 
     * 
     * @var integer
     */
    protected $borderWidth;

    /**
     * Integer defining the elements position in the chart 
     * 
     * @var integer
     */
    protected $position;

    /**
     * Maximum size of the title
     * 
     * @var integer
     */
    protected $maxTitleHeight = 16;

    /**
     * Percentage of boundings which are used for the title with position
     * left, right or center
     * 
     * @var float
     */
    protected $portraitTitleSize = .15;

    /**
     * Percentage of boundings which are used for the title with position
     * top otr bottom
     * 
     * @var float
     */
    protected $landscapeTitleSize = .2;

    /**
     * Font used for this element 
     * 
     * @var ezcGraphFontOptions
     */
    protected $font;

    /**
     * Indicates if font configuration was already cloned for this specific
     * element.
     * 
     * @var boolean
     */
    protected $fontCloned = false;

    public function __construct( array $options = array() )
    {
        $this->boundings = new ezcGraphBoundings();
        $this->position = ezcGraph::LEFT;
        $this->font = new ezcGraphFontOptions();

        parent::__construct( $options );
    }

    /**
     * Set colors and border fro this element
     * 
     * @param ezcGraphPalette $palette Palette
     * @return void
     */
    public function setFromPalette( ezcGraphPalette $palette )
    {
        $this->border = $palette->elementBorderColor;
        $this->borderWidth = $palette->elementBorderWidth;
        $this->background = $palette->elementBackground;
        $this->padding = $palette->padding;
        $this->margin = $palette->margin;
    }

    /**
     * __set 
     * 
     * @param mixed $propertyName 
     * @param mixed $propertyValue 
     * @throws ezcBaseValueException
     *          If a submitted parameter was out of range or type.
     * @throws ezcBasePropertyNotFoundException
     *          If a the value for the property options is not an instance of
     * @return void
     */
    public function __set( $propertyName, $propertyValue )
    {
        switch ( $propertyName )
        {
            case 'title':
                $this->title = (string) $propertyValue;
                break;
            case 'background':
                $this->background = ezcGraphColor::create( $propertyValue );
                break;
            case 'border':
                $this->border = ezcGraphColor::create( $propertyValue );
                break;
            case 'padding':
                $this->padding = max( 0, (int) $propertyValue );
                break;
            case 'margin':
                $this->margin = max( 0, (int) $propertyValue );
                break;
            case 'borderWidth':
                $this->borderWidth = max( 0, (int) $propertyValue);
                break;
            case 'font':
                if ( $propertyValue instanceof ezcGraphFontOptions )
                {
                    $this->font = $propertyValue;
                }
                elseif ( is_string( $propertyValue ) )
                {
                    if ( !$this->fontCloned )
                    {
                        $this->font = clone $this->font;
                        $this->fontCloned = true;
                    }

                    $this->font->font = $propertyValue;
                }
                else
                {
                    throw new ezcBaseValueException( $propertyValue, 'ezcGraphFontOptions' );
                }
                break;
            case 'position':
                $positions = array(
                    ezcGraph::TOP,
                    ezcGraph::BOTTOM,
                    ezcGraph::LEFT,
                    ezcGraph::RIGHT,
                );

                if ( in_array( $propertyValue, $positions, true ) )
                {
                    $this->position = $propertyValue;
                }
                else 
                {
                    throw new ezcBaseValueException( 'position', $propertyValue, 'integer' );
                }
                break;
            default:
                throw new ezcGraphNoSuchDatasetException( $propertyName );
                break;
        }
    }
    
    public function __get( $propertyName )
    {
        if ( $propertyName === 'font' )
        {
            // Clone font configuration when requested for this element
            if ( !$this->fontCloned )
            {
                $this->font = clone $this->font;
                $this->fontCloned = true;
            }
        }

        return parent::__get( $propertyName );
    }

    /**
     * Renders this chart element
     *
     * This method receives and returns a part of the canvas where it can be 
     * rendered on.
     * 
     * @param ezcGraphBoundings $boundings Part of canvase to render element on
     * @return ezcGraphBoundings Part of canvas, which is still free to draw on
     */
    abstract public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings );

    protected function renderBorder( ezcGraphRenderer $renderer )
    {
        // Apply margin
        $this->boundings->x0 += $this->margin;
        $this->boundings->y0 += $this->margin;
        $this->boundings->x1 -= $this->margin;
        $this->boundings->y1 -= $this->margin;

        if ( ( $this->border instanceof ezcGraphColor ) &&
             ( $this->borderWidth > 0 ) )
        {
            // Default bordervalue to 1
            $this->borderWidth = max( 1, $this->borderWidth );
         
            // Draw border
            $renderer->drawRect(
                $this->border,
                new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ),
                $this->boundings->x1 - $this->boundings->x0,
                $this->boundings->y1 - $this->boundings->y0,
                $this->borderWidth
            );

            // Reduce local boundings by borderWidth
            $this->boundings->x0 += $this->borderWidth;
            $this->boundings->y0 += $this->borderWidth;
            $this->boundings->x1 -= $this->borderWidth;
            $this->boundings->y1 -= $this->borderWidth;
        }
    }

    protected function renderBackground( ezcGraphRenderer $renderer )
    {
        if ( $this->background instanceof ezcGraphColor )
        {
            $renderer->drawBackground(
                $this->background,
                new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ),
                $this->boundings->x1 - $this->boundings->x0,
                $this->boundings->y1 - $this->boundings->y0
            );
        }

        // Apply padding
        $this->boundings->x0 += $this->padding;
        $this->boundings->y0 += $this->padding;
        $this->boundings->x1 -= $this->padding;
        $this->boundings->y1 -= $this->padding;
    }

    protected function renderTitle( ezcGraphRenderer $renderer )
    {
        if ( !empty( $this->title ) )
        {
            switch ( $this->position )
            {
                case ezcGraph::LEFT:
                case ezcGraph::RIGHT:
                case ezcGraph::CENTER:
                    $height = min(
                        $this->maxTitleHeight,
                        ( $this->boundings->y1 - $this->boundings->y0 ) * $this->portraitTitleSize
                    );
                    $renderer->drawTextBox(
                        new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ),
                        $this->title,
                        $this->boundings->x1 - $this->boundings->x0,
                        $height
                    );
                    $this->boundings->y0 += $height;
                    break;
                case ezcGraph::TOP:
                    $height = min(
                        $this->maxTitleHeight,
                        ( $this->boundings->y1 - $this->boundings->y0 ) * $this->landscapeTitleSize
                    );
                    $renderer->drawTextBox(
                        new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ),
                        $this->title,
                        $this->boundings->x1 - $this->boundings->x0,
                        $height
                    );
                    $this->boundings->y0 += $height;
                    break;
                case ezcGraph::BOTTOM:
                    $height = min(
                        $this->maxTitleHeight,
                        ( $this->boundings->y1 - $this->boundings->y0 ) * $this->landscapeTitleSize
                    );
                    $renderer->drawTextBox(
                        new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y1 - $height ),
                        $this->title,
                        $this->boundings->x1 - $this->boundings->x0,
                        $height
                    );
                    $this->boundings->y1 -= $height;
                    break;
            }
        }
    }
}

?>
OpenPOWER on IntegriCloud