diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-05-24 12:29:24 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-05-24 12:29:24 +0000 |
commit | aa49d9ec7aaa79cc42043224386ffc30bf7c7b0f (patch) | |
tree | fd09d73e08ca90ee14e46a76a78095cffeaa0e19 | |
parent | 4029d5db40e0cc0c59647d1be98ead6602800d9a (diff) | |
download | zetacomponents-graph-aa49d9ec7aaa79cc42043224386ffc30bf7c7b0f.zip zetacomponents-graph-aa49d9ec7aaa79cc42043224386ffc30bf7c7b0f.tar.gz |
- Fixed issue #10842: Pie charts fatal error with datasets with value sum <= 0
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | src/charts/pie.php | 12 | ||||
-rw-r--r-- | src/datasets/array.php | 6 | ||||
-rw-r--r-- | src/exceptions/invalid_data.php | 25 | ||||
-rw-r--r-- | src/graph_autoload.php | 1 | ||||
-rw-r--r-- | tests/pie_test.php | 48 |
6 files changed, 92 insertions, 1 deletions
@@ -14,6 +14,7 @@ - Fixed issue #10746: Border size reducement algorithm fails for polygones with edge lengths < reducement - Fixed issue #10750: SVG drivers output broken with wrong LC_NUMERIC +- Fixed issue #10842: Pie charts fatal error with datasets with value sum <= 0 1.1beta1 - Monday 07 May 2007 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/charts/pie.php b/src/charts/pie.php index 782ad32..4d616c8 100644 --- a/src/charts/pie.php +++ b/src/charts/pie.php @@ -86,8 +86,13 @@ class ezcGraphPieChart extends ezcGraphChart // Calculate sum of all values to be able to calculate percentage $sum = 0; - foreach ( $dataset as $value ) + foreach ( $dataset as $name => $value ) { + if ( $value <= 0 ) + { + throw new ezcGraphInvalidDataException( "Values > 0 required, '$name' => '$value'." ); + } + $sum += $value; } if ( $this->options->sum !== false ) @@ -95,6 +100,11 @@ class ezcGraphPieChart extends ezcGraphChart $sum = max( $sum, $this->options->sum ); } + if ( $sum <= 0 ) + { + throw new ezcGraphInvalidDataException( "Pie charts require a value sum > 0, your value: '$sum'." ); + } + $angle = 0; foreach ( $dataset as $label => $value ) { diff --git a/src/datasets/array.php b/src/datasets/array.php index 3340cd0..ee7f0df 100644 --- a/src/datasets/array.php +++ b/src/datasets/array.php @@ -44,10 +44,16 @@ class ezcGraphArrayDataSet extends ezcGraphDataSet throw new ezcGraphInvalidArrayDataSourceException( $data ); } + $this->data = array(); foreach ( $data as $key => $value ) { $this->data[$key] = $value; } + + if ( !count( $this->data ) ) + { + throw new ezcGraphInvalidDataException( 'Data sets should contain some values.' ); + } } /** diff --git a/src/exceptions/invalid_data.php b/src/exceptions/invalid_data.php new file mode 100644 index 0000000..4c8f3f2 --- /dev/null +++ b/src/exceptions/invalid_data.php @@ -0,0 +1,25 @@ +<?php +/** + * File containing the ezcGraphInvalidDataException class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Exception thrown when invalid data is provided, which cannot be rendered + * for some reason. + * + * @package Graph + * @version //autogen// + */ +class ezcGraphInvalidDataException extends ezcGraphException +{ + public function __construct( $message ) + { + parent::__construct( "You provided unusable data: '$message'." ); + } +} + +?> diff --git a/src/graph_autoload.php b/src/graph_autoload.php index 12aea2b..5bc8507 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -36,6 +36,7 @@ return array( 'ezcGraphUnknownColorDefinitionException' => 'Graph/exceptions/unknown_color_definition.php', 'ezcGraphUnknownFontTypeException' => 'Graph/exceptions/font_type.php', 'ezcGraphUnregularStepsException' => 'Graph/exceptions/unregular_steps.php', + 'ezcGraphInvalidDataException' => 'Graph/exceptions/invalid_data.php', 'ezcGraphChart' => 'Graph/interfaces/chart.php', 'ezcGraphChartElement' => 'Graph/interfaces/element.php', 'ezcGraphChartOptions' => 'Graph/options/chart.php', diff --git a/tests/pie_test.php b/tests/pie_test.php index f1debe4..5ddf66a 100644 --- a/tests/pie_test.php +++ b/tests/pie_test.php @@ -394,6 +394,54 @@ class ezcGraphPieChartTest extends ezcGraphTestCase $chart->render( 400, 200 ); } + public function testInavlidValues() + { + try + { + $chart = new ezcGraphPieChart(); + $chart->data['Skien'] = new ezcGraphArrayDataSet( array() ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphInvalidDataException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphInvalidDataException.' ); + } + + public function testInvalidValues() + { + try + { + $chart = new ezcGraphPieChart(); + $chart->data['Skien'] = new ezcGraphArrayDataSet( array() ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphInvalidDataException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphInvalidDataException.' ); + } + + public function testEmptyDataSet() + { + try + { + $chart = new ezcGraphPieChart(); + $chart->data['Skien'] = new ezcGraphArrayDataSet( array() ); + $chart->render( 500, 200 ); + } + catch ( ezcGraphInvalidDataException $e ) + { + return true; + } + + $this->fail( 'Expected ezcGraphInvalidDataException.' ); + } + public function testRenderSmallPieChart() { $filename = $this->tempDir . __FUNCTION__ . '.svg'; |