summaryrefslogtreecommitdiffstats
path: root/src/interfaces/chart.php
blob: 18e18490f9b13c7ad8936980dc036a21797b21bd (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
 * File containing the abstract ezcGraphChart 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
 */
/**
 * Class to represent a complete charts.
 *
 * @package Graph
 */
abstract class ezcGraphChart implements ArrayAccess
{

    /**
     * Contains all general chart options
     * 
     * @var ezcGraphChartConfig
     */
    protected $options;

    /**
     * Contains subelelemnts of the chart like legend and axes
     * 
     * @var array( ezcGraphChartElement )
     */
    protected $elements = array();

    /**
     * Contains the data of the chart
     * 
     * @var array( ezcGraphDataset )
     */
    protected $data = array();

    /**
     * Renderer for the chart
     * 
     * @var ezcGraphRenderer
     */
    protected $renderer;

    /**
     * Driver for the chart
     * 
     * @var ezcGraphDriver
     */
    protected $driver;

    /**
     * Palette for default colorization
     * 
     * @var ezcGraphPalette
     */
    protected $palette;

    
    /**
     * Contains the status wheather an element should be rendered
     * 
     * @var array
     */
    protected $renderElement;

    public function __construct( array $options = array() )
    {
        $this->__set( 'palette', 'Tango' );

        // Add standard elements
        $this->addElement( 'title', new ezcGraphChartElementText() );
        $this->elements['title']->position = ezcGraph::TOP;
        $this->renderElement['title'] = false;

        $this->addElement( 'legend', new ezcGraphChartElementLegend() );
        $this->elements['legend']->position = ezcGraph::LEFT;

        // Define standard renderer and driver
        $this->driver = new ezcGraphSvgDriver();
        $this->renderer = new ezcGraphRenderer2D();
        $this->renderer->setDriver( $this->driver );
    }

    protected function addElement( $name, ezcGraphChartElement $element )
    {
        $this->elements[$name] = $element;
        $this->elements[$name]->font = $this->options->font;
        $this->elements[$name]->setFromPalette( $this->palette );

        // Render element by default
        $this->renderElement[$name] = true;
    }

    /**
     * Options write access
     * 
     * @throws ezcBasePropertyNotFoundException
     *          If Option could not be found
     * @throws ezcBaseValueException
     *          If value is out of range
     * @param mixed $propertyName   Option name
     * @param mixed $propertyValue  Option value;
     * @return mixed
     */
    public function __set( $propertyName, $propertyValue ) 
    {
        switch ( $propertyName ) {
            case 'title':
                $this->elements['title']->title = $propertyValue;
                $this->renderElement['title'] = true;
                break;
            case 'legend':
                $this->renderElement['legend'] = (bool) $propertyValue;
                break;
            case 'renderer':
                if ( $propertyValue instanceof ezcGraphRenderer )
                {
                    $this->renderer = $propertyValue;
                    $this->renderer->setDriver( $this->driver );
                    return $this->renderer;
                }
                else 
                {
                    throw new ezcGraphInvalidRendererException( $propertyValue );
                }
                break;
            case 'driver':
                if ( $propertyValue instanceof ezcGraphDriver )
                {
                    $this->driver = $propertyValue;
                    $this->renderer->setDriver( $this->driver );
                    return $this->driver;
                }
                else 
                {
                    throw new ezcGraphInvalidDriverException( $propertyValue );
                }
                break;
            case 'palette':
                if ( $propertyValue instanceof ezcGraphPalette )
                {
                    $this->palette = $propertyValue;
                }
                else
                {
                    $this->palette = ezcGraph::createPalette( $propertyValue );
                }

                $this->setFromPalette( $this->palette );

                break;
            case 'options':
                if ( $propertyValue instanceof ezcGraphChartOptions )
                {
                    $this->options = $propertyValue;
                }
                else
                {
                    throw new ezcBaseValueException( "options", $propertyValue, "instanceof ezcGraphOptions" );
                }
            default:
                throw new ezcBasePropertyNotFoundException( $propertyName );
                break;
        }
    }

    /**
     * Set colors and border fro this element
     * 
     * @param ezcGraphPalette $palette Palette
     * @return void
     */
    public function setFromPalette( ezcGraphPalette $palette )
    {
        $this->options->font->font = $palette->fontFace;
        $this->options->font->color = $palette->fontColor;
        $this->options->background = $palette->chartBackground;
        $this->options->border = $palette->chartBorderColor;
        $this->options->borderWidth = $palette->chartBorderWidth;

        foreach ( $this->elements as $element )
        {
            $element->setFromPalette( $palette );
        }
    }

    /**
     * Adds a dataset to the charts data
     * 
     * @param string $name Name of dataset
     * @param mixed $values Values to create dataset with
     * @throws ezcGraphTooManyDatasetExceptions
     *          If too many datasets are created
     * @return ezcGraphDataset
     */
    protected function addDataSet( $name, $values )
    {
        $this->data[$name] = new ezcGraphDataset();
        
        if ( is_array($values) )
        {
            $this->data[$name]->createFromArray( $values );
            $this->data[$name]->label = $name;
            $this->data[$name]->palette = $this->palette;
        }
        elseif ( $values instanceof PDOStatement )
        {
            $this->data[$name]->createFromStatement( $values );
            $this->data[$name]->label = $name;
            $this->data[$name]->palette = $this->palette;
        }
        else
        {
            throw new ezcGraphUnknownDatasetSourceException( $values );
        }
    }

    /**
     * Returns the requested property 
     * 
     * @param mixed $propertyName 
     * @return mixed
     */
    public function __get( $propertyName )
    {
        if ( isset( $this->$propertyName ) )
        {
            return $this->$propertyName;
        }

        if ( isset( $this->elements[$propertyName] ) )
        {
            return $this->elements[$propertyName];
        }

        if ( $propertyName === "options" )
        {
            return $this->options;
        }
        else
        {
            throw new ezcGraphNoSuchElementException( $propertyName );
        }
    }

    public function offsetExists( $key )
    {
        return isset( $this->data[$key] );
    }

    public function offsetGet( $key )
    {
        if ( !isset( $key ) )
        {
            throw new ezcGraphNoSuchDatasetException( $key );
        }

        return $this->data[$key];
    }

    public function offsetSet( $key, $value )
    {
        return $this->addDataset( $key, $value );
    }

    public function offsetUnset( $key )
    {
        if ( !isset( $key ) )
        {
            throw new ezcGraphNoSuchDatasetException( $key );
        }

        unset( $this->data[$key] );
    }

    public function setOptions( $options )
    {
        if ( is_array( $options ) )
        {
            $this->options->merge( $options );
        } 
        else if ( $options instanceof ezcGraphOptions )
        {
            $this->options = $options;
        }
        else
        {
            throw new ezcBaseValueException( "options", $options, "instance of ezcGraphOptions" );
        }
    }

    /**
     * Render chart border
     * 
     * @param ezcGraphBoundings $boundings Boundings
     * @return ezcGraphBoundings
     */
    protected function renderBorder( ezcGraphBoundings $boundings )
    {
        if ( ( $this->options->border instanceof ezcGraphColor ) &&
             ( $this->options->borderWidth > 0 ) )
        {
            // Default bordervalue to 1
            $this->options->borderWidth = max( 1, $this->options->borderWidth );
         
            // Draw border
            $this->renderer->drawRect(
                $this->options->border,
                new ezcGraphCoordinate( $boundings->x0, $boundings->y0 ),
                $boundings->x1 - $boundings->x0,
                $boundings->y1 - $boundings->y0,
                $this->options->borderWidth
            );

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

        return $boundings;
    }

    /**
     * Render chart background
     * 
     * @param ezcGraphBoundings $boundings Boundings
     * @return ezcGraphBoundings
     */
    protected function renderBackground( ezcGraphBoundings $boundings )
    {
        if ( $this->options->background instanceof ezcGraphColor )
        {
            $this->renderer->drawBackground(
                $this->options->background,
                new ezcGraphCoordinate( $boundings->x0, $boundings->y0 ),
                $boundings->x1 - $boundings->x0,
                $boundings->y1 - $boundings->y0
            );
        }

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

        return $boundings;
    }

    /**
     * Renders this chart
     * 
     * Creates basic visual chart elements from the chart to be processed by 
     * the renderer.
     * 
     * @return void
     */
    abstract public function render( $widht, $height, $file = null );
}

?>
OpenPOWER on IntegriCloud