* // 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' ); * * * @package Graph */ 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 applyTreshHold() { if ( $this->options->percentTreshHold || $this->options->absoluteTreshHold ) { $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 ( ( $value <= $this->options->absoluteTreshHold ) || ( ( $value / $sum ) <= $this->options->percentTreshHold ) ) { 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 ) { // Set image properties in driver $this->driver->options->width = $width; $this->driver->options->height = $height; // Apply tresh hold $this->applyTreshHold(); // 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 ); } } } ?>