diff options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | src/charts/pie.php | 6 | ||||
-rw-r--r-- | src/options/pie_chart.php | 15 | ||||
-rw-r--r-- | tests/pie_test.php | 118 |
4 files changed, 139 insertions, 1 deletions
@@ -1,6 +1,7 @@ 1.0beta2 - [RELEASEDATE] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Implemented issue #9304: Possibility to format labels via callback 1.0beta1 - Monday 25 September 2006 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/charts/pie.php b/src/charts/pie.php index 8396492..85bf7a2 100644 --- a/src/charts/pie.php +++ b/src/charts/pie.php @@ -87,13 +87,17 @@ class ezcGraphPieChart extends ezcGraphChart 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, - sprintf( $this->options->label, $label, $value, $value / $sum * 100 ), + $displayLabel, $dataset->highlight[$label] ); break; diff --git a/src/options/pie_chart.php b/src/options/pie_chart.php index 104bc5a..5974a2d 100644 --- a/src/options/pie_chart.php +++ b/src/options/pie_chart.php @@ -15,6 +15,10 @@ * %$1s Name of pie * %2$d Value of pie * %3$.1f Percentage + * @property callback $labelCallback + * Callback function to format pie chart labels. + * Function will receive 3 parameters: + * string function( label, value, percent ) * @property float $sum * Fixed sum of values. This should be used for incomplete pie * charts. @@ -40,6 +44,7 @@ class ezcGraphPieChartOptions extends ezcGraphChartOptions public function __construct( array $options = array() ) { $this->properties['label'] = '%1$s: %2$d (%3$.1f%%)'; + $this->properties['labelCallback'] = null; $this->properties['sum'] = false; $this->properties['percentTreshHold'] = .0; @@ -66,6 +71,16 @@ class ezcGraphPieChartOptions extends ezcGraphChartOptions case 'label': $this->properties['label'] = (string) $propertyValue; break; + case 'labelCallback': + if ( is_string( $propertyValue ) && function_exists( $propertyValue ) ) + { + $this->properties['labelCallback'] = $propertyValue; + } + else + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'callback function' ); + } + break; case 'sum': $this->properties['sum'] = (float) $propertyValue; break; diff --git a/tests/pie_test.php b/tests/pie_test.php index ec9350e..31c053d 100644 --- a/tests/pie_test.php +++ b/tests/pie_test.php @@ -90,6 +90,36 @@ class ezcGraphPieChartTest extends ezcImageTestCase ); } + public function testPieChartOptionsPropertyLabelCallback() + { + $options = new ezcGraphPieChartOptions(); + +/* Checking for null values does not work with current ezcBaseOptions class + $this->assertSame( + null, + $options->labelCallback, + 'Wrong default value for property labelCallback in class ezcGraphPieChartOptions' + ); */ + + $options->labelCallback = 'printf'; + $this->assertSame( + 'printf', + $options->labelCallback, + 'Setting property value did not work for property labelCallback in class ezcGraphPieChartOptions' + ); + + try + { + $options->labelCallback = 'undefined_function'; + } + catch ( ezcBasevalueException $e ) + { + return true; + } + + $this->fail( 'ezcBasevalueException expected.' ); + } + public function testPieChartOptionsPropertySum() { $options = new ezcGraphPieChartOptions(); @@ -295,6 +325,94 @@ class ezcGraphPieChartTest extends ezcImageTestCase $chart->render( 400, 200 ); } + public function testPieRenderPieSegmentsWithLabelCallback() + { + function labelCallbackFormatFunction( $label, $value, $percent ) { + return 'Callback: ' . $label; + } + + $chart = new ezcGraphPieChart(); + $chart->data['sample'] = new ezcGraphArrayDataSet( array( + 'Mozilla' => 4375, + 'IE' => 345, + 'Opera' => 1204, + 'wget' => 231, + 'Safari' => 987, + ) ); + + $chart->data['sample']->highlight['wget'] = true; + + $chart->options->labelCallback = 'labelCallbackFormatFunction'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array( + 'drawPieSegment', + ) ); + + $mockedRenderer + ->expects( $this->at( 0 ) ) + ->method( 'drawPieSegment' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 80, 0, 400, 200 ) ), + $this->equalTo( new ezcGraphContext( 'sample', 'Mozilla' ) ), + $this->equalTo( ezcGraphColor::fromHex( '#4E9A06' ) ), + $this->equalTo( 0., 1. ), + $this->equalTo( 220.5, .1 ), + $this->equalTo( 'Callback: Mozilla' ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawPieSegment' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 80, 0, 400, 200 ) ), + $this->equalTo( new ezcGraphContext( 'sample', 'IE' ) ), + $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ), + $this->equalTo( 220.5, .1 ), + $this->equalTo( 238., 1. ), + $this->equalTo( 'Callback: IE' ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 2 ) ) + ->method( 'drawPieSegment' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 80, 0, 400, 200 ) ), + $this->equalTo( new ezcGraphContext( 'sample', 'Opera' ) ), + $this->equalTo( ezcGraphColor::fromHex( '#EDD400' ) ), + $this->equalTo( 238., 1. ), + $this->equalTo( 298.6, 1. ), + $this->equalTo( 'Callback: Opera' ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawPieSegment' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 80, 0, 400, 200 ) ), + $this->equalTo( new ezcGraphContext( 'sample', 'wget' ) ), + $this->equalTo( ezcGraphColor::fromHex( '#75505B' ) ), + $this->equalTo( 298.6, 1. ), + $this->equalTo( 310., 1. ), + $this->equalTo( 'Callback: wget' ), + $this->equalTo( true ) + ); + $mockedRenderer + ->expects( $this->at( 4 ) ) + ->method( 'drawPieSegment' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 80, 0, 400, 200 ) ), + $this->equalTo( new ezcGraphContext( 'sample', 'Safari' ) ), + $this->equalTo( ezcGraphColor::fromHex( '#F57900' ) ), + $this->equalTo( 310., 1. ), + $this->equalTo( 360., 1. ), + $this->equalTo( 'Callback: Safari' ), + $this->equalTo( false ) + ); + + $chart->renderer = $mockedRenderer; + $chart->render( 400, 200 ); + } + public function testRenderSmallPieChart() { $filename = $this->tempDir . __FUNCTION__ . '.svg'; |