summaryrefslogtreecommitdiffstats
path: root/src/datasets
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2006-09-06 14:46:41 +0000
committerKore Nordmann <github@kore-nordmann.de>2006-09-06 14:46:41 +0000
commit9f40ec30e4737262a2132dfa76dc0beba30fa795 (patch)
tree5ba5ae643af19c62548be81765c2fddabccc6487 /src/datasets
parent3b131319615c8949013734a141ce3939ea4b614d (diff)
downloadzetacomponents-graph-9f40ec30e4737262a2132dfa76dc0beba30fa795.zip
zetacomponents-graph-9f40ec30e4737262a2132dfa76dc0beba30fa795.tar.gz
- Got average datasets working
Diffstat (limited to 'src/datasets')
-rw-r--r--src/datasets/average.php191
-rw-r--r--src/datasets/base.php118
2 files changed, 270 insertions, 39 deletions
diff --git a/src/datasets/average.php b/src/datasets/average.php
index 2cc7f2e..0dc8f94 100644
--- a/src/datasets/average.php
+++ b/src/datasets/average.php
@@ -14,7 +14,7 @@
* @property int $polynomOrder
* Maximum order of polygon to interpolate from points
* @property int $resolution
- * Rsolution used to draw line in graph
+ * Resolution used to draw line in graph
*
* @package Graph
*/
@@ -25,6 +25,14 @@ class ezcGraphDataSetAveragePolynom extends ezcGraphDataSet
protected $polynom = false;
+ protected $min = false;
+
+ protected $max = false;
+
+ protected $position = 0;
+
+ protected $properties;
+
/**
* Constructor
*
@@ -34,7 +42,9 @@ class ezcGraphDataSetAveragePolynom extends ezcGraphDataSet
*/
public function __construct( ezcGraphDataSet $dataset )
{
- $this->properties['resolution'] = 5;
+ parent::__construct();
+
+ $this->properties['resolution'] = 100;
$this->properties['polynomOrder'] = 3;
$this->source = $dataset;
@@ -58,18 +68,58 @@ class ezcGraphDataSetAveragePolynom extends ezcGraphDataSet
$this->properties['polynomOrder'] = (int) $propertyValue;
$this->polynom = false;
break;
+ case 'resolution':
+ $this->properties['polynomOrder'] = max( 1, (int) $propertyValue );
+ break;
default:
- throw new ezcBasePropertyNotFoundException( $propertyName );
+ parent::__set( $propertyName, $propertyValue );
break;
}
}
+
+ /**
+ * Property get access.
+ * Simply returns a given option.
+ *
+ * @param string $propertyName The name of the option to get.
+ * @return mixed The option value.
+ *
+ * @throws ezcBasePropertyNotFoundException
+ * If a the value for the property options is not an instance of
+ */
+ public function __get( $propertyName )
+ {
+ if ( isset( $this->properties[$propertyName] ) )
+ {
+ return $this->properties[$propertyName];
+ }
+ else
+ {
+ return parent::__get( $propertyName );
+ }
+ }
+ /**
+ * Build the polynom based on the given points.
+ *
+ * @return void
+ */
protected function buildPolynom()
{
$points = array();
foreach ( $this->source as $key => $value )
{
+ if ( ( $this->min === false ) || ( $this->min > $key ) )
+ {
+ $this->min = (float) $key;
+ }
+
+ if ( ( $this->max === false ) || ( $this->max < $key ) )
+ {
+ $this->max = (float) $key;
+ }
+
$points[] = new ezcGraphCoordinate( (float) $key, (float) $value );
}
@@ -95,6 +145,12 @@ class ezcGraphDataSetAveragePolynom extends ezcGraphDataSet
$this->polynom = $left->solveNonlinearEquatation( $right );
}
+ /**
+ * Returns a polynom of the defined order witch matches the datapoints
+ * using the least squares algorithm.
+ *
+ * @return ezcGraphPolynom Polynom
+ */
public function getPolynom()
{
if ( $this->polynom === false )
@@ -104,5 +160,134 @@ class ezcGraphDataSetAveragePolynom extends ezcGraphDataSet
return $this->polynom;
}
+
+ /**
+ * Get the x coordinate for the current position
+ *
+ * @param int $position Position
+ * @return float x coordinate
+ */
+ protected function getKey()
+ {
+ return $this->min +
+ ( $this->max - $this->min ) / $this->resolution * $this->position;
+ }
+
+ /**
+ * Returns true if the given datapoint exists
+ * Allows isset() using ArrayAccess.
+ *
+ * @param string $key The key of the datapoint to get.
+ * @return bool Wether the key exists.
+ */
+ public function offsetExists( $key )
+ {
+ return ( ( $key >= $this->min ) && ( $key <= $this->max ) );
+ }
+
+ /**
+ * Returns the value for the given datapoint
+ * Get an datapoint value by ArrayAccess.
+ *
+ * @param string $key The key of the datapoint to get.
+ * @return float The datapoint value.
+ */
+ public function offsetGet( $key )
+ {
+ $polynom = $this->getPolynom();
+ return $polynom->evaluate( $key );
+ }
+
+ /**
+ * Throws a ezcBasePropertyPermissionException because single datapoints
+ * cannot be set in average datasets.
+ *
+ * @param string $key The kex of a datapoint to set.
+ * @param float $value The value for the datapoint.
+ * @throws ezcBasePropertyPermissionException
+ * Always, because access is readonly.
+ * @return void
+ */
+ public function offsetSet( $key, $value )
+ {
+ throw new ezcBasePropertyPermissionException( $key, ezcBasePropertyPermissionException::READ );
+ }
+
+ /**
+ * Returns the currently selected datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return string The currently selected datapoint.
+ */
+ final public function current()
+ {
+ $polynom = $this->getPolynom();
+ return $polynom->evaluate( $this->getKey() );
+ }
+
+ /**
+ * Returns the next datapoint and selects it or false on the last datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return float datapoint if it exists, or false.
+ */
+ final public function next()
+ {
+ if ( ++$this->position >= $this->resolution )
+ {
+ return false;
+ }
+ else
+ {
+ return $this->current();
+ }
+ }
+
+ /**
+ * Returns the key of the currently selected datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return string The key of the currently selected datapoint.
+ */
+ final public function key()
+ {
+ return (string) $this->getKey();
+ }
+
+ /**
+ * Returns if the current datapoint is valid.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return bool If the current datapoint is valid
+ */
+ final public function valid()
+ {
+ return ( ( $this->getKey() >= $this->min ) && ( $this->getKey() <= $this->max ) );
+ }
+
+ /**
+ * Selects the very first datapoint and returns it.
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return float The very first datapoint.
+ */
+ final public function rewind()
+ {
+ $this->position = 0;
+ }
}
?>
diff --git a/src/datasets/base.php b/src/datasets/base.php
index 2f5fc92..8ab6e5d 100644
--- a/src/datasets/base.php
+++ b/src/datasets/base.php
@@ -16,28 +16,28 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
{
/**
- * labels for dataset and dataset elements
+ * labels for datapoint and datapoint elements
*
* @var ezcGraphDataSetStringProperty
*/
protected $label;
/**
- * Colors for dataset elements
+ * Colors for datapoint elements
*
* @var ezcGraphDataSetColorProperty
*/
protected $color;
/**
- * Symbols for dataset elements
+ * Symbols for datapoint elements
*
* @var ezcGraphDataSetIntProperty
*/
protected $symbol;
/**
- * Status if dataset element is hilighted
+ * Status if datapoint element is hilighted
*
* @var ezcGraphDataSetBooleanProperty
* @access protected
@@ -52,22 +52,22 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
protected $displayType;
/**
- * Array which contains the data of the dataset
+ * Array which contains the data of the datapoint
*
* @var array
*/
protected $data;
/**
- * Current dataset element
- * needed for iteration over dataset with ArrayAccess
+ * Current datapoint element
+ * needed for iteration over datapoint with ArrayAccess
*
* @var mixed
*/
protected $current;
/**
- * Color palette used for dataset colorization
+ * Color palette used for datapoint colorization
*
* @var ezcGraphPalette
*/
@@ -112,6 +112,16 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
}
}
+ /**
+ * Property get access.
+ * Simply returns a given option.
+ *
+ * @param string $propertyName The name of the option to get.
+ * @return mixed The option value.
+ *
+ * @throws ezcBasePropertyNotFoundException
+ * If a the value for the property options is not an instance of
+ */
public function __get( $propertyName )
{
if ( isset( $this->$propertyName ) ) {
@@ -124,46 +134,38 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
}
/**
- * Returns if an option exists.
+ * Returns true if the given datapoint exists
* Allows isset() using ArrayAccess.
*
- * @param string $key The name of the option to get.
- * @return bool Wether the option exists.
+ * @param string $key The key of the datapoint to get.
+ * @return bool Wether the key exists.
*/
- final public function offsetExists( $key )
+ public function offsetExists( $key )
{
return isset( $this->data[$key] );
}
/**
- * Returns an option value.
- * Get an option value by ArrayAccess.
+ * Returns the value for the given datapoint
+ * Get an datapoint value by ArrayAccess.
*
- * @param string $key The name of the option to get.
- * @return mixed The option value.
- *
- * @throws ezcBasePropertyNotFoundException
- * If a the value for the property options is not an instance of
+ * @param string $key The key of the datapoint to get.
+ * @return float The datapoint value.
*/
- final public function offsetGet( $key )
+ public function offsetGet( $key )
{
return $this->data[$key];
}
/**
- * Set an option.
- * Sets an option using ArrayAccess.
+ * Sets the value for a datapoint.
+ * Sets an datapoint using ArrayAccess.
*
- * @param string $key The option to set.
- * @param mixed $value The value for the option.
+ * @param string $key The kex of a datapoint to set.
+ * @param float $value The value for the datapoint.
* @return void
- *
- * @throws ezcBasePropertyNotFoundException
- * If a the value for the property options is not an instance of
- * @throws ezcBaseValueException
- * If a the value for a property is out of range.
*/
- final public function offsetSet( $key, $value )
+ public function offsetSet( $key, $value )
{
$this->data[$key] = (float) $value;
}
@@ -180,12 +182,21 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
* @throws ezcBaseValueException
* If a the value for a property is out of range.
*/
- final public function offsetUnset( $key )
+ public function offsetUnset( $key )
{
unset( $this->data[$key] );
}
- final public function current()
+ /**
+ * Returns the currently selected datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return string The currently selected datapoint.
+ */
+ public function current()
{
$keys = array_keys( $this->data );
if ( !isset( $this->current ) )
@@ -196,7 +207,16 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
return $this->data[$keys[$this->current]];
}
- final public function next()
+ /**
+ * Returns the next datapoint and selects it or false on the last datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return float datapoint if it exists, or false.
+ */
+ public function next()
{
$keys = array_keys( $this->data );
if ( ++$this->current >= count( $keys ) )
@@ -209,19 +229,45 @@ abstract class ezcGraphDataSet implements ArrayAccess, Iterator
}
}
- final public function key()
+ /**
+ * Returns the key of the currently selected datapoint.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return string The key of the currently selected datapoint.
+ */
+ public function key()
{
$keys = array_keys( $this->data );
return $keys[$this->current];
}
- final public function valid()
+ /**
+ * Returns if the current datapoint is valid.
+ *
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return bool If the current datapoint is valid
+ */
+ public function valid()
{
$keys = array_keys( $this->data );
return isset( $keys[$this->current] );
}
- final public function rewind()
+ /**
+ * Selects the very first datapoint and returns it.
+ * This method is part of the Iterator interface to allow access to the
+ * datapoints of this row by iterating over it like an array (e.g. using
+ * foreach).
+ *
+ * @return float The very first datapoint.
+ */
+ public function rewind()
{
$this->current = 0;
}
OpenPOWER on IntegriCloud