From 182ce919974d9ec9a6de098a41c02e2965beaba3 Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Thu, 8 Jun 2006 10:17:39 +0000 Subject: - Refactored axis API to be able to use each axis type for both, X- and Y-axis --- src/axis/labeled.php | 53 +++++++++++++++++++++++++------------------ src/axis/numeric.php | 64 ++++++++++++++++++++++++++++++++-------------------- src/charts/line.php | 20 ++++++++++++---- src/element/axis.php | 49 +++++++++++++++++++++++++++------------- 4 files changed, 120 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/axis/labeled.php b/src/axis/labeled.php index 341e36b..b6e0960 100644 --- a/src/axis/labeled.php +++ b/src/axis/labeled.php @@ -44,39 +44,48 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis } /** - * Get labels from datasets in right order to be rendered later - * - * @param array $datasets + * Add data for this axis + * + * @param mixed $value Value which will be displayed on this axis * @return void */ - public function calculateFromDataset(array $datasets) + public function addData( array $values ) { - foreach ( $datasets as $dataset ) + $position = 0; + foreach ( $values as $label ) { - $position = 0; - foreach ( $dataset as $label => $value ) - { - $label = (string) $label; + $label = (string) $label; - if ( !in_array( $label, $this->labels, true ) ) + if ( !in_array( $label, $this->labels, true ) ) + { + if ( isset( $this->labels[$position] ) ) { - if ( isset( $this->labels[$position] ) ) - { - $this->labels = $this->increaseKeys( $this->labels, $position ); - $this->labels[$position++] = $label; - } - else - { - $this->labels[$position++] = $label; - } + $this->labels = $this->increaseKeys( $this->labels, $position ); + $this->labels[$position++] = $label; } - else + else { - $position = array_search( $label, $this->labels, true ) + 1; + $this->labels[$position++] = $label; } } - ksort( $this->labels ); + else + { + $position = array_search( $label, $this->labels, true ) + 1; + } } + ksort( $this->labels ); + } + + /** + * Calculate axis bounding values on base of the assigned values + * + * @abstract + * @access public + * @return void + */ + public function calculateAxisBoundings() + { + return true; } /** diff --git a/src/axis/numeric.php b/src/axis/numeric.php index 2640eb5..755e858 100644 --- a/src/axis/numeric.php +++ b/src/axis/numeric.php @@ -30,6 +30,20 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis protected $max = false; /** + * Minimum Value to display on this axis + * + * @var float + */ + protected $minValue = false; + + /** + * Maximum value to display on this axis + * + * @var float + */ + protected $maxValue = false; + + /** * Constant used for calculation of automatic definition of major scaling * steps */ @@ -164,55 +178,57 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis } /** - * Calculate steps, min and max values from given datasets, if not set - * manually before. receives an array of array( ezcGraphDataset ) + * Add data for this axis * - * @param array $datasets + * @param mixed $value Value which will be displayed on this axis * @return void */ - public function calculateFromDataset(array $datasets) + public function addData( array $values ) { - $min = false; - $max = false; - - // Determine minimum and maximum values - foreach ( $datasets as $dataset ) + foreach ( $values as $value ) { - foreach ( $dataset as $value ) + if ( $this->minValue === false || + $value < $this->minValue ) { - if ( $min === false || - $value < $min ) - { - $min = $value; - } + $this->minValue = $value; + } - if ( $max === false || - $value > $max ) - { - $max = $value; - } + if ( $this->maxValue === false || + $value > $this->maxValue ) + { + $this->maxValue = $value; } } + } + /** + * Calculate axis bounding values on base of the assigned values + * + * @abstract + * @access public + * @return void + */ + public function calculateAxisBoundings() + { // Calculate "nice" values for scaling parameters if ( $this->majorStep === false ) { - $this->calculateMajorStep( $min, $max ); + $this->calculateMajorStep( $this->minValue, $this->maxValue ); } if ( $this->minorStep === false ) { - $this->calculateMinorStep( $min, $max ); + $this->calculateMinorStep( $this->minValue, $this->maxValue ); } if ( $this->min === false ) { - $this->calculateMinimum( $min, $max ); + $this->calculateMinimum( $this->minValue, $this->maxValue ); } if ( $this->max === false ) { - $this->calculateMaximum( $min, $max ); + $this->calculateMaximum( $this->minValue, $this->maxValue ); } } diff --git a/src/charts/line.php b/src/charts/line.php index 5f92861..77d9f13 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -88,10 +88,22 @@ class ezcGraphLineChart extends ezcGraphChart $this->driver->options->height = $height; // Calculate axis scaling and labeling - $this->elements['X_axis']->calculateFromDataset( $this->data ); - $this->elements['X_axis']->font = $this->options->font; - $this->elements['Y_axis']->calculateFromDataset( $this->data ); - $this->elements['Y_axis']->font = $this->options->font; + foreach ( $this->data as $dataset ) + { + $labels = array(); + $values = array(); + foreach ( $dataset as $label => $value ) + { + $labels[] = $label; + $values[] = $value; + } + + $this->elements['X_axis']->addData( $labels ); + $this->elements['Y_axis']->addData( $values ); + } + + $this->elements['X_axis']->calculateAxisBoundings(); + $this->elements['Y_axis']->calculateAxisBoundings(); // Generate legend $this->elements['legend']->generateFromDatasets( $this->data ); diff --git a/src/element/axis.php b/src/element/axis.php index 0dead4e..32d4c7d 100644 --- a/src/element/axis.php +++ b/src/element/axis.php @@ -398,6 +398,23 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement } /** + * Add data for this axis + * + * @param mixed $value Value which will be displayed on this axis + * @return void + */ + abstract public function addData( array $values ); + + /** + * Calculate axis bounding values on base of the assigned values + * + * @abstract + * @access public + * @return void + */ + abstract public function calculateAxisBoundings(); + + /** * Render an axe * * @param ezcGraphRenderer $renderer @@ -412,12 +429,12 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->drawAxis( $renderer, new ezcGraphCoordinate( - $this->nullPosition, - $boundings->y0 + (int) $this->nullPosition, + (int) $boundings->y0 ), new ezcGraphCoordinate( - $this->nullPosition, - $boundings->y1 + (int) $this->nullPosition, + (int) $boundings->y1 ), $boundings ); @@ -426,12 +443,12 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->drawAxis( $renderer, new ezcGraphCoordinate( - $this->nullPosition, - $boundings->y1 + (int) $this->nullPosition, + (int) $boundings->y1 ), new ezcGraphCoordinate( - $this->nullPosition, - $boundings->y0 + (int) $this->nullPosition, + (int) $boundings->y0 ), $boundings ); @@ -440,12 +457,12 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->drawAxis( $renderer, new ezcGraphCoordinate( - $boundings->x0, - $this->nullPosition + (int) $boundings->x0, + (int) $this->nullPosition ), new ezcGraphCoordinate( - $boundings->x1, - $this->nullPosition + (int) $boundings->x1, + (int) $this->nullPosition ), $boundings ); @@ -454,12 +471,12 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->drawAxis( $renderer, new ezcGraphCoordinate( - $boundings->x1, - $this->nullPosition + (int) $boundings->x1, + (int) $this->nullPosition ), new ezcGraphCoordinate( - $boundings->x0, - $this->nullPosition + (int) $boundings->x0, + (int) $this->nullPosition ), $boundings ); -- cgit v1.1