Author: Kore Nordmann
Date: 2007-02-26 11:18:00 +0100 (Mon, 26 Feb 2007)
New Revision: 4685

Log:
- Implemented #20276 for centered axis label renderer

Added:
   trunk/Graph/src/structs/step.php
Modified:
   trunk/Graph/src/charts/line.php
   trunk/Graph/src/element/axis.php
   trunk/Graph/src/graph_autoload.php
   trunk/Graph/src/renderer/axis_label_centered.php

Modified: trunk/Graph/src/charts/line.php
===================================================================
--- trunk/Graph/src/charts/line.php     2007-02-26 09:18:11 UTC (rev 4684)
+++ trunk/Graph/src/charts/line.php     2007-02-26 10:18:00 UTC (rev 4685)
@@ -227,6 +227,9 @@
                     }
                     break;
                 case ezcGraph::BAR:
+                    // @TODO:
+                    // - Use getSteps() to determine bar width
+                    // - throw Exception on unregular step width on x axis
                     $barCount = ( 
                         ( count ( $data ) - 1 ) > 
$this->elements['xAxis']->getMajorStepCount() ? 
                         ( $this->elements['xAxis']->getMajorStepCount() + 1 ) 
* ( $this->elements['xAxis']->getMinorStepCount() - 1 ) : 

Modified: trunk/Graph/src/element/axis.php
===================================================================
--- trunk/Graph/src/element/axis.php    2007-02-26 09:18:11 UTC (rev 4684)
+++ trunk/Graph/src/element/axis.php    2007-02-26 10:18:00 UTC (rev 4685)
@@ -243,6 +243,49 @@
     abstract public function getLabel( $step );
 
     /**
+     * Return array of steps on this axis
+     * 
+     * @return array( ezcGraphAxisStep )
+     */
+    public function getSteps()
+    {
+        $majorSteps = $this->getMajorStepCount();
+        $minorStepsPerMajorStepCount = ( $this->getMinorStepCount() / 
$majorSteps );
+
+        $majorStepSize = 1 / $majorSteps;
+        $minorStepSize = ( $minorStepsPerMajorStepCount > 0 ? $majorStepSize / 
$minorStepsPerMajorStepCount : 0 );
+
+        $steps = array();
+        for ( $major = 0; $major <= $majorSteps; ++$major )
+        {
+            $majorStep = new ezcGraphAxisStep(
+                $majorStepSize * $major,
+                $majorStepSize,
+                $this->getLabel( $major ),
+                array(),
+                $this->isZeroStep( $major ),
+                ( $major !== $majorSteps )
+            );
+
+            // Do not add minor steps at major steps positions
+            if ( $minorStepsPerMajorStepCount > 0 )
+            {
+                for( $minor = 1; $minor < $minorStepsPerMajorStepCount; 
++$minor )
+                {
+                    $majorStep->childs[] = new ezcGraphAxisStep(
+                        ( $majorStepSize * $major ) + ( $minorStepSize * 
$minor ),
+                        $minorStepSize
+                    );
+                }
+            }
+
+            $steps[] = $majorStep;
+        }
+
+        return $steps;
+    }
+
+    /**
      * Is zero step
      *
      * Returns true if the given step is the one on the initial axis position

Modified: trunk/Graph/src/graph_autoload.php
===================================================================
--- trunk/Graph/src/graph_autoload.php  2007-02-26 09:18:11 UTC (rev 4684)
+++ trunk/Graph/src/graph_autoload.php  2007-02-26 10:18:00 UTC (rev 4685)
@@ -110,6 +110,7 @@
     'ezcGraphPolynom'                           => 'Graph/math/polynom.php',
     'ezcGraphBoundings'                         => 'Graph/math/boundings.php',
 
+    'ezcGraphAxisStep'                          => 'Graph/structs/step.php',
     'ezcGraphCoordinate'                        => 
'Graph/structs/coordinate.php',
     'ezcGraphContext'                           => 'Graph/structs/context.php',
 );

Modified: trunk/Graph/src/renderer/axis_label_centered.php
===================================================================
--- trunk/Graph/src/renderer/axis_label_centered.php    2007-02-26 09:18:11 UTC 
(rev 4684)
+++ trunk/Graph/src/renderer/axis_label_centered.php    2007-02-26 10:18:00 UTC 
(rev 4685)
@@ -84,34 +84,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
         );
+        $direction->unify();
         
-        $length = sqrt( pow( $direction->x, 2) + pow( $direction->y, 2 ) );
-        $direction->x /= $length;
-        $direction->y /= $length;
-        
-        // 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;
@@ -126,107 +112,156 @@
             );
         }
 
-        // Determine size of labels
-        switch ( $axis->position )
+        // Draw steps and grid
+        foreach ( $steps as $nr => $step )
         {
-            case ezcGraph::RIGHT:
-            case ezcGraph::LEFT:
-                $labelWidth = min( 
-                    abs( $majorStep->x ),
-                    ( $boundings->x1 - $boundings->x0 ) * $axis->axisSpace * 2
-                );
-                $labelHeight = ( $boundings->y1 - $boundings->y0 ) * 
$axis->axisSpace;
-                break;
-            case ezcGraph::BOTTOM:
-            case ezcGraph::TOP:
-                $labelWidth = ( $boundings->x1 - $boundings->x0 ) * 
$axis->axisSpace;
-                $labelHeight = min( 
-                    abs( $majorStep->y ),
-                    ( $boundings->y1 - $boundings->y0 ) * $axis->axisSpace * 2
-                );
-                break;
-        }
+            $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
+            );
 
-        // Draw steps and grid
-        $step = 0;
-        while ( $step <= $this->majorStepCount )
-        {
-            if ( ! $axis->isZeroStep( $step ) )
+            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
-            if ( $this->showZeroValue || ! $axis->isZeroStep( $step ) )
+            if ( $this->showZeroValue || ! $step->isZero )
             {
-                $label = $axis->getLabel( $step );
-                switch ( $axis->position )
+                // Calculate label boundings
+                if ( abs( $direction->x ) > abs( $direction->y ) )
                 {
-                    case ezcGraph::TOP:
-                    case ezcGraph::BOTTOM:
-                        $renderer->drawText(
-                            new ezcGraphBoundings(
-                                $start->x - $labelWidth + $this->labelPadding,
-                                $start->y - $labelHeight / 2 + 
$this->labelPadding,
-                                $start->x - $this->labelPadding,
-                                $start->y + $labelHeight / 2 - 
$this->labelPadding
-                            ),
-                            $label,
-                            ezcGraph::MIDDLE | ezcGraph::RIGHT
-                        );
-                        break;
-                    case ezcGraph::LEFT:
-                    case ezcGraph::RIGHT:
-                        $renderer->drawText(
-                            new ezcGraphBoundings(
-                                $start->x - $labelWidth / 2 + 
$this->labelPadding,
-                                $start->y + $this->labelPadding,
-                                $start->x + $labelWidth / 2 - 
$this->labelPadding,
-                                $start->y + $labelHeight - $this->labelPadding
-                            ),
-                            $label,
-                            ezcGraph::CENTER | ezcGraph::TOP
-                        );
-                        break;
-                }
-            }
+                    // Horizontal labels
+                    switch ( true )
+                    {
+                        case ( $nr === 0 ):
+                            // First label
+                            $labelSize = min(
+                                $renderer->xAxisSpace * 2,
+                                $step->width * $axisBoundings->width
+                            );
+                            break;
+                        case ( $step->isLast ):
+                            // Last label
+                            $labelSize = min(
+                                $renderer->xAxisSpace * 2,
+                                $steps[$nr - 1]->width * $axisBoundings->width
+                            );
+                            break;
+                        default:
+                            $labelSize = min(
+                                $step->width * $axisBoundings->width,
+                                $steps[$nr - 1]->width * $axisBoundings->width
+                            );
+                            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
-                );
+                    $labelBoundings = new ezcGraphBoundings(
+                        $position->x - $labelSize / 2 + $this->labelPadding,
+                        $position->y + $this->labelPadding,
+                        $position->x + $labelSize / 2 - $this->labelPadding,
+                        $position->y + $renderer->yAxisSpace - 
$this->labelPadding
+                    );
 
-                while ( $minorStepNmbr++ < $minorStepsPerMajorStepCount )
+                    $alignement = ezcGraph::CENTER | ezcGraph::TOP;
+                }
+                else
                 {
-                    // minor grid
-                    if ( $axis->minorGrid )
+                    // Vertical labels
+                    switch ( true )
                     {
-                        $this->drawGrid( $renderer, $gridBoundings, 
$minorGridPosition, $majorStep, $axis->minorGrid );
+                        case ( $nr === 0 ):
+                            // First label
+                            $labelSize = min(
+                                $renderer->yAxisSpace * 2,
+                                $step->width * $axisBoundings->height
+                            );
+                            break;
+                        case ( $step->isLast ):
+                            // Last label
+                            $labelSize = min(
+                                $renderer->yAxisSpace * 2,
+                                $steps[$nr - 1]->width * $axisBoundings->height
+                            );
+                            break;
+                        default:
+                            $labelSize = min(
+                                $step->width * $axisBoundings->height,
+                                $steps[$nr - 1]->width * $axisBoundings->height
+                            );
+                            break;
                     }
 
-                    // minor step
-                    $this->drawStep( $renderer, $minorGridPosition, 
$direction, $axis->position, $this->minorStepSize, $axis->border );
+                    $labelBoundings = new ezcGraphBoundings(
+                        $position->x - $renderer->xAxisSpace + 
$this->labelPadding,
+                        $position->y - $labelSize / 2 + $this->labelPadding,
+                        $position->x - $this->labelPadding,
+                        $position->y + $labelSize / 2 - $this->labelPadding
+                    );
 
-                    $minorGridPosition->x += $minorStep->x;
-                    $minorGridPosition->y += $minorStep->y;
+                    $alignement = ezcGraph::MIDDLE | ezcGraph::RIGHT;
                 }
+
+                $renderer->drawText( $labelBoundings, $step->label, 
$alignement );
             }
 
-            $start->x += $majorStep->x;
-            $start->y += $majorStep->y;
-            ++$step;
+            // Iterate over minor steps
+            foreach ( $step->childs as $minorStep )
+            {
+                $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, 
+                        $minorStepPosition,
+                        $minorStepSize,
+                        $axis->minorGrid
+                    );
+                }
+                
+                // major step
+                $this->drawStep( 
+                    $renderer, 
+                    $minorStepPosition,
+                    $direction, 
+                    $axis->position, 
+                    $this->minorStepSize, 
+                    $axis->border
+                );
+            }
         }
     }
 }

Added: trunk/Graph/src/structs/step.php
===================================================================
--- trunk/Graph/src/structs/step.php    2007-02-26 09:18:11 UTC (rev 4684)
+++ trunk/Graph/src/structs/step.php    2007-02-26 10:18:00 UTC (rev 4685)
@@ -0,0 +1,96 @@
+<?php
+/**
+ * File containing the ezcGraphAxisStep struct
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Represents a single step on the axis
+ *
+ * @package Graph
+ */
+class ezcGraphAxisStep
+{
+    /**
+     * Position of step on one axis.
+     * 
+     * @var float
+     */
+    public $position = 0;
+
+    /**
+     * Size of step
+     * 
+     * @var float
+     */
+    public $width = 0;
+
+    /**
+     * Steps label 
+     * 
+     * @var string
+     */
+    public $label = false;
+
+    /**
+     * Childrens of step 
+     * 
+     * @var array( ezcGraphAxisStep )
+     */
+    public $childs = array();
+
+    /**
+     * True if the step is at the same position as the other axis
+     * 
+     * @var bool
+     */
+    public $isZero = false;
+
+    /**
+     * True if this step is the last one
+     * 
+     * @var bool
+     */
+    public $isLast = false;
+
+    /**
+     * Simple constructor
+     *
+     * @param float $position 
+     * @param float $width 
+     * @param string $label 
+     * @param array $childs 
+     * @ignore
+     */
+    public function __construct( $position = .0, $width = .0, $label = false, 
array $childs = array(), $isZero = false, $isLast = false )
+    {
+        $this->position = (float) $position;
+        $this->width = (float) $width;
+        $this->label = $label;
+        $this->childs = $childs;
+        $this->isZero = (bool) $isZero;
+        $this->isLast = (bool) $isLast;
+    }
+
+    /**
+     * __set_state 
+     * 
+     * @param array $properties Struct properties
+     * @return void
+     * @ignore
+     */
+    public function __set_state( array $properties )
+    {
+        $this->position = $properties['position'];
+        $this->width = $properties['width'];
+        $this->label = $properties['label'];
+        $this->childs = $properties['childs'];
+        $this->isZero = $properties['isZero'];
+        $this->isLast = $properties['isLast'];
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/structs/step.php
___________________________________________________________________
Name: svn:eol-style
   + native

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

Reply via email to