summaryrefslogtreecommitdiffstats
path: root/src/axis
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2006-05-24 12:07:19 +0000
committerKore Nordmann <github@kore-nordmann.de>2006-05-24 12:07:19 +0000
commiteb7d6e8d3679a4680a20a43a3c949120c3d61ea2 (patch)
tree86b29283938c622917a42557da0aad38fe23dc4c /src/axis
parenta5476184b65f07f983d9f676d74550bcfe1925d6 (diff)
downloadzetacomponents-graph-eb7d6e8d3679a4680a20a43a3c949120c3d61ea2.zip
zetacomponents-graph-eb7d6e8d3679a4680a20a43a3c949120c3d61ea2.tar.gz
- Added tests for custom padding and symbol sizes for legend
- Moved legend renderer test to legend_test.php - Refactored axis to inherit from ElementAxis - Defined methods for axis rendering
Diffstat (limited to 'src/axis')
-rw-r--r--src/axis/labeled_axis.php106
-rw-r--r--src/axis/numeric_axis.php272
2 files changed, 378 insertions, 0 deletions
diff --git a/src/axis/labeled_axis.php b/src/axis/labeled_axis.php
new file mode 100644
index 0000000..b10fe6b
--- /dev/null
+++ b/src/axis/labeled_axis.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * File containing the abstract ezcGraphChartElementLabeledAxis class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Class to represent a axe as a chart element
+ *
+ * @package Graph
+ */
+class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis
+{
+
+ /**
+ * Array with labeles for data
+ *
+ * @var array
+ */
+ protected $labels = array();
+
+ protected function increaseKeys( $array, $startKey )
+ {
+ foreach ( $array as $key => $value )
+ {
+ if ( $key === $startKey )
+ {
+ // Recursive check, if next key should be increased, too
+ if ( isset ( $array[$key + 1] ) )
+ {
+ $array = $this->increaseKeys( $array, $key + 1 );
+ }
+
+ // Increase key
+ $array[$key + 1] = $array[$key];
+ unset( $array[$key] );
+ }
+ }
+
+ return $array;
+ }
+
+ /**
+ * Get labels from datasets in right order to be rendered later
+ *
+ * @param array $datasets
+ * @return void
+ */
+ public function calculateFromDataset(array $datasets)
+ {
+ foreach ( $datasets as $dataset )
+ {
+ $position = 0;
+ foreach ( $dataset as $label => $value )
+ {
+ $label = (string) $label;
+
+ if ( !in_array( $label, $this->labels, true ) )
+ {
+ if ( isset( $this->labels[$position] ) )
+ {
+ $this->labels = $this->increaseKeys( $this->labels, $position );
+ $this->labels[$position++] = $label;
+ }
+ else
+ {
+ $this->labels[$position++] = $label;
+ }
+ }
+ else
+ {
+ $position = array_search( $label, $this->labels, true ) + 1;
+ }
+ }
+ ksort( $this->labels );
+ }
+ }
+
+ /**
+ * Get coordinate for a dedicated value on the chart
+ *
+ * @param ezcGraphBounding $boundings
+ * @param string $value Value to determine position for
+ * @return float Position on chart
+ */
+ public function getCoordinate( ezcGraphBoundings $boundings, $value )
+ {
+
+ }
+
+ /**
+ * Render an axe
+ *
+ * @param ezcGraphRenderer $renderer
+ * @access public
+ * @return void
+ */
+ public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
+ {
+ return $boundings;
+ }
+
+}
diff --git a/src/axis/numeric_axis.php b/src/axis/numeric_axis.php
new file mode 100644
index 0000000..a34fe5b
--- /dev/null
+++ b/src/axis/numeric_axis.php
@@ -0,0 +1,272 @@
+<?php
+/**
+ * File containing the abstract ezcGraphChartElementNumericAxis class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Class to represent a axe as a chart element
+ *
+ * @package Graph
+ */
+class ezcGraphChartElementNumericAxis extends ezcGraphChartElementAxis
+{
+
+ /**
+ * Minimum value of displayed scale on axis
+ *
+ * @var float
+ */
+ protected $min = false;
+
+ /**
+ * Maximum value of displayed scale on axis
+ *
+ * @var float
+ */
+ protected $max = false;
+
+ /**
+ * Labeled major steps displayed on the axis
+ *
+ * @var float
+ */
+ protected $majorStep = false;
+
+ /**
+ * Non labeled minor steps on the axis
+ *
+ * @var mixed
+ * @access protected
+ */
+ protected $minorStep = false;
+
+ /**
+ * Constant used for calculation of automatic definition of major scaling
+ * steps
+ */
+ const MIN_MAJOR_COUNT = 5;
+
+ /**
+ * Constant used for automatic calculation of minor steps from given major
+ * steps
+ */
+ const MIN_MINOR_COUNT = 8;
+
+ /**
+ * __set
+ *
+ * @param mixed $propertyName
+ * @param mixed $propertyValue
+ * @throws ezcBaseValueException
+ * If a submitted parameter was out of range or type.
+ * @throws ezcBasePropertyNotFoundException
+ * If a the value for the property options is not an instance of
+ * @return void
+ */
+ public function __set( $propertyName, $propertyValue )
+ {
+ switch ( $propertyName )
+ {
+ case 'min':
+ $this->min = (float) $propertyValue;
+ break;
+ case 'max':
+ $this->max = (float) $propertyValue;
+ break;
+ case 'majorStep':
+ if ( $propertyValue <= 0 )
+ {
+ throw new ezcBaseValueException( 'majorStep', $propertyValue, 'float > 0' );
+ }
+ $this->majorStep = (float) $propertyValue;
+ break;
+ case 'minorStep':
+ if ( $propertyValue <= 0 )
+ {
+ throw new ezcBaseValueException( 'minorStep', $propertyValue, 'float > 0' );
+ }
+ $this->minorStep = (float) $propertyValue;
+ break;
+ default:
+ parent::__set( $propertyName, $propertyValue );
+ break;
+ }
+ }
+
+ /**
+ * Returns a "nice" number for a given floating point number.
+ *
+ * Nice numbers are steps on a scale which are easily recognized by humans
+ * like 0.5, 25, 1000 etc.
+ *
+ * @param float $float Number to be altered
+ * @return float Nice number
+ */
+ protected function getNiceNumber( $float )
+ {
+ // Get absolute value and save sign
+ $abs = abs( $float );
+ $sign = $float / $abs;
+
+ // Normalize number to a range between 1 and 10
+ $log = (int) round( log10( $abs ), 0);
+ $abs /= pow( 10, $log );
+
+
+ // find next nice number
+ if ( $abs > 5 ) {
+ $abs = 10.;
+ }
+ elseif ( $abs > 2.5 )
+ {
+ $abs = 5.;
+ }
+ elseif ( $abs > 1 )
+ {
+ $abs = 2.5;
+ }
+ else
+ {
+ $abs = 1;
+ }
+
+ // unnormalize number to original values
+ return $abs * pow( 10, $log ) * $sign;
+ }
+
+ /**
+ * Calculate minimum value for displayed axe basing on real minimum and
+ * major step size
+ *
+ * @param float $min Real data minimum
+ * @param float $max Real data maximum
+ * @return void
+ */
+ protected function calculateMinimum( $min, $max )
+ {
+ $this->min = floor( $min / $this->majorStep ) * $this->majorStep;
+ }
+
+ /**
+ * Calculate maximum value for displayed axe basing on real maximum and
+ * major step size
+ *
+ * @param float $min Real data minimum
+ * @param float $max Real data maximum
+ * @return void
+ */
+ protected function calculateMaximum( $min, $max )
+ {
+ $this->max = ceil( $max / $this->majorStep ) * $this->majorStep;
+ }
+
+ /**
+ * Calculate size of minor steps based on the size of the major step size
+ *
+ * @param float $min Real data minimum
+ * @param float $max Real data maximum
+ * @return void
+ */
+ protected function calculateMinorStep( $min, $max )
+ {
+ $stepSize = $this->majorStep / self::MIN_MINOR_COUNT;
+ $this->minorStep = $this->getNiceNumber( $stepSize );
+ }
+
+ /**
+ * Calculate size of major step based on the span to be displayed and the
+ * defined MIN_MAJOR_COUNT constant.
+ *
+ * @param float $min Real data minimum
+ * @param float $max Real data maximum
+ * @return void
+ */
+ protected function calculateMajorStep( $min, $max )
+ {
+ $span = $max - $min;
+ $stepSize = $span / self::MIN_MAJOR_COUNT;
+ $this->majorStep = $this->getNiceNumber( $stepSize );
+ }
+
+ /**
+ * Calculate steps, min and max values from given datasets, if not set
+ * manually before. receives an array of array( ezcGraphDataset )
+ *
+ * @param array $datasets
+ * @return void
+ */
+ public function calculateFromDataset(array $datasets)
+ {
+ $min = false;
+ $max = false;
+
+ // Determine minimum and maximum values
+ foreach ( $datasets as $dataset )
+ {
+ foreach ( $dataset as $value )
+ {
+ if ( $min === false ||
+ $value < $min )
+ {
+ $min = $value;
+ }
+
+ if ( $max === false ||
+ $value > $max )
+ {
+ $max = $value;
+ }
+ }
+ }
+
+ // Calculate "nice" values for scaling parameters
+ if ( $this->majorStep === false )
+ {
+ $this->calculateMajorStep( $min, $max );
+ }
+
+ if ( $this->minorStep === false )
+ {
+ $this->calculateMinorStep( $min, $max );
+ }
+
+ if ( $this->min === false )
+ {
+ $this->calculateMinimum( $min, $max );
+ }
+
+ if ( $this->max === false )
+ {
+ $this->calculateMaximum( $min, $max );
+ }
+ }
+
+ /**
+ * Get coordinate for a dedicated value on the chart
+ *
+ * @param ezcGraphBounding $boundings
+ * @param float $value Value to determine position for
+ * @return float Position on chart
+ */
+ public function getCoordinate( ezcGraphBoundings $boundings, $value )
+ {
+
+ }
+
+ /**
+ * Render an axe
+ *
+ * @param ezcGraphRenderer $renderer
+ * @access public
+ * @return void
+ */
+ public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
+ {
+ return $boundings;
+ }
+
+}
OpenPOWER on IntegriCloud