Author: Kore Nordmann
Date: 2007-02-26 15:20:12 +0100 (Mon, 26 Feb 2007)
New Revision: 4687

Log:
 - Implemented #20276 for exact axis label renderer

Modified:
   trunk/Graph/src/element/axis.php
   trunk/Graph/src/renderer/axis_label_exact.php
   
trunk/Graph/tests/data/compare/ezcGraphRenderer2dTest_testRenderLineChartWithDifferentAxisSpace.svg
   
trunk/Graph/tests/data/compare/ezcGraphRenderer3dTest_testRenderLineChartWithDifferentAxisSpace.svg
   trunk/Graph/tests/numeric_axis_test.php

Modified: trunk/Graph/src/element/axis.php
===================================================================
--- trunk/Graph/src/element/axis.php    2007-02-26 12:05:42 UTC (rev 4686)
+++ trunk/Graph/src/element/axis.php    2007-02-26 14:20:12 UTC (rev 4687)
@@ -264,7 +264,7 @@
                 $this->getLabel( $major ),
                 array(),
                 $this->isZeroStep( $major ),
-                ( $major !== $majorSteps )
+                ( $major === $majorSteps )
             );
 
             // Do not add minor steps at major steps positions

Modified: trunk/Graph/src/renderer/axis_label_exact.php
===================================================================
--- trunk/Graph/src/renderer/axis_label_exact.php       2007-02-26 12:05:42 UTC 
(rev 4686)
+++ trunk/Graph/src/renderer/axis_label_exact.php       2007-02-26 14:20:12 UTC 
(rev 4687)
@@ -85,33 +85,20 @@
         ezcGraphChartElementAxis $axis )
     {
         // receive rendering parameters from axis
-        $this->majorStepCount = $axis->getMajorStepCount();
-        $this->minorStepCount = $axis->getMinorStepCount();
-        $minorStepsPerMajorStepCount = $this->minorStepCount / 
$this->majorStepCount - 1;
+        $steps = $axis->getSteps();
 
+        $axisBoundings = new ezcGraphBoundings(
+            $start->x, $start->y,
+            $end->x, $end->y
+        );
+
         // Determine normalized axis direction
-        $direction = new ezcGraphCoordinate(
+        $direction = new ezcGraphVector(
             $start->x - $end->x,
             $start->y - $end->y
         );
-        $length = sqrt( pow( $direction->x, 2) + pow( $direction->y, 2 ) );
-        $direction->x /= $length;
-        $direction->y /= $length;
+        $direction->unify();
         
-        // Calculate stepsizes for mjor and minor steps
-        $majorStep = new ezcGraphCoordinate(
-            ( $end->x - $start->x ) / $this->majorStepCount,
-            ( $end->y - $start->y ) / $this->majorStepCount
-        );
-
-        if ( $this->minorStepCount )
-        {
-            $minorStep = new ezcGraphCoordinate(
-                ( $end->x - $start->x ) / $this->minorStepCount,
-                ( $end->y - $start->y ) / $this->minorStepCount
-            );
-        }
-
         if ( $this->outerGrid )
         {
             $gridBoundings = $boundings;
@@ -119,153 +106,161 @@
         else
         {
             $gridBoundings = new ezcGraphBoundings(
-                $boundings->x0 + $boundings->width * $axis->axisSpace,
-                $boundings->y0 + $boundings->height * $axis->axisSpace,
-                $boundings->x1 - $boundings->width * $axis->axisSpace,
-                $boundings->y1 - $boundings->height * $axis->axisSpace
+                $boundings->x0 + $renderer->xAxisSpace,
+                $boundings->y0 + $renderer->yAxisSpace,
+                $boundings->x1 - $renderer->xAxisSpace,
+                $boundings->y1 - $renderer->yAxisSpace
             );
         }
 
-        // Determine size of labels
-        $xPaddingDirection = 1;
-        $yPaddingDirection = 1;
-        switch ( $axis->position )
-        {
-            case ezcGraph::RIGHT:
-                $xPaddingDirection = -1;
-            case ezcGraph::LEFT:
-                if ( $this->showLastValue )
-                {
-                    $labelWidth = $majorStep->x / 2;
-                }
-                else
-                {
-                    $labelWidth = $majorStep->x;
-                }
-                $labelHeight = $boundings->height * $axis->axisSpace;
-                break;
-            case ezcGraph::BOTTOM:
-                $yPaddingDirection = -1;
-            case ezcGraph::TOP:
-                if ( $this->showLastValue )
-                {
-                    $labelHeight = $majorStep->y / 2;
-                }
-                else
-                {
-                    $labelHeight = $majorStep->y;
-                }
-                $labelWidth = $boundings->width * $axis->axisSpace;
-                break;
-        }
-
         // Draw steps and grid
-        $step = 0;
-        while ( $step <= $this->majorStepCount )
+        foreach ( $steps as $nr => $step )
         {
-            if ( ! $axis->isZeroStep( $step ) )
+            $position = new ezcGraphCoordinate(
+                $start->x + ( $end->x - $start->x ) * $step->position,
+                $start->y + ( $end->y - $start->y ) * $step->position
+            );
+            $stepSize = new ezcGraphCoordinate(
+                $axisBoundings->width * $step->width,
+                $axisBoundings->height * $step->width
+            );
+
+            if ( ! $step->isZero )
             {
                 // major grid
                 if ( $axis->majorGrid )
                 {
-                    $this->drawGrid( $renderer, $gridBoundings, $start, 
$majorStep, $axis->majorGrid );
+                    $this->drawGrid( 
+                        $renderer, 
+                        $gridBoundings, 
+                        $position,
+                        $stepSize,
+                        $axis->majorGrid
+                    );
                 }
                 
                 // major step
-                $this->drawStep( $renderer, $start, $direction, 
$axis->position, $this->majorStepSize, $axis->border );
+                $this->drawStep( 
+                    $renderer, 
+                    $position,
+                    $direction, 
+                    $axis->position, 
+                    $this->majorStepSize, 
+                    $axis->border
+                );
             }
 
-            // draw label
-            $label = $axis->getLabel( $step );
+            switch ( $axis->position )
+            {
+                case ezcGraph::RIGHT:
+                case ezcGraph::LEFT:
+                    $labelWidth = $axisBoundings->width * 
+                        $steps[$nr - $step->isLast]->width /
+                        ( $this->showLastValue + 1 );
+                    $labelHeight = $renderer->yAxisSpace;
+                    break;
+
+                case ezcGraph::BOTTOM:
+                case ezcGraph::TOP:
+                    $labelWidth = $renderer->xAxisSpace;
+                    $labelHeight = $axisBoundings->height * 
+                        $steps[$nr - $step->isLast]->width /
+                        ( $this->showLastValue + 1 );
+                    break;
+            }
+
             switch ( true )
             {
-                case ( !$this->showLastValue && 
-                       ( $step == $this->majorStepCount ) ):
+                case ( !$this->showLastValue && $step->isLast ):
                     // Skip last step if showLastValue is false
                     break;
                 // Draw label at top left of step
                 case ( ( $axis->position === ezcGraph::BOTTOM ) &&
-                       ( $step < $this->majorStepCount ) ) ||
+                       ( !$step->isLast ) ) ||
                      ( ( $axis->position === ezcGraph::TOP ) &&
-                       ( $step == $this->majorStepCount ) ):
-                    $renderer->drawText(
-                        new ezcGraphBoundings(
-                            $start->x - $labelWidth + $this->labelPadding,
-                            $start->y - $labelHeight * $yPaddingDirection + 
$this->labelPadding,
-                            $start->x - $this->labelPadding,
-                            $start->y - $this->labelPadding
-                        ),
-                        $label,
-                        ezcGraph::RIGHT | ezcGraph::BOTTOM
+                       ( $step->isLast ) ):
+                    $labelBoundings = new ezcGraphBoundings(
+                        $position->x - $labelWidth + $this->labelPadding,
+                        $position->y - $labelHeight + $this->labelPadding,
+                        $position->x - $this->labelPadding,
+                        $position->y - $this->labelPadding
                     );
-                break;
+                    $alignement = ezcGraph::RIGHT | ezcGraph::BOTTOM;
+                    break;
                 // Draw label at bottom right of step
                 case ( ( $axis->position === ezcGraph::LEFT ) &&
-                       ( $step < $this->majorStepCount ) ) ||
+                       ( !$step->isLast ) ) ||
                      ( ( $axis->position === ezcGraph::RIGHT ) &&
-                       ( $step == $this->majorStepCount ) ):
-                    $renderer->drawText(
-                        new ezcGraphBoundings(
-                            $start->x + $this->labelPadding,
-                            $start->y + $this->labelPadding,
-                            $start->x + $labelWidth * $xPaddingDirection - 
$this->labelPadding,
-                            $start->y + $labelHeight - $this->labelPadding
-                        ),
-                        $label,
-                        ezcGraph::LEFT | ezcGraph::TOP
+                       ( $step->isLast ) ):
+                    $labelBoundings = new ezcGraphBoundings(
+                        $position->x + $this->labelPadding,
+                        $position->y + $this->labelPadding,
+                        $position->x + $labelWidth - $this->labelPadding,
+                        $position->y + $labelHeight - $this->labelPadding
                     );
-                break;
+                    $alignement = ezcGraph::LEFT | ezcGraph::TOP;
+                    break;
                 // Draw label at bottom left of step
                 case ( ( $axis->position === ezcGraph::TOP ) &&
-                       ( $step < $this->majorStepCount ) ) ||
+                       ( !$step->isLast ) ) ||
                      ( ( $axis->position === ezcGraph::RIGHT ) &&
-                       ( $step < $this->majorStepCount ) ) ||
+                       ( !$step->isLast ) ) ||
                      ( ( $axis->position === ezcGraph::BOTTOM ) &&
-                       ( $step == $this->majorStepCount ) ) ||
+                       ( $step->isLast ) ) ||
                      ( ( $axis->position === ezcGraph::LEFT ) &&
-                       +( $step == $this->majorStepCount ) ):
-                    $renderer->drawText(
-                        new ezcGraphBoundings(
-                            $start->x - $labelWidth * $xPaddingDirection + 
$this->labelPadding,
-                            $start->y + $this->labelPadding,
-                            $start->x - $this->labelPadding,
-                            $start->y + $labelHeight * $yPaddingDirection - 
$this->labelPadding
-                        ),
-                        $label,
-                        ezcGraph::RIGHT | ezcGraph::TOP
+                       ( $step->isLast ) ):
+                    $labelBoundings = new ezcGraphBoundings(
+                        $position->x - $labelWidth + $this->labelPadding,
+                        $position->y + $this->labelPadding,
+                        $position->x - $this->labelPadding,
+                        $position->y + $labelHeight - $this->labelPadding
                     );
-                break;
+                    $alignement = ezcGraph::RIGHT | ezcGraph::TOP;
+                    break;
             }
 
-            // second iteration for minor steps, if wanted
-            $minorStepNmbr = 0;
-            if ( $this->minorStepCount &&
-                 ( $step < $this->majorStepCount ) )
-            {
-                $minorGridPosition = new ezcGraphCoordinate(
-                    $start->x + $minorStep->x,
-                    $start->y + $minorStep->y
-                );
+            $renderer->drawText(
+                $labelBoundings,
+                $step->label,
+                $alignement
+            );
 
-                while ( $minorStepNmbr++ < $minorStepsPerMajorStepCount )
+            if ( !$step->isLast )
+            {
+                // Iterate over minor steps
+                foreach ( $step->childs as $minorStep )
                 {
-                    // minor grid
+                    $minorStepPosition = new ezcGraphCoordinate(
+                        $start->x + ( $end->x - $start->x ) * 
$minorStep->position,
+                        $start->y + ( $end->y - $start->y ) * 
$minorStep->position
+                    );
+                    $minorStepSize = new ezcGraphCoordinate(
+                        $axisBoundings->width * $minorStep->width,
+                        $axisBoundings->height * $minorStep->width
+                    );
+
                     if ( $axis->minorGrid )
                     {
-                        $this->drawGrid( $renderer, $gridBoundings, 
$minorGridPosition, $majorStep, $axis->minorGrid );
+                        $this->drawGrid( 
+                            $renderer, 
+                            $gridBoundings, 
+                            $minorStepPosition,
+                            $minorStepSize,
+                            $axis->minorGrid
+                        );
                     }
-
-                    // minor step
-                    $this->drawStep( $renderer, $minorGridPosition, 
$direction, $axis->position, $this->minorStepSize, $axis->border );
-
-                    $minorGridPosition->x += $minorStep->x;
-                    $minorGridPosition->y += $minorStep->y;
+                    
+                    // major step
+                    $this->drawStep( 
+                        $renderer, 
+                        $minorStepPosition,
+                        $direction, 
+                        $axis->position, 
+                        $this->minorStepSize, 
+                        $axis->border
+                    );
                 }
             }
-
-            $start->x += $majorStep->x;
-            $start->y += $majorStep->y;
-            ++$step;
         }
     }
 }

Modified: 
trunk/Graph/tests/data/compare/ezcGraphRenderer2dTest_testRenderLineChartWithDifferentAxisSpace.svg
===================================================================
(Binary files differ)

Modified: 
trunk/Graph/tests/data/compare/ezcGraphRenderer3dTest_testRenderLineChartWithDifferentAxisSpace.svg
===================================================================
(Binary files differ)

Modified: trunk/Graph/tests/numeric_axis_test.php
===================================================================
--- trunk/Graph/tests/numeric_axis_test.php     2007-02-26 12:05:42 UTC (rev 
4686)
+++ trunk/Graph/tests/numeric_axis_test.php     2007-02-26 14:20:12 UTC (rev 
4687)
@@ -31,6 +31,49 @@
         );
     }
 
+    public function testGetSteps()
+    {
+        $chart = new ezcGraphLineChart();
+        $chart->data['sample'] = new ezcGraphArrayDataSet( array( 2000 => 70, 
12, 39, 87 ) );
+        $chart->render( 500, 200 );
+
+        $steps = $chart->yAxis->getSteps();
+
+        $position = 0.;
+        $label = 0.;
+        foreach ( $steps as $nr => $step )
+        {
+            $this->assertEquals(
+                $step->position,
+                $position,
+                "[$nr] Step position wrong.",
+                .00001
+            );
+            $position += .25;
+
+            $this->assertEquals(
+                $step->width,
+                .25,
+                "[$nr] Step width wrong.",
+                .00001
+            );
+
+            $this->assertEquals(
+                $step->label,
+                $label,
+                "[$nr] Step label wrong.",
+                .00001
+            );
+            $label += 25;
+
+            $this->assertSame(
+                count( $step->childs ),
+                4,
+                "[$nr] Step child count wrong."
+            );
+        }
+    }
+
     public function testManualScaling()
     {
         $chart = new ezcGraphLineChart();

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to