diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-06-06 08:49:03 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-06-06 08:49:03 +0000 |
commit | 8e4f782905f165b9834bff2e95d07884c1fa5bd6 (patch) | |
tree | 7ab180f36768b39a81235ba8883e38daf91c8806 /src/axis | |
parent | 792f2f514e83efc794dd9cec3c07534c651b2a21 (diff) | |
download | zetacomponents-graph-8e4f782905f165b9834bff2e95d07884c1fa5bd6.zip zetacomponents-graph-8e4f782905f165b9834bff2e95d07884c1fa5bd6.tar.gz |
- Fixed axis to use axis type based labeling methods, which results in better
labeling
Diffstat (limited to 'src/axis')
-rw-r--r-- | src/axis/labeled.php | 138 | ||||
-rw-r--r-- | src/axis/numeric.php | 147 |
2 files changed, 285 insertions, 0 deletions
diff --git a/src/axis/labeled.php b/src/axis/labeled.php index d679855..8525460 100644 --- a/src/axis/labeled.php +++ b/src/axis/labeled.php @@ -152,6 +152,144 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis return count( $this->labels ) - 1; } + /** + * Draw labels for an axis + * + * @param ezcGraphRenderer $renderer + * @param ezcGraphCoordinate $start + * @param ezcGraphCoordinate $end + * @param ezcGraphBoundings $boundings + * @return void + */ + protected function drawLabels( ezcGraphRenderer $renderer, ezcGraphCoordinate $start, ezcGraphCoordinate $end, ezcGraphBoundings $boundings ) + { + // Draw major steps + $steps = $this->getMajorStepCount() + 1; + + // Calculate stepsize + $xStepsize = ( $end->x - $start->x ) / $steps; + $yStepsize = ( $end->y - $start->y ) / $steps; + + // Caluclate datafree chart border + $xBorder = abs ( ( $boundings->x1 - $boundings->x0 ) * ( $this->padding / 2 ) ); + $yBorder = abs ( ( $boundings->y1 - $boundings->y0 ) * ( $this->padding / 2 ) ); + + for ( $i = 0; $i <= $steps; ++$i ) + { + $label = $this->getLabel( $i ); + + switch ( $this->position ) + { + case ezcGraph::LEFT: + if ( $i === 0 ) + { + $align = ezcGraph::LEFT; + } + elseif ( $i >= ( $steps - 1 ) ) + { + $align = ezcGraph::RIGHT; + } + else + { + $align = ezcGraph::CENTER; + } + + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $this->labelPadding ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( $xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + $align | ezcGraph::TOP + ); + break; + case ezcGraph::RIGHT: + if ( $i === 0 ) + { + $align = ezcGraph::RIGHT; + } + elseif ( $i >= ( $steps - 1 ) ) + { + $align = ezcGraph::LEFT; + } + else + { + $align = ezcGraph::CENTER; + } + + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $xStepsize ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( -$xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + $align | ezcGraph::TOP + ); + break; + case ezcGraph::BOTTOM: + if ( $i === 0 ) + { + $align = ezcGraph::BOTTOM; + } + elseif ( $i >= ( $steps - 1 ) ) + { + $align = ezcGraph::TOP; + } + else + { + $align = ezcGraph::MIDDLE; + } + + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $yStepsize ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( -$yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | $align + ); + break; + case ezcGraph::TOP: + if ( $i === 0 ) + { + $align = ezcGraph::TOP; + } + elseif ( $i >= ( $steps - 1 ) ) + { + $align = ezcGraph::BOTTOM; + } + else + { + $align = ezcGraph::MIDDLE; + } + + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( $yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | $align + ); + break; + } + } + } + + /** + * Get label for a dedicated step on the axis + * + * @param integer $step Number of step + * @return string label + */ protected function getLabel( $step ) { if ( isset( $this->labels[$step] ) ) diff --git a/src/axis/numeric.php b/src/axis/numeric.php index c92ebb0..161130c 100644 --- a/src/axis/numeric.php +++ b/src/axis/numeric.php @@ -290,6 +290,153 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis return (int) ( ( $this->max - $this->min ) / $this->majorStep ); } + /** + * Draw labels for an axis + * + * @param ezcGraphRenderer $renderer + * @param ezcGraphCoordinate $start + * @param ezcGraphCoordinate $end + * @param ezcGraphBoundings $boundings + * @return void + */ + protected function drawLabels( ezcGraphRenderer $renderer, ezcGraphCoordinate $start, ezcGraphCoordinate $end, ezcGraphBoundings $boundings ) + { + // Draw major steps + $steps = $this->getMajorStepCount(); + + // Calculate stepsize + $xStepsize = ( $end->x - $start->x ) / $steps; + $yStepsize = ( $end->y - $start->y ) / $steps; + + // Caluclate datafree chart border + $xBorder = abs ( ( $boundings->x1 - $boundings->x0 ) * ( $this->padding / 2 ) ); + $yBorder = abs ( ( $boundings->y1 - $boundings->y0 ) * ( $this->padding / 2 ) ); + + for ( $i = 0; $i <= $steps; ++$i ) + { + // Draw label + if ( $i < $steps ) + { + $label = $this->getLabel( $i ); + + switch ( $this->position ) + { + case ezcGraph::LEFT: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $this->labelPadding ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( $xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + ezcGraph::LEFT | ezcGraph::TOP + ); + break; + case ezcGraph::RIGHT: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $xStepsize ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( -$xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::TOP + ); + break; + case ezcGraph::BOTTOM: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $yStepsize ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( -$yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::BOTTOM + ); + break; + case ezcGraph::TOP: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( $yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::TOP + ); + break; + } + } + else + { + $label = $this->getLabel( $i-- ); + + switch ( $this->position ) + { + case ezcGraph::LEFT: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $this->labelPadding ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( $xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::TOP + ); + break; + case ezcGraph::RIGHT: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + $xStepsize ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + (int) round( -$xStepsize ) - $this->labelPadding, + $yBorder - $this->labelPadding, + ezcGraph::LEFT | ezcGraph::TOP + ); + break; + case ezcGraph::BOTTOM: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $yStepsize + $this->labelPadding ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( -$yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::TOP + ); + break; + case ezcGraph::TOP: + $renderer->drawTextBox( + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize - $xBorder ), + (int) round( $start->y + $i * $yStepsize + $this->labelPadding ) + ), + $label, + $xBorder - $this->labelPadding, + (int) round( $yStepsize ) - $this->labelPadding, + ezcGraph::RIGHT | ezcGraph::BOTTOM + ); + break; + } + ++$i; + } + } + } + + /** + * Get label for a dedicated step on the axis + * + * @param integer $step Number of step + * @return string label + */ protected function getLabel( $step ) { return $this->min + ( $step * $this->majorStep ); |