Author: Kore Nordmann Date: 2007-04-12 14:45:42 +0200 (Thu, 12 Apr 2007) New Revision: 4871
Log: - Added feature #10470: Add support for format callback functions on all axis Modified: trunk/Graph/ChangeLog trunk/Graph/src/axis/date.php trunk/Graph/src/axis/labeled.php trunk/Graph/src/axis/logarithmic.php trunk/Graph/src/axis/numeric.php trunk/Graph/src/element/axis.php trunk/Graph/tests/date_axis_test.php trunk/Graph/tests/element_options_test.php trunk/Graph/tests/labeled_axis_test.php trunk/Graph/tests/logarithmical_axis_test.php trunk/Graph/tests/numeric_axis_test.php Modified: trunk/Graph/ChangeLog =================================================================== --- trunk/Graph/ChangeLog 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/ChangeLog 2007-04-12 12:45:42 UTC (rev 4871) @@ -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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Modified: trunk/Graph/src/axis/date.php =================================================================== --- trunk/Graph/src/axis/date.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/src/axis/date.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -454,7 +454,20 @@ */ 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 ) ); + } } /** Modified: trunk/Graph/src/axis/labeled.php =================================================================== --- trunk/Graph/src/axis/labeled.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/src/axis/labeled.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -167,6 +167,21 @@ { $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 ) Modified: trunk/Graph/src/axis/logarithmic.php =================================================================== --- trunk/Graph/src/axis/logarithmic.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/src/axis/logarithmic.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -259,11 +259,28 @@ */ 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'] ) + ); + } } /** Modified: trunk/Graph/src/axis/numeric.php =================================================================== --- trunk/Graph/src/axis/numeric.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/src/axis/numeric.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -348,7 +348,20 @@ */ 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'] ); + } } /** Modified: trunk/Graph/src/element/axis.php =================================================================== --- trunk/Graph/src/element/axis.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/src/element/axis.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -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 @@ $this->properties['labelSize'] = 14; $this->properties['labelMargin'] = 2; $this->properties['maxArrowHeadSize'] = 8; + $this->properties['labelCallback'] = null; parent::__construct( $options ); @@ -186,6 +192,16 @@ 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; Modified: trunk/Graph/tests/date_axis_test.php =================================================================== --- trunk/Graph/tests/date_axis_test.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/tests/date_axis_test.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -461,6 +461,78 @@ ); } + 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'; Modified: trunk/Graph/tests/element_options_test.php =================================================================== --- trunk/Graph/tests/element_options_test.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/tests/element_options_test.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -875,6 +875,42 @@ $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(); Modified: trunk/Graph/tests/labeled_axis_test.php =================================================================== --- trunk/Graph/tests/labeled_axis_test.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/tests/labeled_axis_test.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -575,6 +575,70 @@ $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' + ); + } + } } ?> Modified: trunk/Graph/tests/logarithmical_axis_test.php =================================================================== --- trunk/Graph/tests/logarithmical_axis_test.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/tests/logarithmical_axis_test.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -399,5 +399,72 @@ $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' + ); + } + } } ?> Modified: trunk/Graph/tests/numeric_axis_test.php =================================================================== --- trunk/Graph/tests/numeric_axis_test.php 2007-04-12 12:15:28 UTC (rev 4870) +++ trunk/Graph/tests/numeric_axis_test.php 2007-04-12 12:45:42 UTC (rev 4871) @@ -739,5 +739,69 @@ $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' + ); + } + } } ?> -- svn-components mailing list [EMAIL PROTECTED] http://lists.ez.no/mailman/listinfo/svn-components