diff --git a/src/element/axis.php b/src/element/axis.php index e9a65387..0ade916c 100644 --- a/src/element/axis.php +++ b/src/element/axis.php @@ -119,6 +119,8 @@ * Size of axis label * @property int $labelMargin * Distance between label an axis + * @property int $labelPosition + * Integer defining the labels position regarding the axe. * @property int $minArrowHeadSize * Minimum Size used to draw arrow heads. * @property int $maxArrowHeadSize @@ -179,6 +181,7 @@ public function __construct( array $options = array() ) $this->properties['label'] = false; $this->properties['labelSize'] = 14; $this->properties['labelMargin'] = 2; + $this->properties['labelPosition'] = ezcGraph::LEFT; $this->properties['minArrowHeadSize'] = 4; $this->properties['maxArrowHeadSize'] = 8; $this->properties['labelCallback'] = null; @@ -296,6 +299,21 @@ public function __set( $propertyName, $propertyValue ) $this->properties['labelMargin'] = (int) $propertyValue; break; + case 'labelPosition': + $positions = array( + ezcGraph::LEFT, + ezcGraph::RIGHT, + ); + + if ( in_array( $propertyValue, $positions, true ) ) + { + $this->properties['labelPosition'] = $propertyValue; + } + else + { + throw new ezcBaseValueException( 'labelPosition', $propertyValue, 'integer' ); + } + break; case 'maxArrowHeadSize': if ( !is_numeric( $propertyValue ) || ( $propertyValue <= 0 ) ) diff --git a/src/renderer/axis_label_exact.php b/src/renderer/axis_label_exact.php index a158756e..a07fd5de 100644 --- a/src/renderer/axis_label_exact.php +++ b/src/renderer/axis_label_exact.php @@ -207,6 +207,30 @@ public function renderLabels( // Skip last step if showLastValue is false $showLabel = false; break; + case ( ( $axis->labelPosition === ezcGraph::RIGHT ) && + ( !$step->isLast ) ) || + ( ( $axis->labelPosition === ezcGraph::RIGHT ) && + ( $step->isLast ) && + ( $this->renderLastOutside ) ) : + $labelBoundings = new ezcGraphBoundings( + $position->x + $this->labelPadding, + $position->y - $labelHeight + $this->labelPadding, + $position->x + $labelWidth - $this->labelPadding, + $position->y - $this->labelPadding + ); + $alignement = ezcGraph::LEFT | ezcGraph::BOTTOM; + break; + case ( ( $axis->labelPosition === ezcGraph::RIGHT ) && + ( $step->isLast ) && + ( !$this->renderLastOutside ) ) : + $labelBoundings = new ezcGraphBoundings( + $position->x + $this->labelPadding, + $position->y + $this->labelPadding, + $position->x + $labelWidth - $this->labelPadding, + $position->y + $labelHeight - $this->labelPadding + ); + $alignement = ezcGraph::TOP | ezcGraph::LEFT; + break; // Draw label at top left of step case ( ( $axis->position === ezcGraph::BOTTOM ) && ( !$step->isLast ) ) || diff --git a/tests/axis_exact_renderer_test.php b/tests/axis_exact_renderer_test.php index dc384290..7a9de51d 100644 --- a/tests/axis_exact_renderer_test.php +++ b/tests/axis_exact_renderer_test.php @@ -390,6 +390,49 @@ public function testRenderTextBoxes() $chart->render( 500, 200 ); } + public function testRenderTextBoxesWithLabelPositionRight() + { + $chart = new ezcGraphLineChart(); + $chart->palette = new ezcGraphPaletteBlack(); + $chart->xAxis->axisLabelRenderer = new ezcGraphAxisExactLabelRenderer(); + $chart->xAxis->labelPosition = ezcGraph::RIGHT; + $chart->yAxis->axisLabelRenderer = new ezcGraphAxisNoLabelRenderer(); + $chart->data['sampleData'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1) ); + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2d', array( + 'drawText', + ) ); + + $mockedRenderer + ->expects( $this->at( 0 ) ) + ->method( 'drawText' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 142., 162., 178., 178. ), 1. ), + $this->equalTo( 'sample 1' ), + $this->equalTo( ezcGraph::BOTTOM | ezcGraph::LEFT ) + ); + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawText' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 222., 162., 258., 178. ), 1. ), + $this->equalTo( 'sample 2' ), + $this->equalTo( ezcGraph::BOTTOM | ezcGraph::LEFT ) + ); + $mockedRenderer + ->expects( $this->at( 4 ) ) + ->method( 'drawText' ) + ->with( + $this->equalTo( new ezcGraphBoundings( 462., 182., 498., 198. ), 1. ), + $this->equalTo( 'sample 5' ), + $this->equalTo( ezcGraph::TOP | ezcGraph::LEFT ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + public function testRenderTextBoxesWithoutLastLabel() { $chart = new ezcGraphLineChart(); diff --git a/tests/element_options_test.php b/tests/element_options_test.php index cd9856f8..353358ae 100644 --- a/tests/element_options_test.php +++ b/tests/element_options_test.php @@ -652,6 +652,34 @@ public function testChartElementAxisPropertyAxisSpace() $this->fail( 'Expected ezcBaseValueException.' ); } + public function testChartElementAxisPropertyLabelPosition() + { + $options = new ezcGraphChartElementNumericAxis(); + + $this->assertSame( + ezcGraph::LEFT, + $options->labelPosition, + 'Wrong default value for property labelPosition in class ezcGraphChartElementNumericAxis' + ); + + $options->labelPosition = ezcGraph::RIGHT; + + $this->assertSame( + ezcGraph::RIGHT, + $options->labelPosition, + 'Setting property value did not work for property labelPosition in class ezcGraphChartElementNumericAxis' + ); + + try + { + $options->labelPosition = ezcGraph::TOP; + } + catch ( ezcBaseValueException $e ) + { + return true; + } + } + /* Disabled for now. public function testChartElementAxisPropertyOuterAxisSpace() {