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
|
<?php
/**
* File containing the ezcGraphPieChart 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 for pie charts. Can only use one dataset which will be dispalyed as a
* pie chart.
*
* <code>
* // Create a new line chart
* $chart = new ezcGraphLineChart();
*
* // Add data to line chart
* $chart->data['sample dataset'] = new ezcGraphArrayDataSet(
* array(
* 'one' => 1.2,
* 'two' => 43.2,
* 'three' => -34.14,
* 'four' => 65,
* 'five' => 123,
* )
* );
*
* // Render chart with default 2d renderer and default SVG driver
* $chart->render( 500, 200, 'line_chart.svg' );
* </code>
*
* @package Graph
* @mainclass
*/
class ezcGraphPieChart extends ezcGraphChart
{
/**
* Constructor
*
* @param array $options Default option array
* @return void
* @ignore
*/
public function __construct( array $options = array() )
{
$this->options = new ezcGraphPieChartOptions( $options );
parent::__construct( $options );
$this->data = new ezcGraphChartSingleDataContainer( $this );
}
/**
* 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 )
{
// Only draw the first (and only) dataset
$dataset = $this->data->rewind();
$datasetName = $this->data->key();
$this->driver->options->font = $this->options->font;
// Calculate sum of all values to be able to calculate percentage
$sum = 0;
foreach ( $dataset as $value )
{
$sum += $value;
}
if ( $this->options->sum !== false )
{
$sum = max( $sum, $this->options->sum );
}
$angle = 0;
foreach ( $dataset as $label => $value )
{
switch ( $dataset->displayType->default )
{
case ezcGraph::PIE:
$displayLabel = ( $this->options->labelCallback !== null
? call_user_func( $this->options->labelCallback, $label, $value, $value / $sum )
: sprintf( $this->options->label, $label, $value, $value / $sum * 100 ) );
$renderer->drawPieSegment(
$boundings,
new ezcGraphContext( $datasetName, $label ),
$dataset->color[$label],
$angle,
$angle += $value / $sum * 360,
$displayLabel,
$dataset->highlight[$label]
);
break;
default:
throw new ezcGraphInvalidDisplayTypeException( $dataset->displayType->default );
break;
}
}
}
/**
* Returns the default display type of the current chart type.
*
* @return int Display type
*/
public function getDefaultDisplayType()
{
return ezcGraph::PIE;
}
/**
* Apply tresh hold
*
* Iterates over the dataset and applies the configured tresh hold to
* the datasets data.
*
* @return void
*/
protected function applyThreshold()
{
if ( $this->options->percentThreshold || $this->options->absoluteThreshold )
{
$dataset = $this->data->rewind();
$sum = 0;
foreach ( $dataset as $value )
{
$sum += $value;
}
if ( $this->options->sum !== false )
{
$sum = max( $sum, $this->options->sum );
}
$unset = array();
foreach ( $dataset as $label => $value )
{
if ( $label === $this->options->summarizeCaption )
{
continue;
}
if ( ( $value <= $this->options->absoluteThreshold ) ||
( ( $value / $sum ) <= $this->options->percentThreshold ) )
{
if ( !isset( $dataset[$this->options->summarizeCaption] ) )
{
$dataset[$this->options->summarizeCaption] = $value;
}
else
{
$dataset[$this->options->summarizeCaption] += $value;
}
$unset[] = $label;
}
}
foreach ( $unset as $label )
{
unset( $dataset[$label] );
}
}
}
/**
* Render the pie 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
* @return void
*/
public function render( $width, $height, $file = null )
{
if ( !count( $this->data ) )
{
throw new ezcGraphNoDataException();
}
// Set image properties in driver
$this->driver->options->width = $width;
$this->driver->options->height = $height;
// Apply tresh hold
$this->applyThreshold();
// Generate legend
$this->elements['legend']->generateFromDataSet( $this->data->rewind() );
// Get boundings from parameters
$this->options->width = $width;
$this->options->height = $height;
$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 );
if ( !empty( $file ) )
{
$this->renderer->render( $file );
}
}
}
?>
|