summaryrefslogtreecommitdiffstats
path: root/src/charts/radar.php
blob: 92473bc228bbe104e6bf4689a7ba29b1287a111b (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/**
 * File containing the ezcGraphRadarChart class
 *
 * @package Graph
 * @version //autogentag//
 * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 */
/**
 * Class for radar charts.
 * Can make use of an unlimited amount of datasets and will display them as
 * lines by default.
 * Rotation axis:
 *  - Labeled axis
 *  - Centered axis label renderer
 * Axis:
 *  - Numeric axis
 *  - radar axis label renderer
 *
 * <code>
 *  // Create a new radar chart
 *  $chart = new ezcGraphRadarChart();
 *
 *  // Add data to line chart
 *  $chart->data['sample dataset'] = new ezcGraphArrayDataSet(
 *      array(
 *          '100' => 1.2,
 *          '200' => 43.2,
 *          '300' => -34.14,
 *          '350' => 65,
 *          '400' => 123,
 *      )   
 *  );
 *
 *  // Render chart with default 2d renderer and default SVG driver
 *  $chart->render( 500, 200, 'radar_chart.svg' );
 * </code>
 *
 * Each chart consists of several chart elements which represents logical 
 * parts of the chart and can be formatted independently. The line chart
 * consists of:
 *  - title ( {@link ezcGraphChartElementText} )
 *  - legend ( {@link ezcGraphChartElementLegend} )
 *  - background ( {@link ezcGraphChartElementBackground} )
 *  - axis ( {@link ezcGraphChartElementNumericAxis} )
 *  - ratation axis ( {@link ezcGraphChartElementLabeledAxis} )
 *
 * The type of the axis may be changed and all elements can be configured by
 * accessing them as properties of the chart:
 *
 * The chart itself also offers several options to configure the appearance.
 * The extended configure options are available in 
 * {@link ezcGraphRadarChartOptions} extending the 
 * {@link ezcGraphChartOptions}.
 *
 * <code>
 *  $chart->legend->position = ezcGraph::RIGHT;
 * </code>
 *
 * @property ezcGraphRadarChartOptions $options
 *           Chart options class
 *
 * @version //autogentag//
 * @package Graph
 * @mainclass
 */
class ezcGraphRadarChart extends ezcGraphChart
{
    /**
     * Store major grid color for child axis.
     * 
     * @var ezcGraphColor
     */
    protected $childAxisColor;

    /**
     * Constructor
     * 
     * @param array $options Default option array
     * @return void
     * @ignore
     */
    public function __construct( array $options = array() )
    {
        $this->options = new ezcGraphRadarChartOptions( $options );
        $this->options->highlightFont = $this->options->font;

        parent::__construct();

        $this->elements['rotationAxis'] = new ezcGraphChartElementLabeledAxis();

        $this->addElement( 'axis', new ezcGraphChartElementNumericAxis() );
        $this->elements['axis']->position = ezcGraph::BOTTOM;
        $this->elements['axis']->axisLabelRenderer = new ezcGraphAxisRadarLabelRenderer();
        $this->elements['axis']->axisLabelRenderer->outerStep = true;

        $this->addElement( 'rotationAxis', new ezcGraphChartElementLabeledAxis() );

        // Do not render axis with default method, because we need an axis for
        // each label in dataset
        $this->renderElement['axis'] = false;
        $this->renderElement['rotationAxis'] = false;
    }

    /**
     * Set colors and border fro this element
     * 
     * @param ezcGraphPalette $palette Palette
     * @return void
     */
    public function setFromPalette( ezcGraphPalette $palette )
    {
        $this->childAxisColor = $palette->majorGridColor;

        parent::setFromPalette( $palette );
    }

    /**
     * Property write access
     * 
     * @throws ezcBasePropertyNotFoundException
     *          If Option could not be found
     * @throws ezcBaseValueException
     *          If value is out of range
     * @param string $propertyName Option name
     * @param mixed $propertyValue Option value;
     * @return void
     * @ignore
     */
    public function __set( $propertyName, $propertyValue ) 
    {
        switch ( $propertyName ) {
            case 'axis':
                if ( $propertyValue instanceof ezcGraphChartElementAxis )
                {
                    $this->addElement( 'axis', $propertyValue );
                    $this->elements['axis']->position = ezcGraph::BOTTOM;
                    $this->elements['axis']->axisLabelRenderer = new ezcGraphAxisRadarLabelRenderer();
                    $this->renderElement['axis'] = false;
                }
                else
                {
                    throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphChartElementAxis' );
                }
                break;
            case 'rotationAxis':
                if ( $propertyValue instanceof ezcGraphChartElementAxis )
                {
                    $this->addElement( 'rotationAxis', $propertyValue );
                    $this->renderElement['rotationAxis'] = false;
                }
                else
                {
                    throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphChartElementAxis' );
                }
                break;
            case 'renderer':
                if ( $propertyValue instanceof ezcGraphRadarRenderer )
                {
                    parent::__set( $propertyName, $propertyValue );
                }
                else 
                {
                    throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphRadarRenderer' );
                }
                break;
            default:
                parent::__set( $propertyName, $propertyValue );
        }
    }

    /**
     * Draws a single rotated axis
     *
     * Sets the axis label position depending on the axis rotation.
     * 
     * @param ezcGraphChartElementAxis $axis 
     * @param ezcGraphBoundings $boundings 
     * @param ezcGraphCoordinate $center 
     * @param float $position 
     * @param float $lastPosition 
     * @return void
     */
    protected function drawRotatedAxis( ezcGraphChartElementAxis $axis, ezcGraphBoundings $boundings, ezcGraphCoordinate $center, $position, $lastPosition = null )
    {
        // Set axis position depending on angle for better axis label 
        // positioning
        $angle = $position * 2 * M_PI;
        switch ( (int) ( ( $position + .125 ) * 4 ) )
        {
            case 0:
            case 4:
                $axis->position = ezcGraph::BOTTOM;
                break;
            case 1:
                $axis->position = ezcGraph::LEFT;
                break;
            case 2:
                $axis->position = ezcGraph::TOP;
                break;
            case 3:
                $axis->position = ezcGraph::RIGHT;
                break;
        }

        // Set last step to correctly draw grid
        if ( $axis->axisLabelRenderer instanceof ezcGraphAxisRadarLabelRenderer )
        {
            $axis->axisLabelRenderer->lastStep = $lastPosition;
        }

        // Do not draw axis label for last step
        if ( abs( $position - 1 ) <= .001 ) 
        {
            $axis->label = null;
        }

        $this->renderer->drawAxis(
            $boundings,
            clone $center,
            $dest = new ezcGraphCoordinate(
                $center->x + sin( $angle ) * ( $boundings->width / 2 ),
                $center->y - cos( $angle ) * ( $boundings->height / 2 )
            ),
            clone $axis,
            clone $axis->axisLabelRenderer
        );
    }

    /**
     * Render the assigned data
     *
     * Will renderer all charts data in the remaining boundings after drawing 
     * all other chart elements. The data will be rendered depending on the 
     * settings in the dataset.
     * 
     * @param ezcGraphRenderer $renderer Renderer
     * @param ezcGraphBoundings $boundings Remaining boundings
     * @return void
     */
    protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
    {
        // Apply axis space
        $xAxisSpace = ( $boundings->x1 - $boundings->x0 ) * $this->axis->axisSpace;
        $yAxisSpace = ( $boundings->y1 - $boundings->y0 ) * $this->axis->axisSpace;

        $center = new ezcGraphCoordinate(
            ( $boundings->width / 2 ),
            ( $boundings->height / 2 )
        );

        // We do not differentiate between display types in radar charts.
        $nr = $count = count( $this->data );

        // Draw axis at major steps of virtual axis
        $steps = $this->elements['rotationAxis']->getSteps();
        $lastStepPosition = null;
        $axisColor = $this->elements['axis']->border;
        foreach ( $steps as $step )
        {
            $this->elements['axis']->label = $step->label;
            $this->drawRotatedAxis( $this->elements['axis'], $boundings, $center, $step->position, $lastStepPosition );
            $lastStepPosition = $step->position;

            if ( count( $step->childs ) )
            {
                foreach ( $step->childs as $childStep )
                {
                    $this->elements['axis']->label = null;
                    $this->elements['axis']->border = $this->childAxisColor;

                    $this->drawRotatedAxis( $this->elements['axis'], $boundings, $center, $childStep->position, $lastStepPosition );
                    $lastStepPosition = $childStep->position;
                }
            }

            $this->elements['axis']->border = $axisColor;
        }

        // Display data
        $this->elements['axis']->position = ezcGraph::TOP;
        foreach ( $this->data as $datasetName => $data )
        {
            --$nr;
            // Determine fill color for dataset
            if ( $this->options->fillLines !== false )
            {
                $fillColor = clone $data->color->default;
                $fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) );
            }
            else
            {
                $fillColor = null;
            }

            // Draw lines for dataset
            $lastPoint = false;
            foreach ( $data as $key => $value )
            {
                $point = new ezcGraphCoordinate( 
                    $this->elements['rotationAxis']->getCoordinate( $key ),
                    $this->elements['axis']->getCoordinate( $value )
                ); 

                /* Transformation required for 3d like renderers ... 
                 * which axis should transform here?
                $point = $this->elements['xAxis']->axisLabelRenderer->modifyChartDataPosition( 
                    $this->elements['yAxis']->axisLabelRenderer->modifyChartDataPosition(
                        new ezcGraphCoordinate( 
                            $this->elements['xAxis']->getCoordinate( $key ),
                            $this->elements['yAxis']->getCoordinate( $value )
                        )
                    )
                ); 
                // */

                $renderer->drawRadarDataLine(
                    $boundings,
                    new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
                    $data->color->default,
                    clone $center,
                    ( $lastPoint === false ? $point : $lastPoint ),
                    $point,
                    $nr,
                    $count,
                    $data->symbol[$key],
                    $data->color[$key],
                    $fillColor,
                    $this->options->lineThickness
                );

                $lastPoint = $point;
            }
        }
    }

    /**
     * Returns the default display type of the current chart type.
     * 
     * @return int Display type
     */
    public function getDefaultDisplayType()
    {
        return ezcGraph::LINE;
    }

    /**
     * Renders the basic elements of this chart type
     * 
     * @param int $width 
     * @param int $height 
     * @return void
     */
    protected function renderElements( $width, $height )
    {
        if ( !count( $this->data ) )
        {
            throw new ezcGraphNoDataException();
        }

        // Set image properties in driver
        $this->driver->options->width = $width;
        $this->driver->options->height = $height;

        // Calculate axis scaling and labeling
        foreach ( $this->data as $dataset )
        {
            $labels = array();
            $values = array();
            foreach ( $dataset as $label => $value )
            {
                $labels[] = $label;
                $values[] = $value;
            }

            $this->elements['axis']->addData( $values );
            $this->elements['rotationAxis']->addData( $labels );
        }

        $this->elements['axis']->calculateAxisBoundings();
        $this->elements['rotationAxis']->calculateAxisBoundings();

        // Generate legend
        $this->elements['legend']->generateFromDataSets( $this->data );

        // Get boundings from parameters
        $this->options->width = $width;
        $this->options->height = $height;

        // Render subelements
        $boundings = new ezcGraphBoundings();
        $boundings->x1 = $this->options->width;
        $boundings->y1 = $this->options->height;

        // Render subelements
        foreach ( $this->elements as $name => $element )
        {
            // Skip element, if it should not get rendered
            if ( $this->renderElement[$name] === false )
            {
                continue;
            }

            $this->driver->options->font = $element->font;
            $boundings = $element->render( $this->renderer, $boundings );
        }

        // Render graph
        $this->renderData( $this->renderer, $boundings );
    }

    /**
     * Render the line chart
     *
     * Renders the chart into a file or stream. The width and height are 
     * needed to specify the dimensions of the resulting image. For direct
     * output use 'php://stdout' as output file.
     * 
     * @param int $width Image width
     * @param int $height Image height
     * @param string $file Output file
     * @apichange
     * @return void
     */
    public function render( $width, $height, $file = null )
    {
        $this->renderElements( $width, $height );

        if ( !empty( $file ) )
        {
            $this->renderer->render( $file );
        }

        $this->renderedFile = $file;
    }

    /**
     * Renders this chart to direct output
     * 
     * Does the same as ezcGraphChart::render(), but renders directly to 
     * output and not into a file.
     *
     * @param int $width
     * @param int $height
     * @apichange
     * @return void
     */
    public function renderToOutput( $width, $height )
    {
        // @TODO: merge this function with render an deprecate ommit of third 
        // argument in render() when API break is possible
        $this->renderElements( $width, $height );
        $this->renderer->render( null );
    }
}
?>
OpenPOWER on IntegriCloud