diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-04-12 12:45:42 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-04-12 12:45:42 +0000 |
commit | e3c691c66bc360caae052a7387a258ac8af0c2a1 (patch) | |
tree | eda7c4184d615f590e20bf142b7dcd24bbe7f20c | |
parent | 40b5444276af768209c8a6b0df633b150da86ac8 (diff) | |
download | zetacomponents-graph-e3c691c66bc360caae052a7387a258ac8af0c2a1.zip zetacomponents-graph-e3c691c66bc360caae052a7387a258ac8af0c2a1.tar.gz |
- Added feature #10470: Add support for format callback functions on all axis
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | src/axis/date.php | 15 | ||||
-rw-r--r-- | src/axis/labeled.php | 15 | ||||
-rw-r--r-- | src/axis/logarithmic.php | 27 | ||||
-rw-r--r-- | src/axis/numeric.php | 15 | ||||
-rw-r--r-- | src/element/axis.php | 16 | ||||
-rw-r--r-- | tests/date_axis_test.php | 72 | ||||
-rw-r--r-- | tests/element_options_test.php | 36 | ||||
-rw-r--r-- | tests/labeled_axis_test.php | 64 | ||||
-rw-r--r-- | tests/logarithmical_axis_test.php | 67 | ||||
-rw-r--r-- | tests/numeric_axis_test.php | 64 |
11 files changed, 385 insertions, 7 deletions
@@ -28,6 +28,7 @@ - Fixed issue #10584: API Doc for ezcGraphPdoDataSet::createFromPdo() is wrong - Fixed issue #10599: Pie chart label formattig callback only accepts callback functions but neither static nor non static methods. +- Added feature #10470: Add support for format callback functions on all axis 1.0 - Monday 18 December 2006 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/axis/date.php b/src/axis/date.php index bbf2bd4..9f72306 100644 --- a/src/axis/date.php +++ b/src/axis/date.php @@ -454,7 +454,20 @@ class ezcGraphChartElementDateAxis extends ezcGraphChartElementAxis */ public function getLabel( $step ) { - return date( $this->properties['dateFormat'], $this->startDate + ( $step * $this->interval ) ); + if ( $this->properties['labelCallback'] !== null ) + { + return call_user_func_array( + $this->properties['labelCallback'], + array( + date( $this->properties['dateFormat'], $this->startDate + ( $step * $this->interval ) ), + $step, + ) + ); + } + else + { + return date( $this->properties['dateFormat'], $this->startDate + ( $step * $this->interval ) ); + } } /** diff --git a/src/axis/labeled.php b/src/axis/labeled.php index d169d1e..131e302 100644 --- a/src/axis/labeled.php +++ b/src/axis/labeled.php @@ -167,6 +167,21 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis { $this->steps = array(); + // Apply label format callback function + if ( $this->properties['labelCallback'] !== null ) + { + foreach ( $this->labels as $nr => $label ) + { + $this->labels[$nr] = call_user_func_array( + $this->properties['labelCallback'], + array( + $label, + $nr + ) + ); + } + } + $labelCount = count( $this->labels ) - 1; if ( $labelCount === 0 ) diff --git a/src/axis/logarithmic.php b/src/axis/logarithmic.php index 370057d..a697a2d 100644 --- a/src/axis/logarithmic.php +++ b/src/axis/logarithmic.php @@ -259,11 +259,28 @@ class ezcGraphChartElementLogarithmicalAxis extends ezcGraphChartElementAxis */ public function getLabel( $step ) { - return sprintf( - $this->properties['logarithmicalFormatString'], - $this->properties['base'], - $this->properties['min'] + ( $step * $this->properties['majorStep'] ) - ); + if ( $this->properties['labelCallback'] !== null ) + { + return call_user_func_array( + $this->properties['labelCallback'], + array( + sprintf( + $this->properties['logarithmicalFormatString'], + $this->properties['base'], + $this->properties['min'] + ( $step * $this->properties['majorStep'] ) + ), + $step, + ) + ); + } + else + { + return sprintf( + $this->properties['logarithmicalFormatString'], + $this->properties['base'], + $this->properties['min'] + ( $step * $this->properties['majorStep'] ) + ); + } } /** diff --git a/src/axis/numeric.php b/src/axis/numeric.php index 9dd594c..563bb1d 100644 --- a/src/axis/numeric.php +++ b/src/axis/numeric.php @@ -348,7 +348,20 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis */ public function getLabel( $step ) { - return $this->properties['min'] + ( $step * $this->properties['majorStep'] ); + if ( $this->properties['labelCallback'] !== null ) + { + return call_user_func_array( + $this->properties['labelCallback'], + array( + $this->properties['min'] + ( $step * $this->properties['majorStep'] ), + $step, + ) + ); + } + else + { + return $this->properties['min'] + ( $step * $this->properties['majorStep'] ); + } } /** diff --git a/src/element/axis.php b/src/element/axis.php index c3cab21..b57c75d 100644 --- a/src/element/axis.php +++ b/src/element/axis.php @@ -35,6 +35,11 @@ * Maximum Size used to draw arrow heads. * @property ezcGraphAxisLabelRenderer $axisLabelRenderer * AxisLabelRenderer used to render labels and grid on this axis. + * @property callback $labelCallback + * Callback function to format pie chart labels. + * Function will receive two parameters and should return a + * reformatted label. + * string function( label, step ) * * @package Graph */ @@ -67,6 +72,7 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->properties['labelSize'] = 14; $this->properties['labelMargin'] = 2; $this->properties['maxArrowHeadSize'] = 8; + $this->properties['labelCallback'] = null; parent::__construct( $options ); @@ -186,6 +192,16 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphAxisLabelRenderer' ); } break; + case 'labelCallback': + if ( is_callable( $propertyValue ) ) + { + $this->properties['labelCallback'] = $propertyValue; + } + else + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'callback function' ); + } + break; default: parent::__set( $propertyName, $propertyValue ); break; diff --git a/tests/date_axis_test.php b/tests/date_axis_test.php index 8a4dbd6..d0615d1 100644 --- a/tests/date_axis_test.php +++ b/tests/date_axis_test.php @@ -461,6 +461,78 @@ class ezcGraphDateAxisTest extends ezcGraphTestCase ); } + public function testRenderedLabels() + { + $this->chart->data['some data'] = new ezcGraphArrayDataSet( array( + '1.1.2001' => 324, + '1.1.2002' => 324, + '1.1.2003' => 324, + '1.1.2004' => 324, + ) ); + + try + { + $this->chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $this->chart->xAxis->getSteps(); + + $expectedLabels = array( + '2001', '2002', '2003', '2004' + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } + + public function testRenderedLabelsWithLabelFormattingCallback() + { + $this->chart->data['some data'] = new ezcGraphArrayDataSet( array( + '1.1.2001' => 324, + '1.1.2002' => 324, + '1.1.2003' => 324, + '1.1.2004' => 324, + ) ); + $this->chart->xAxis->labelCallback = create_function( + '$label', + 'return "*$label*";' + ); + + try + { + $this->chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $this->chart->xAxis->getSteps(); + + $expectedLabels = array( + '*2001*', '*2002*', '*2003*', '*2004*' + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } + public function testStrToTimeLabelConvertionRendering() { $filename = $this->tempDir . __FUNCTION__ . '.svg'; diff --git a/tests/element_options_test.php b/tests/element_options_test.php index c86fab1..b2f4a23 100644 --- a/tests/element_options_test.php +++ b/tests/element_options_test.php @@ -875,6 +875,42 @@ class ezcGraphElementOptionsTest extends ezcImageTestCase $this->fail( 'Expected ezcBaseValueException.' ); } + public function testChartElementAxisPropertyLabelCallback() + { + $options = new ezcGraphChartElementNumericAxis(); + + $this->assertSame( + null, + $options->labelCallback, + 'Wrong default value for property labelCallback in class ezcGraphChartElementNumericAxis' + ); + + $options->labelCallback = 'printf'; + $this->assertSame( + 'printf', + $options->labelCallback, + 'Setting property value did not work for property labelCallback in class ezcGraphChartElementNumericAxis' + ); + + $options->labelCallback = array( $this, __METHOD__ ); + $this->assertSame( + array( $this, __METHOD__ ), + $options->labelCallback, + 'Setting property value did not work for property labelCallback in class ezcGraphChartElementNumericAxis' + ); + + try + { + $options->labelCallback = 'undefined_function'; + } + catch ( ezcBasevalueException $e ) + { + return true; + } + + $this->fail( 'ezcBasevalueException expected.' ); + } + public function testChartElementTextPropertyMaxHeight() { $options = new ezcGraphChartElementText(); diff --git a/tests/labeled_axis_test.php b/tests/labeled_axis_test.php index 2bdc2ab..8d8bf86 100644 --- a/tests/labeled_axis_test.php +++ b/tests/labeled_axis_test.php @@ -575,6 +575,70 @@ class ezcGraphLabeledAxisTest extends ezcGraphTestCase $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' ); } + + public function testRenderedLabels() + { + try + { + $chart = new ezcGraphLineChart(); + $chart->data['sample'] = new ezcGraphArrayDataSet( array( 2000 => 1045, 2001 => 1300, 2004 => 1012 ) ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->xAxis->getSteps(); + + $expectedLabels = array( + '2000', '2001', '2004' + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } + + public function testRenderedLabelsWithLabelFormattingCallback() + { + try + { + $chart = new ezcGraphLineChart(); + + $chart->xAxis->labelCallback = create_function( + '$label', + 'return "*$label*";' + ); + + $chart->data['sample'] = new ezcGraphArrayDataSet( array( 2000 => 1045, 2001 => 1300, 2004 => 1012 ) ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->xAxis->getSteps(); + + $expectedLabels = array( + '*2000*', '*2001*', '*2004*' + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } } ?> diff --git a/tests/logarithmical_axis_test.php b/tests/logarithmical_axis_test.php index 2bda3d1..8285673 100644 --- a/tests/logarithmical_axis_test.php +++ b/tests/logarithmical_axis_test.php @@ -399,5 +399,72 @@ class ezcGraphLogarithmicalAxisTest extends ezcGraphTestCase $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' ); } + + public function testRenderedLabels() + { + try + { + $chart = new ezcGraphLineChart(); + $chart->yAxis = new ezcGraphChartElementLogarithmicalAxis(); + $chart->data['sample'] = new ezcGraphArrayDataSet( array( .03, 12, 43, 1023, .02, 1.5, 9823 ) ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->yAxis->getSteps(); + + $expectedLabels = array( + '10^-2', '10^-1', '10^0', '10^1', '10^2', '10^3', '10^4', + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } + + public function testRenderedLabelsWithLabelFormattingCallback() + { + try + { + $chart = new ezcGraphLineChart(); + + $chart->yAxis = new ezcGraphChartElementLogarithmicalAxis(); + $chart->yAxis->labelCallback = create_function( + '$label', + 'return "*$label*";' + ); + + $chart->data['sample'] = new ezcGraphArrayDataSet( array( .03, 12, 43, 1023, .02, 1.5, 9823 ) ); + + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->yAxis->getSteps(); + + $expectedLabels = array( + '*10^-2*', '*10^-1*', '*10^0*', '*10^1*', '*10^2*', '*10^3*', '*10^4*', + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } } ?> diff --git a/tests/numeric_axis_test.php b/tests/numeric_axis_test.php index defd8f2..96652cb 100644 --- a/tests/numeric_axis_test.php +++ b/tests/numeric_axis_test.php @@ -739,5 +739,69 @@ class ezcGraphNumericAxisTest extends ezcTestCase $this->fail( 'Expected ezcBaseValueException.' ); } + + public function testRenderedLabels() + { + try + { + $chart = new ezcGraphLineChart(); + $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => -21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1) ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->yAxis->getSteps(); + + $expectedLabels = array( + '-100', '0', '100', '200', '300', '400', + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertEquals( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } + + public function testRenderedLabelsWithLabelFormattingCallback() + { + try + { + $chart = new ezcGraphLineChart(); + + $chart->yAxis->labelCallback = create_function( + '$label', + 'return "*$label*";' + ); + + $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => -21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1) ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphFontRenderingException $e ) + { + // Ignore + } + + $steps = $chart->yAxis->getSteps(); + + $expectedLabels = array( + '*-100*', '*0*', '*100*', '*200*', '*300*', '*400*', + ); + + foreach ( $steps as $nr => $step ) + { + $this->assertSame( + $step->label, + $expectedLabels[$nr], + 'Label not as expected' + ); + } + } } ?> |