| [ Index ] |
PHP Cross Reference of MantisBT |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * File containing the ezcGraphPieChart class 4 * 5 * @package Graph 6 * @version 1.5 7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. 8 * @license http://ez.no/licenses/new_bsd New BSD License 9 */ 10 /** 11 * Class for pie charts. Can only use one dataset which will be dispalyed as a 12 * pie chart. 13 * 14 * <code> 15 * // Create a new pie chart 16 * $chart = new ezcGraphPieChart(); 17 * 18 * // Add data to line chart 19 * $chart->data['sample dataset'] = new ezcGraphArrayDataSet( 20 * array( 21 * 'one' => 1.2, 22 * 'two' => 43.2, 23 * 'three' => -34.14, 24 * 'four' => 65, 25 * 'five' => 123, 26 * ) 27 * ); 28 * 29 * // Render chart with default 2d renderer and default SVG driver 30 * $chart->render( 500, 200, 'pie_chart.svg' ); 31 * </code> 32 * 33 * Each chart consists of several chart elements which represents logical 34 * parts of the chart and can be formatted independently. The pie chart 35 * consists of: 36 * - title ( {@link ezcGraphChartElementText} ) 37 * - legend ( {@link ezcGraphChartElementLegend} ) 38 * - background ( {@link ezcGraphChartElementBackground} ) 39 * 40 * All elements can be configured by accessing them as properties of the chart: 41 * 42 * <code> 43 * $chart->legend->position = ezcGraph::RIGHT; 44 * </code> 45 * 46 * The chart itself also offers several options to configure the appearance. 47 * The extended configure options are available in 48 * {@link ezcGraphPieChartOptions} extending the {@link ezcGraphChartOptions}. 49 * 50 * @property ezcGraphPieChartOptions $options 51 * Chart options class 52 * 53 * @version 1.5 54 * @package Graph 55 * @mainclass 56 */ 57 class ezcGraphPieChart extends ezcGraphChart 58 { 59 60 /** 61 * Constructor 62 * 63 * @param array $options Default option array 64 * @return void 65 * @ignore 66 */ 67 public function __construct( array $options = array() ) 68 { 69 $this->options = new ezcGraphPieChartOptions( $options ); 70 71 parent::__construct( $options ); 72 73 $this->data = new ezcGraphChartSingleDataContainer( $this ); 74 } 75 76 /** 77 * Render the assigned data 78 * 79 * Will renderer all charts data in the remaining boundings after drawing 80 * all other chart elements. The data will be rendered depending on the 81 * settings in the dataset. 82 * 83 * @param ezcGraphRenderer $renderer Renderer 84 * @param ezcGraphBoundings $boundings Remaining boundings 85 * @return void 86 */ 87 protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) 88 { 89 // Only draw the first (and only) dataset 90 $dataset = $this->data->rewind(); 91 $datasetName = $this->data->key(); 92 93 $this->driver->options->font = $this->options->font; 94 95 // Calculate sum of all values to be able to calculate percentage 96 $sum = 0; 97 foreach ( $dataset as $name => $value ) 98 { 99 if ( $value < 0 ) 100 { 101 throw new ezcGraphInvalidDataException( "Values >= 0 required, '$name' => '$value'." ); 102 } 103 104 $sum += $value; 105 } 106 if ( $this->options->sum !== false ) 107 { 108 $sum = max( $sum, $this->options->sum ); 109 } 110 111 if ( $sum <= 0 ) 112 { 113 throw new ezcGraphInvalidDataException( "Pie charts require a value sum > 0, your value: '$sum'." ); 114 } 115 116 $angle = 0; 117 foreach ( $dataset as $label => $value ) 118 { 119 // Skip rendering values which equals 0 120 if ( $value <= 0 ) 121 { 122 continue; 123 } 124 125 switch ( $dataset->displayType->default ) 126 { 127 case ezcGraph::PIE: 128 $displayLabel = ( $this->options->labelCallback !== null 129 ? call_user_func( $this->options->labelCallback, $label, $value, $value / $sum ) 130 : sprintf( $this->options->label, $label, $value, $value / $sum * 100 ) ); 131 132 $renderer->drawPieSegment( 133 $boundings, 134 new ezcGraphContext( $datasetName, $label, $dataset->url[$label] ), 135 $dataset->color[$label], 136 $angle, 137 $angle += $value / $sum * 360, 138 $displayLabel, 139 $dataset->highlight[$label] 140 ); 141 break; 142 default: 143 throw new ezcGraphInvalidDisplayTypeException( $dataset->displayType->default ); 144 break; 145 } 146 } 147 } 148 149 /** 150 * Returns the default display type of the current chart type. 151 * 152 * @return int Display type 153 */ 154 public function getDefaultDisplayType() 155 { 156 return ezcGraph::PIE; 157 } 158 159 /** 160 * Apply tresh hold 161 * 162 * Iterates over the dataset and applies the configured tresh hold to 163 * the datasets data. 164 * 165 * @return void 166 */ 167 protected function applyThreshold() 168 { 169 if ( $this->options->percentThreshold || $this->options->absoluteThreshold ) 170 { 171 $dataset = $this->data->rewind(); 172 173 $sum = 0; 174 foreach ( $dataset as $value ) 175 { 176 $sum += $value; 177 } 178 if ( $this->options->sum !== false ) 179 { 180 $sum = max( $sum, $this->options->sum ); 181 } 182 183 $unset = array(); 184 foreach ( $dataset as $label => $value ) 185 { 186 if ( $label === $this->options->summarizeCaption ) 187 { 188 continue; 189 } 190 191 if ( ( $value <= $this->options->absoluteThreshold ) || 192 ( ( $value / $sum ) <= $this->options->percentThreshold ) ) 193 { 194 if ( !isset( $dataset[$this->options->summarizeCaption] ) ) 195 { 196 $dataset[$this->options->summarizeCaption] = $value; 197 } 198 else 199 { 200 $dataset[$this->options->summarizeCaption] += $value; 201 } 202 203 $unset[] = $label; 204 } 205 } 206 207 foreach ( $unset as $label ) 208 { 209 unset( $dataset[$label] ); 210 } 211 } 212 } 213 214 /** 215 * Renders the basic elements of this chart type 216 * 217 * @param int $width 218 * @param int $height 219 * @return void 220 */ 221 protected function renderElements( $width, $height ) 222 { 223 if ( !count( $this->data ) ) 224 { 225 throw new ezcGraphNoDataException(); 226 } 227 228 // Set image properties in driver 229 $this->driver->options->width = $width; 230 $this->driver->options->height = $height; 231 232 // Apply tresh hold 233 $this->applyThreshold(); 234 235 // Generate legend 236 $this->elements['legend']->generateFromDataSet( $this->data->rewind() ); 237 238 // Get boundings from parameters 239 $this->options->width = $width; 240 $this->options->height = $height; 241 242 $boundings = new ezcGraphBoundings(); 243 $boundings->x1 = $this->options->width; 244 $boundings->y1 = $this->options->height; 245 246 // Render subelements 247 foreach ( $this->elements as $name => $element ) 248 { 249 // Skip element, if it should not get rendered 250 if ( $this->renderElement[$name] === false ) 251 { 252 continue; 253 } 254 255 $this->driver->options->font = $element->font; 256 $boundings = $element->render( $this->renderer, $boundings ); 257 } 258 259 // Render graph 260 $this->renderData( $this->renderer, $boundings ); 261 } 262 263 /** 264 * Render the pie chart 265 * 266 * Renders the chart into a file or stream. The width and height are 267 * needed to specify the dimensions of the resulting image. For direct 268 * output use 'php://stdout' as output file. 269 * 270 * @param int $width Image width 271 * @param int $height Image height 272 * @param string $file Output file 273 * @apichange 274 * @return void 275 */ 276 public function render( $width, $height, $file = null ) 277 { 278 $this->renderElements( $width, $height ); 279 280 if ( !empty( $file ) ) 281 { 282 $this->renderer->render( $file ); 283 } 284 285 $this->renderedFile = $file; 286 } 287 288 /** 289 * Renders this chart to direct output 290 * 291 * Does the same as ezcGraphChart::render(), but renders directly to 292 * output and not into a file. 293 * 294 * @param int $width 295 * @param int $height 296 * @apichange 297 * @return void 298 */ 299 public function renderToOutput( $width, $height ) 300 { 301 // @TODO: merge this function with render an deprecate ommit of third 302 // argument in render() when API break is possible 303 $this->renderElements( $width, $height ); 304 $this->renderer->render( null ); 305 } 306 } 307 308 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jul 28 15:48:31 2011 | Cross-referenced by PHPXref 0.7 |