diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-02-26 10:18:00 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-02-26 10:18:00 +0000 |
commit | 15e0162c83faa211ea4e4e6cae7faf70e10e9a9c (patch) | |
tree | 972999d0443918acf78454610bb8cd9c32955753 | |
parent | e9d7bc2afe91b3aa6633cfc987ffe29b0206fa89 (diff) | |
download | zetacomponents-graph-15e0162c83faa211ea4e4e6cae7faf70e10e9a9c.zip zetacomponents-graph-15e0162c83faa211ea4e4e6cae7faf70e10e9a9c.tar.gz |
- Implemented #20276 for centered axis label renderer
-rw-r--r-- | src/charts/line.php | 3 | ||||
-rw-r--r-- | src/element/axis.php | 43 | ||||
-rw-r--r-- | src/graph_autoload.php | 1 | ||||
-rw-r--r-- | src/renderer/axis_label_centered.php | 235 | ||||
-rw-r--r-- | src/structs/step.php | 96 |
5 files changed, 278 insertions, 100 deletions
diff --git a/src/charts/line.php b/src/charts/line.php index a0ec609..0f5aa42 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -227,6 +227,9 @@ class ezcGraphLineChart extends ezcGraphChart } 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 ) : diff --git a/src/element/axis.php b/src/element/axis.php index 2dd9202..f355fcf 100644 --- a/src/element/axis.php +++ b/src/element/axis.php @@ -243,6 +243,49 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement 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 diff --git a/src/graph_autoload.php b/src/graph_autoload.php index b97f609..757df48 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -110,6 +110,7 @@ return array( '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', ); diff --git a/src/renderer/axis_label_centered.php b/src/renderer/axis_label_centered.php index 6b5ec02..3eaa18c 100644 --- a/src/renderer/axis_label_centered.php +++ b/src/renderer/axis_label_centered.php @@ -84,34 +84,20 @@ class ezcGraphAxisCenteredLabelRenderer extends ezcGraphAxisLabelRenderer 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 @@ class ezcGraphAxisCenteredLabelRenderer extends ezcGraphAxisLabelRenderer ); } - // Determine size of labels - switch ( $axis->position ) - { - 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; - } - // 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 - 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 + ); + } } } } diff --git a/src/structs/step.php b/src/structs/step.php new file mode 100644 index 0000000..7dcb593 --- /dev/null +++ b/src/structs/step.php @@ -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']; + } +} + +?> |