diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/charts/pie.php | 23 | ||||
-rw-r--r-- | src/datasets/base.php | 112 | ||||
-rw-r--r-- | src/datasets/property/color.php | 30 | ||||
-rw-r--r-- | src/datasets/property/integer.php | 30 | ||||
-rw-r--r-- | src/datasets/property/string.php | 30 | ||||
-rw-r--r-- | src/exceptions/no_such_data.php | 21 | ||||
-rw-r--r-- | src/exceptions/too_many_datasets.php | 21 | ||||
-rw-r--r-- | src/exceptions/unknown_dataset_source.php | 21 | ||||
-rw-r--r-- | src/graph_autoload.php | 7 | ||||
-rw-r--r-- | src/interfaces/chart.php | 37 | ||||
-rw-r--r-- | src/interfaces/dataset_property.php | 173 |
11 files changed, 492 insertions, 13 deletions
diff --git a/src/charts/pie.php b/src/charts/pie.php index dc774c4..5ac49e8 100644 --- a/src/charts/pie.php +++ b/src/charts/pie.php @@ -25,5 +25,28 @@ class ezcGraphPieChart extends ezcGraphChart { } + + /** + * Adds a dataset to the charts data + * + * @param string $name Name of dataset + * @param mixed $values Values to create dataset with + * @throws ezcGraphTooManyDatasetExceptions + * If too many datasets are created + * @return ezcGraphDataset + */ + protected function addDataSet( $name, $values ) + { + if ( count( $this->data ) >= 1 && + !isset( $this->data[$name] ) ) + { + throw new ezcGraphTooManyDatasetsExceptions( $name ); + } + else + { + parent::addDataSet( $name, $values ); + } + } + } ?> diff --git a/src/datasets/base.php b/src/datasets/base.php index 03d53eb..c643855 100644 --- a/src/datasets/base.php +++ b/src/datasets/base.php @@ -12,9 +12,24 @@ * * @package Graph */ -class eczGraphDataset +class ezcGraphDataset implements ArrayAccess { + protected $label; + + protected $color; + + protected $symbol; + + protected $data; + + public function __construct() + { + $this->label = new ezcGraphDatasetStringProperty( $this ); + $this->color = new ezcGraphDatasetColorProperty( $this ); + $this->symbol = new ezcGraphDatasetIntProperty( $this ); + } + /** * setData * @@ -22,21 +37,102 @@ class eczGraphDataset * @access public * @return void */ - public function setData( $data = array() ) + public function createFromArray( $data = array() ) { - + foreach ( $data as $key => $value ) + { + if ( is_numeric( $value ) ) + { + $this->data[$key] = (float) $value; + } + } } + public function __set( $propertyName, $propertyValue ) + { + switch ( $propertyName ) + { + case 'label': + $this->label->default = $propertyValue; + break; + case 'color': + $this->color->default = $propertyValue; + break; + case 'symbol': + $this->symbol->default = $propertyValue; + break; + } + } + + public function __get( $propertyName ) + { + if ( isset( $this->$propertyName ) ) { + return $this->$propertyName; + } else { + throw new ezcBasePropertyNotFoundException( $propertyName ); + } + } + /** - * addData + * Returns if an option exists. + * Allows isset() using ArrayAccess. * - * @param array $data - * @access public + * @param string $key The name of the option to get. + * @return bool Wether the option exists. + */ + final public function offsetExists( $key ) + { + return isset( $this->data[$key] ); + } + + /** + * Returns an option value. + * Get an option 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 + */ + final public function offsetGet( $key ) + { + return $this->data[$key]; + } + + /** + * Set an option. + * Sets an option using ArrayAccess. + * + * @param string $key The option to set. + * @param mixed $value The value for the option. + * @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 ) + { + $this->data[$key] = (float) $value; + } + + /** + * Unset an option. + * Unsets an option using ArrayAccess. + * + * @param string $key The options to unset. * @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. */ - public function addData( $data = array() ) + final public function offsetUnset( $key ) { - + unset( $this->data[$key] ); } } diff --git a/src/datasets/property/color.php b/src/datasets/property/color.php new file mode 100644 index 0000000..a177c4b --- /dev/null +++ b/src/datasets/property/color.php @@ -0,0 +1,30 @@ +<?php +/** + * File containing the abstract ezcGraphDatasetColorProperty class + * + * @package Graph + * @version $id$ + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Class for color properties of datasets + * + * @package Graph + */ +class ezcGraphDatasetColorProperty extends ezcGraphDatasetProperty +{ + /** + * Converts value to an ezcGraphColor object + * + * @param & $value + * @return void + */ + protected function checkValue( &$value ) + { + $value = ezcGraphColor::create( $value ); + return true; + } +} + +?> diff --git a/src/datasets/property/integer.php b/src/datasets/property/integer.php new file mode 100644 index 0000000..67c9c73 --- /dev/null +++ b/src/datasets/property/integer.php @@ -0,0 +1,30 @@ +<?php +/** + * File containing the abstract ezcGraphDatasetIntProperty class + * + * @package Graph + * @version $id$ + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Class for integer properties of datasets + * + * @package Graph + */ +class ezcGraphDatasetIntProperty extends ezcGraphDatasetProperty +{ + /** + * Converts value to an ezcGraphColor object + * + * @param & $value + * @return void + */ + protected function checkValue( &$value ) + { + $value = (int) $value; + return true; + } +} + +?> diff --git a/src/datasets/property/string.php b/src/datasets/property/string.php new file mode 100644 index 0000000..a96dab1 --- /dev/null +++ b/src/datasets/property/string.php @@ -0,0 +1,30 @@ +<?php +/** + * File containing the abstract ezcGraphDatasetStringProperty class + * + * @package Graph + * @version $id$ + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Class for string properties of datasets + * + * @package Graph + */ +class ezcGraphDatasetStringProperty extends ezcGraphDatasetProperty +{ + /** + * Converts value to an ezcGraphColor object + * + * @param & $value + * @return void + */ + protected function checkValue( &$value ) + { + $value = (string) $value; + return true; + } +} + +?> diff --git a/src/exceptions/no_such_data.php b/src/exceptions/no_such_data.php new file mode 100644 index 0000000..dd73c0f --- /dev/null +++ b/src/exceptions/no_such_data.php @@ -0,0 +1,21 @@ +<?php +/** + * File containing the ezcGraphNoSuchDataException class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * ezcGraphUnknownChartTypeException is the exception which is thrown when the + * factory method tries to return an instance of an unknown chart type + * + * @package Graph + * @version //autogen// + */ +class ezcGraphNoSuchDataException extends ezcBaseException +{ +} + +?> diff --git a/src/exceptions/too_many_datasets.php b/src/exceptions/too_many_datasets.php new file mode 100644 index 0000000..65cbd0d --- /dev/null +++ b/src/exceptions/too_many_datasets.php @@ -0,0 +1,21 @@ +<?php +/** + * File containing the ezcGraphTooManyDatasetsExceptions class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * ezcGraphUnknownChartTypeException is the exception which is thrown when the + * factory method tries to return an instance of an unknown chart type + * + * @package Graph + * @version //autogen// + */ +class ezcGraphTooManyDatasetsExceptions extends ezcBaseException +{ +} + +?> diff --git a/src/exceptions/unknown_dataset_source.php b/src/exceptions/unknown_dataset_source.php new file mode 100644 index 0000000..e3d359e --- /dev/null +++ b/src/exceptions/unknown_dataset_source.php @@ -0,0 +1,21 @@ +<?php +/** + * File containing the ezcGraphUnknownDatasetSourceException class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * ezcGraphUnknownChartTypeException is the exception which is thrown when the + * factory method tries to return an instance of an unknown chart type + * + * @package Graph + * @version //autogen// + */ +class ezcGraphUnknownDatasetSourceException extends ezcBaseException +{ +} + +?> diff --git a/src/graph_autoload.php b/src/graph_autoload.php index 6d46924..f7275ff 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -34,7 +34,14 @@ return array( 'ezcGraphDataset' => 'Graph/datasets/base.php', 'ezcGraphDatasetAverage' => 'Graph/datasets/average.php', + 'ezcGraphDatasetProperty' => 'Graph/interfaces/dataset_property.php', + 'ezcGraphDatasetColorProperty' => 'Graph/datasets/property/color.php', + 'ezcGraphDatasetStringProperty' => 'Graph/datasets/property/string.php', + 'ezcGraphDatasetIntProperty' => 'Graph/datasets/property/integer.php', + 'ezcGraphNoSuchDataException' => 'Graph/exceptions/no_such_data.php', 'ezcGraphNoSuchDatasetException' => 'Graph/exceptions/no_such_dataset.php', + 'ezcGraphTooManyDatasetsExceptions' => 'Graph/exceptions/too_many_datasets.php', + 'ezcGraphUnknownDatasetSourceException' => 'Graph/exceptions/unknown_dataset_source.php', ); ?> diff --git a/src/interfaces/chart.php b/src/interfaces/chart.php index b9033dd..e4ba22a 100644 --- a/src/interfaces/chart.php +++ b/src/interfaces/chart.php @@ -71,14 +71,13 @@ abstract class ezcGraphChart * If value is out of range * @param mixed $propertyName Option name * @param mixed $propertyValue Option value; - * @return void + * @return mixed */ public function __set( $propertyName, $propertyValue ) { switch ( $propertyName ) { case 'title': - $this->title = (string) $propertyValue; - return $this; + return $this->title = (string) $propertyValue; break; case 'renderer': if ( $propertyValue instanceof ezcGraphRenderer ) @@ -101,12 +100,40 @@ abstract class ezcGraphChart } break; default: - // Consider everything else as dataset - // @TODO: Implement + return $this->addDataSet($propertyName, $propertyValue); break; } } + /** + * Adds a dataset to the charts data + * + * @param string $name Name of dataset + * @param mixed $values Values to create dataset with + * @throws ezcGraphTooManyDatasetExceptions + * If too many datasets are created + * @return ezcGraphDataset + */ + protected function addDataSet( $name, $values ) + { + $this->data[$name] = new ezcGraphDataset(); + + if ( is_array($values) ) + { + $this->data[$name]->createFromArray( $values ); + $this->data[$name]->label = $name; + } + elseif ( $values instanceof PDOStatement ) + { + $this->data[$name]->createFromStatement( $values ); + $this->data[$name]->label = $name; + } + else + { + throw new ezcGraphUnknownDatasetSourceException( $values ); + } + } + public function __get( $propertyName ) { if ( isset( $this->$propertyName ) ) diff --git a/src/interfaces/dataset_property.php b/src/interfaces/dataset_property.php new file mode 100644 index 0000000..246ca5f --- /dev/null +++ b/src/interfaces/dataset_property.php @@ -0,0 +1,173 @@ +<?php +/** + * File containing the abstract ezcGraphDatasetProperty class + * + * @package Graph + * @version $id$ + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Abstract class for properties of datasets + * + * @package Graph + */ +abstract class ezcGraphDatasetProperty implements ArrayAccess +{ + /** + * Default value for this property + * + * @var mixed + */ + protected $defaultValue; + + /** + * Contains specified values for single dataset elements + * + * @var array + */ + protected $dataValue; + + /** + * Contains a reference to the dataset to check for availability of data + * keys + * + * @var ezcGraphDataset + */ + protected $dataset; + + /** + * Abstract method to contain the check for validity of the value + * + * @param & $value + * @abstract + * @access protected + * @return void + */ + abstract protected function checkValue( &$value ); + + public function __construct( ezcGraphDataset $dataset ) + { + $this->dataset = $dataset; + } + + /** + * Set the default value for this property + * + * @param string $name Property name + * @param mixed $value Property value + * @return void + */ + public function __set( $name, $value ) + { + if ( $name === 'default' && + $this->checkValue( $value ) ) + { + $this->defaultValue = $value; + } + } + + /** + * Get the default value for this property + * + * @param string $name Property name + * @return mixed + */ + public function __get( $name ) + { + if ( $name === 'default' ) + { + return $this->defaultValue; + } + } + + /** + * Returns if an option exists. + * Allows isset() using ArrayAccess. + * + * @param string $key The name of the option to get. + * @return bool Wether the option exists. + */ + final public function offsetExists( $key ) + { + return isset( $this->dataset[$key] ); + } + + /** + * Returns an option value. + * Get an option 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 + */ + final public function offsetGet( $key ) + { + if ( isset( $this->dataValue[$key] ) ) + { + return $this->dataValue[$key]; + } + elseif ( isset( $this->dataset[$key] ) ) + { + return $this->defaultValue; + } + else + { + throw new ezcGraphNoSuchDataException( $key ); + } + } + + /** + * Set an option. + * Sets an option using ArrayAccess. + * + * @param string $key The option to set. + * @param mixed $value The value for the option. + * @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 ) + { + if ( isset( $this->dataset[$key] ) && + $this->checkValue( $value ) ) + { + $this->dataValue[$key] = $value; + } + else + { + throw new ezcGraphNoSuchDataException( $key ); + } + } + + /** + * Unset an option. + * Unsets an option using ArrayAccess. + * + * @param string $key The options to unset. + * @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 offsetUnset( $key ) + { + if ( isset( $this->dataset[$value] ) ) + { + unset( $this->dataValue[$key] ); + } + else + { + throw new ezcGraphNoSuchDataException( $key ); + } + } +} + +?> |