From aa49d9ec7aaa79cc42043224386ffc30bf7c7b0f Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Thu, 24 May 2007 12:29:24 +0000 Subject: - Fixed issue #10842: Pie charts fatal error with datasets with value sum <= 0 --- src/charts/pie.php | 12 +++++++++++- src/datasets/array.php | 6 ++++++ src/exceptions/invalid_data.php | 25 +++++++++++++++++++++++++ src/graph_autoload.php | 1 + 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/exceptions/invalid_data.php (limited to 'src') 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 @@ + 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', -- cgit v1.1