cvs: pear /Image_Graph/Graph/Plot Pie.php /Image_Graph/docs/examples plot_pie_rest.php

From: Date: Thu, 13 Oct 2005 20:18:28 +0000
Subject: cvs: pear /Image_Graph/Graph/Plot Pie.php /Image_Graph/docs/examples plot_pie_rest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-35501@lists.php.net to get a copy of this message
nosey Thu Oct 13 16:18:28 2005 EDT Added files: /pear/Image_Graph/docs/examples plot_pie_rest.php Modified files: /pear/Image_Graph/Graph/Plot Pie.php Log: Added Pie chart support for grouping items with values below a specified threshold in a "the rest" group

http://cvs.php.net/diff.php/pear/Image_Graph/Graph/Plot/Pie.php?r1=1.17&r2=1.18&ty=u Index: pear/Image_Graph/Graph/Plot/Pie.php diff -u pear/Image_Graph/Graph/Plot/Pie.php:1.17 pear/Image_Graph/Graph/Plot/Pie.php:1.18 --- pear/Image_Graph/Graph/Plot/Pie.php:1.17 Thu Sep 29 14:21:24 2005 +++ pear/Image_Graph/Graph/Plot/Pie.php Thu Oct 13 16:18:28 2005 @@ -24,7 +24,7 @@ * @author Jesper Veggerby <pear.nosey@veggerby.dk> * @copyright Copyright (C) 2003, 2004 Jesper Veggerby Hansen * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 - * @version CVS: $Id: Pie.php,v 1.17 2005/09/29 18:21:24 nosey Exp $ + * @version CVS: $Id: Pie.php,v 1.18 2005/10/13 20:18:28 nosey Exp $ * @link http://pear.php.net/package/Image_Graph */ @@ -82,6 +82,20 @@ * @var int */ var $_diameter = false; + + /** + * Group items below this limit together as "the rest" + * @access private + * @var double + */ + var $_restGroupLimit = false; + + /** + * Rest group title + * @access private + * @var string + */ + var $_restGroupTitle = 'The rest'; /** * Perform the actual drawing on the legend. @@ -200,11 +214,34 @@ $dataset->_reset(); $currentY = 0; + $the_rest = 0; while ($point = $dataset->_next()) { - if ((!is_object($this->_dataSelector)) || - ($this->_dataSelector->select($point)) - ) { - $point = $this->_getMarkerData( + if (($this->_restGroupLimit !== false) && ($point['Y'] <= $this->_restGroupLimit)) { + $the_rest += $point['Y']; + } + else { + if ((!is_object($this->_dataSelector)) || + ($this->_dataSelector->select($point)) + ) { + $point = $this->_getMarkerData( + $point, + false, + false, + $totals + ); + if (is_array($point)) { + $this->_marker->_drawMarker( + $point['MARKER_X'], + $point['MARKER_Y'], + $point + ); + } + } + } + } + if ($the_rest > 0) { + $point = array('X' => $this->_restGroupTitle, 'Y' => $the_rest); + $point = $this->_getMarkerData( $point, false, false, @@ -217,7 +254,6 @@ $point ); } - } } $number++; } @@ -274,6 +310,20 @@ } /** + * Set the limit for the y-value, where values below are grouped together + * as "the rest" + * + * @param double $limit The limit + * @param string $title The title to display in the legends (default 'The + * rest') + */ + function setRestGroup($limit, $title = 'The rest') + { + $this->_restGroupLimit = $limit; + $this->_restGroupTitle = $title; + } + + /** * Get the diameter of the plot * @return int The number of pixels the diameter is * @access private @@ -342,13 +392,61 @@ $radius1 = ($number + 1) * $dr; } + $the_rest = 0; while ($point = $dataset->_next()) { + if (($this->_restGroupLimit !== false) && ($point['Y'] <= $this->_restGroupLimit)) { + $the_rest += $point['Y']; + } + else { + $angle1 = 360 * ($currentY / $totalY) + $this->_startingAngle; + $currentY += $this->_angleDirection * $point['Y']; + $angle2 = 360 * ($currentY / $totalY) + $this->_startingAngle; + + $x = $point['X']; + $id = $point['ID']; + + $dX = 0; + $dY = 0; + $explodeRadius = 0; + if ((is_array($this->_explode)) && (isset($this->_explode[$x]))) { + $explodeRadius = $this->_explode[$x]; + } elseif (is_numeric($this->_explode)) { + $explodeRadius = $this->_explode; + } + + if ($explodeRadius > 0) { + $dX = $explodeRadius * cos(deg2rad(($angle1 + $angle2) / 2)); + $dY = $explodeRadius * sin(deg2rad(($angle1 + $angle2) / 2)); + } + + $ID = $point['ID']; + $this->_getFillStyle($ID); + $this->_getLineStyle($ID); + $this->_canvas->pieslice( + $this->_mergeData( + $point, + array( + 'x' => $centerX + $dX, + 'y' => $centerY + $dY, + 'rx' => $radius1, + 'ry' => $radius1, + 'v1' => $angle1, + 'v2' => $angle2, + 'srx' => $radius0, + 'sry' => $radius0 + ) + ) + ); + } + } + + if ($the_rest > 0) { $angle1 = 360 * ($currentY / $totalY) + $this->_startingAngle; - $currentY += $this->_angleDirection * $point['Y']; + $currentY += $this->_angleDirection * $the_rest; $angle2 = 360 * ($currentY / $totalY) + $this->_startingAngle; - $x = $point['X']; - $id = $point['ID']; + $x = 'rest'; + $id = 'rest'; $dX = 0; $dY = 0; @@ -364,22 +462,22 @@ $dY = $explodeRadius * sin(deg2rad(($angle1 + $angle2) / 2)); } - $ID = $point['ID']; + $ID = $id; $this->_getFillStyle($ID); $this->_getLineStyle($ID); $this->_canvas->pieslice( $this->_mergeData( $point, - array( - 'x' => $centerX + $dX, - 'y' => $centerY + $dY, - 'rx' => $radius1, - 'ry' => $radius1, - 'v1' => $angle1, - 'v2' => $angle2, - 'srx' => $radius0, - 'sry' => $radius0 - ) + array( + 'x' => $centerX + $dX, + 'y' => $centerY + $dY, + 'rx' => $radius1, + 'ry' => $radius1, + 'v1' => $angle1, + 'v2' => $angle2, + 'srx' => $radius0, + 'sry' => $radius0 + ) ) ); } @@ -419,11 +517,65 @@ $count++; $dataset->_reset(); + $the_rest = 0; while ($point = $dataset->_next()) { $caption = $point['X']; - + if (($this->_restGroupLimit !== false) && ($point['Y'] <= $this->_restGroupLimit)) { + $the_rest += $point['Y']; + } + else { + $this->_canvas->setFont($param['font']); + $width = 20 + $param['width'] + $this->_canvas->textWidth($caption); + $param['maxwidth'] = max($param['maxwidth'], $width); + $x2 = $param['x'] + $width; + $y2 = $param['y'] + $param['height']+5; + + if ((($param['align'] & IMAGE_GRAPH_ALIGN_VERTICAL) != 0) && ($y2 > $param['bottom'])) { + $param['y'] = $param['top']; + $param['x'] = $x2; + $y2 = $param['y'] + $param['height']; + } elseif ((($param['align'] & IMAGE_GRAPH_ALIGN_VERTICAL) == 0) && ($x2 > $param['right'])) { + $param['x'] = $param['left']; + $param['y'] = $y2; + $x2 = $param['x'] + 20 + $param['width'] + $this->_canvas->textWidth($caption); + } + + $x = $x0 = $param['x']; + $y = $param['y']; + $y0 = $param['y'] - $param['height']/2; + $x1 = $param['x'] + $param['width']; + $y1 = $param['y'] + $param['height']/2; + + if (!isset($param['simulate'])) { + $this->_getFillStyle($point['ID']); + $this->_getLineStyle($point['ID']); + $this->_drawLegendSample($x0, $y0, $x1, $y1); + + if (($this->_marker) && ($dataset) && ($param['show_marker'])) { + $prevPoint = $dataset->_nearby(-2); + $nextPoint = $dataset->_nearby(); + + $p = $this->_getMarkerData($point, $nextPoint, $prevPoint, $totals); + if (is_array($point)) { + $p['MARKER_X'] = $x+$param['width']/2; + $p['MARKER_Y'] = $y; + unset ($p['AVERAGE_Y']); + $this->_marker->_drawMarker($p['MARKER_X'], $p['MARKER_Y'], $p); + } + } + $this->write($x + $param['width'] +10, $y, $caption, IMAGE_GRAPH_ALIGN_CENTER_Y | IMAGE_GRAPH_ALIGN_LEFT, $param['font']); + } + + if (($param['align'] & IMAGE_GRAPH_ALIGN_VERTICAL) != 0) { + $param['y'] = $y2; + } else { + $param['x'] = $x2; + } + } + } + if ($the_rest > 0) { $this->_canvas->setFont($param['font']); - $width = 20 + $param['width'] + $this->_canvas->textWidth($caption); + $width = 20 + $param['width'] + $this->_canvas->textWidth($this->_restGroupTitle); $param['maxwidth'] = max($param['maxwidth'], $width); $x2 = $param['x'] + $width; $y2 = $param['y'] + $param['height']+5; @@ -435,7 +587,7 @@ } elseif ((($param['align'] & IMAGE_GRAPH_ALIGN_VERTICAL) == 0) && ($x2 > $param['right'])) { $param['x'] = $param['left']; $param['y'] = $y2; - $x2 = $param['x'] + 20 + $param['width'] + $this->_canvas->textWidth($caption); + $x2 = $param['x'] + 20 + $param['width'] + $this->_canvas->textWidth($this->_restGroupTitle); } $x = $x0 = $param['x']; @@ -445,23 +597,11 @@ $y1 = $param['y'] + $param['height']/2; if (!isset($param['simulate'])) { - $this->_getFillStyle($point['ID']); - $this->_getLineStyle($point['ID']); + $this->_getFillStyle('rest'); + $this->_getLineStyle('rest'); $this->_drawLegendSample($x0, $y0, $x1, $y1); - if (($this->_marker) && ($dataset) && ($param['show_marker'])) { - $prevPoint = $dataset->_nearby(-2); - $nextPoint = $dataset->_nearby(); - - $p = $this->_getMarkerData($point, $nextPoint, $prevPoint, $totals); - if (is_array($point)) { - $p['MARKER_X'] = $x+$param['width']/2; - $p['MARKER_Y'] = $y; - unset ($p['AVERAGE_Y']); - $this->_marker->_drawMarker($p['MARKER_X'], $p['MARKER_Y'], $p); - } - } - $this->write($x + $param['width'] +10, $y, $caption, IMAGE_GRAPH_ALIGN_CENTER_Y | IMAGE_GRAPH_ALIGN_LEFT, $param['font']); + $this->write($x + $param['width'] + 10, $y, $this->_restGroupTitle, IMAGE_GRAPH_ALIGN_CENTER_Y | IMAGE_GRAPH_ALIGN_LEFT, $param['font']); } if (($param['align'] & IMAGE_GRAPH_ALIGN_VERTICAL) != 0) { http://cvs.php.net/co.php/pear/Image_Graph/docs/examples/plot_pie_rest.php?r=1.1&p=1 Index: pear/Image_Graph/docs/examples/plot_pie_rest.php +++ pear/Image_Graph/docs/examples/plot_pie_rest.php <?php /** * Usage example for Image_Graph. * * Main purpose: * Show pie chart * * Other: * None specific * * $Id: plot_pie_rest.php,v 1.1 2005/10/13 20:18:27 nosey Exp $ * * @package Image_Graph * @author Jesper Veggerby <pear.nosey@veggerby.dk> */ require_once 'Image/Graph.php'; // create the graph $Graph =& Image_Graph::factory('graph', array(400, 350)); // add a TrueType font $Font =& $Graph->addNew('font', 'Verdana'); // set the font size to 11 pixels $Font->setSize(8); $Graph->setFont($Font); // setup the plotarea, legend and their layout $Graph->add( Image_Graph::vertical( Image_Graph::factory('title', array('Pie Chart Sample', 12)), Image_Graph::vertical( $Plotarea = Image_Graph::factory('plotarea'), $Legend = Image_Graph::factory('legend'), 70 ), 5 ) ); $Legend->setPlotArea($Plotarea); $Plotarea->hideAxis(); // create the dataset $Dataset =& Image_Graph::factory('dataset', array( array( 'Monkey' => 12, 'Cat' => 14, 'Dog' => 13, 'Platypuss' => 29, 'Spider' => 5, 'Elephant' => 9, 'Gerbil' => 3, 'Mouse' => 19, 'Fly' => 11 ) ) ); // create the 1st plot as smoothed area chart using the 1st dataset $Plot =& $Plotarea->addNew('Image_Graph_Plot_Pie', $Dataset); $Plot->setRestGroup(11, 'Other animals'); $Plot->Radius = 2; // set a line color $Plot->setLineColor('gray'); // set a standard fill style $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); $Plot->setFillStyle($FillArray); $FillArray->addColor('green@0.2'); $FillArray->addColor('blue@0.2'); $FillArray->addColor('yellow@0.2'); $FillArray->addColor('red@0.2'); $FillArray->addColor('orange@0.2'); $FillArray->addColor('black@0.2', 'rest'); $Plot->explode(10); // create a Y data value marker $Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_PCT_Y_TOTAL); // fill it with white $Marker->setFillColor('white'); // and use black border $Marker->setBorderColor('black'); // and format it using a data preprocessor $Marker->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted', '%0.1f%%')); $Marker->setFontSize(7); // create a pin-point marker type $PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$Marker)); // and use the marker on the plot $Plot->setMarker($PointingMarker); // output the Graph $Graph->done(); ?>
« previous php.pear.cvs (#35501) next »