summaryrefslogtreecommitdiffstats
path: root/src/element/legend.php
blob: ed110f42de8d14cac6be307ee80fbdc8c4bac7a9 (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
<?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 )
    {
        $this->boundings = clone $boundings;

        switch ( $this->position )
        {
            case ezcGraph::TOP:
                $size = (int) round( $boundings->y0 + ( $boundings->y1 - $boundings->y0) * $this->landscapeSize );

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

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

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

                $boundings->y1 -= $size;
                $this->boundings->y0 = $boundings->y1;
                break;
        }

        return $boundings;
    }

    /**
     * Render a legend
     * 
     * @param ezcGraphRenderer $renderer 
     * @access public
     * @return void
     */
    public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
    {
        $boundings = $this->calculateBoundings( $boundings );
        
        if ( $this->position === ezcGraph::LEFT || $this->position === ezcGraph::RIGHT )
        {
            $type = ezcGraph::VERTICAL;
        }
        else
        {
            $type = ezcGraph::HORIZONTAL;
        }

        // Render standard elements
        $this->boundings = $renderer->drawBox(
            $this->boundings,
            $this->background,
            $this->border,
            $this->borderWidth,
            $this->margin,
            $this->padding,
            $this->title,
            $this->getTitleSize( $this->boundings, $type )
        );

        // Render legend
        $renderer->drawLegend(
            $this->boundings,
            $this,
            $type
        );

        var_dump( $this->boundings, $boundings );

        return $boundings;  
    }
}

?>
OpenPOWER on IntegriCloud