summaryrefslogtreecommitdiffstats
path: root/src/element/legend.php
blob: a8192ae48a6d3efeb793d7391b5f26ff6ec10ad5 (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
<?php
/**
 * File containing the abstract ezcGraphChartElementLegend 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 legend as a chart element
 *
 * @package Graph
 */
class ezcGraphChartElementLegend extends ezcGraphChartElement
{

    /**
     * Contains data which should be shown in the legend
     *  array(
     *      array(
     *          'label' => (string) 'Label of data element',
     *          'color' => (ezcGraphColor) $color,
     *          'symbol' => (integer) ezcGraph::DIAMOND,
     *      ),
     *      ...
     *  )
     * 
     * @var array
     */
    protected $labels;

    /**
     * Size of a portrait style legend in percent of the size of the complete 
     * chart
     * 
     * @var float
     */
    protected $portraitSize = .2;

    /**
     * Size of a landscape style legend in percent of the size of the complete 
     * chart
     * 
     * @var float
     */
    protected $landscapeSize = .1;

    /**
     * Standard size of symbols and text in legends 
     * 
     * @var integer
     */
    protected $symbolSize = 14;

    /**
     * Padding for label elements 
     * 
     * @var integer
     */
    protected $padding = 1;

    /**
     * Scale symbol size up to to percent of complete legends size for very
     * big legends
     * 
     * @var float
     */
    protected $minimumSymbolSize = .05;

    /**
     * Space between lael elements in pixel 
     * 
     * @var integer
     */
    protected $spacing = 2;

    /**
     * __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 'padding':
                $this->padding = max( 0, (int) $propertyValue );
                break;
            case 'symbolSize':
                $this->symbolSize = max( 1, (int) $propertyValue );
                break;
            case 'landscapeSize':
                $this->landscapeSize = max( 0, min( 1, (float) $propertyValue ) );
                break;
            case 'portraitSize':
                $this->portraitSize = max( 0, min( 1, (float) $propertyValue ) );
                break;
            case 'spacing':
                $this->portraitSize = max( 0, (int) $propertyValue );
                break;
            default:
                parent::__set( $propertyName, $propertyValue );
                break;
        }
    }

    /**
     * Generate legend from several datasets with on entry per dataset
     * 
     * @param array $datasets 
     * @return void
     */
    public function generateFromDatasets(array $datasets)
    {
        $this->labels = array();
        foreach ($datasets as $dataset)
        {
            $this->labels[] = array(
                'label' => $dataset->label->default,
                'color' => $dataset->color->default,
                'symbol' => ( $dataset->symbol->default === null ?
                              ezcGraph::NO_SYMBOL :
                              $dataset->symbol->default ),
            );
        }
    }

    /**
     * Generate legend from single dataset with on entry per data element 
     * 
     * @param ezcGraphDataset $dataset 
     * @return void
     */
    public function generateFromDataset(ezcGraphDataset $dataset)
    {
        $this->labels = array();
        foreach ($dataset as $label => $data)
        {
            $this->labels[] = array(
                'label' => $label,
                'color' => $dataset->color[$label],
                'symbol' => ( $dataset->symbol[$label] === null ?
                              ezcGraph::NO_SYMBOL :
                              $dataset->symbol[$label] ),
            );
        }
    }
    
    protected function calculateBoundings( ezcGraphBoundings $boundings )
    {
        switch ( $this->position )
        {
            case ezcGraph::TOP:
                $this->boundings = clone $boundings;

                $size = (int) round( $boundings->y0 + ( $boundings->y1 - $boundings->y0) * $this->landscapeSize );
                $this->boundings->y1 = $size;
                $boundings->y0 = $size;
                break;
            case ezcGraph::LEFT:
                $this->boundings = clone $boundings;

                $size = (int) round( $boundings->x0 + ( $boundings->x1 - $boundings->x0) * $this->portraitSize );
                $this->boundings->x1 = $size;
                $boundings->x0 = $size;
                break;
            case ezcGraph::RIGHT:
                $this->boundings = clone $boundings;

                $size = (int) round( $boundings->x1 - ( $boundings->x1 - $boundings->x0) * $this->portraitSize );
                $this->boundings->x0 = $size;
                $boundings->x1 = $size;
                break;
            case ezcGraph::BOTTOM:
                $this->boundings = clone $boundings;

                $size = (int) round( $boundings->y1 - ( $boundings->y1 - $boundings->y0) * $this->landscapeSize );
                $this->boundings->y0 = $size;
                $boundings->y1 = $size;
                break;
        }

        return $boundings;
    }

    protected function renderLegend( ezcGraphRenderer $renderer )
    {
        switch ( $this->position )
        {
            case ezcGraph::LEFT:
            case ezcGraph::RIGHT:
                $symbolSize = (int) round( min(
                    max(
                        $this->symbolSize,
                        ( $this->boundings->y1 - $this->boundings->y0 ) * $this->minimumSymbolSize
                    ),
                    ( $this->boundings->y1 - $this->boundings->y0 ) / count( $this->labels )
                ) );

                foreach ( $this->labels as $labelNr => $label )
                {
                    $renderer->drawSymbol(
                        $label['color'],
                        new ezcGraphCoordinate( 
                            $this->boundings->x0 + $this->padding,
                            $this->boundings->y0 + $labelNr * ( $symbolSize + $this->spacing ) + $this->padding
                        ),
                        $symbolSize - 2 * $this->padding,
                        $symbolSize - 2 * $this->padding,
                        $label['symbol']
                    );
                    $renderer->drawTextBox(
                        new ezcGraphCoordinate(
                            $this->boundings->x0 + $symbolSize + $this->spacing,
                            $this->boundings->y0 + $labelNr * ( $symbolSize + $this->spacing ) + $this->padding
                        ),
                        $label['label'],
                        $this->boundings->x1 - $this->boundings->x0 - $symbolSize - $this->padding - $this->spacing,
                        $symbolSize - 2 * $this->padding,
                        ezcGraph::LEFT | ezcGraph::MIDDLE
                    );
                }
                break;
            case ezcGraph::TOP:
            case ezcGraph::BOTTOM:
                $symbolSize = (int) round( min(
                    $this->symbolSize,
                    ( $this->boundings->y1 - $this->boundings->y0 )
                ) );
                $width = (int) round( ( $this->boundings->x1 - $this->boundings->x0 ) / count( $this->labels ) );

                foreach ( $this->labels as $labelNr => $label )
                {
                    $renderer->drawSymbol(
                        $label['color'],
                        new ezcGraphCoordinate( 
                            $this->boundings->x0 + $labelNr * $width + $this->padding,
                            $this->boundings->y0 + $this->padding
                        ),
                        $symbolSize - 2 * $this->padding,
                        $symbolSize - 2 * $this->padding,
                        $label['symbol']
                    );
                    $renderer->drawTextBox(
                        new ezcGraphCoordinate(
                            $this->boundings->x0 + $labelNr * $width + $this->padding + $symbolSize + $this->spacing,
                            $this->boundings->y0 + $this->padding
                        ),
                        $label['label'],
                        $width - $this->padding - $symbolSize - $this->spacing,
                        $symbolSize - 2 * $this->padding,
                        ezcGraph::LEFT | ezcGraph::MIDDLE
                    );
                }
                break;
        }
    }

    /**
     * Render a legend
     * 
     * @param ezcGraphRenderer $renderer 
     * @access public
     * @return void
     */
    public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
    {
        $boundings = $this->calculateBoundings( $boundings );
        
        // Render standard elements
        $this->renderBorder( $renderer );
        $this->renderBackground( $renderer );
        $this->renderTitle( $renderer );

        // Render legend
        $this->renderLegend( $renderer );

        return $boundings;  
    }
}

?>
OpenPOWER on IntegriCloud