From 11e6744455cadf222f828a437e095205da53da7a Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Tue, 14 Jul 2009 14:13:45 +0000 Subject: - Implemented: #13341: Vertical Bar Charts --- ChangeLog | 1 + src/charts/horizontal_bar.php | 292 +++++++++++++++++++++ src/charts/line.php | 74 +++--- src/graph_autoload.php | 17 +- src/interfaces/horizontal_bar_renderer.php | 48 ++++ src/renderer/horizontal_bar.php | 177 +++++++++++++ ...ererTests_testRenderBasicHorizontalBarChart.svg | 2 + ...ts_testRenderHorizontalBarChartMultipleBars.svg | 2 + ..._testRenderHorizontalBarChartNegativeValues.svg | 2 + tests/horizontal_bar_chart_renderer.php | 96 +++++++ tests/suite.php | 2 + 11 files changed, 674 insertions(+), 39 deletions(-) create mode 100644 src/charts/horizontal_bar.php create mode 100644 src/interfaces/horizontal_bar_renderer.php create mode 100644 src/renderer/horizontal_bar.php create mode 100644 tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderBasicHorizontalBarChart.svg create mode 100644 tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartMultipleBars.svg create mode 100644 tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartNegativeValues.svg create mode 100644 tests/horizontal_bar_chart_renderer.php diff --git a/ChangeLog b/ChangeLog index c83358f..fc54b34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Implemented: #14294: Add option for axis label rotation +- Implemented: #13341: Vertical Bar Charts 1.4.3 - Monday 08 June 2009 diff --git a/src/charts/horizontal_bar.php b/src/charts/horizontal_bar.php new file mode 100644 index 0000000..7e4e4a0 --- /dev/null +++ b/src/charts/horizontal_bar.php @@ -0,0 +1,292 @@ + + * // Create a new horizontal bar chart + * $chart = new ezcGraphHorizontalBarChart(); + * + * // Add data to line chart + * $chart->data['sample dataset'] = new ezcGraphArrayDataSet( + * array( + * '100' => 1.2, + * '200' => 43.2, + * '300' => -34.14, + * '350' => 65, + * '400' => 123, + * ) + * ); + * + * // Render chart with the special designated renderer and default SVG driver + * $chart->renderer = new ezcGraphHorizontalRenderer(); + * $chart->render( 500, 200, 'bar_chart.svg' ); + * + * + * Each chart consists of several chart elements which represents logical + * parts of the chart and can be formatted independently. The bar chart + * consists of: + * - title ( {@link ezcGraphChartElementText} ) + * - legend ( {@link ezcGraphChartElementLegend} ) + * - background ( {@link ezcGraphChartElementBackground} ) + * - xAxis ( {@link ezcGraphChartElementLabeledAxis} ) + * - yAxis ( {@link ezcGraphChartElementNumericAxis} ) + * + * The type of the axis may be changed and all elements can be configured by + * accessing them as properties of the chart: + * + * + * $chart->legend->position = ezcGraph::RIGHT; + * + * + * The chart itself also offers several options to configure the appearance. As + * bar charts extend line charts the the extended configure options are + * available in {@link ezcGraphLineChartOptions} extending the + * {@link ezcGraphChartOptions}. + * + * @property ezcGraphLineChartOptions $options + * Chart options class + * + * @version //autogentag// + * @package Graph + * @mainclass + */ +class ezcGraphHorizontalBarChart extends ezcGraphBarChart +{ + /** + * Constructor + * + * @param array $options Default option array + * @return void + * @ignore + */ + public function __construct( array $options = array() ) + { + parent::__construct(); + + $this->addElement( 'xAxis', new ezcGraphChartElementNumericAxis() ); + $this->elements['xAxis']->axisLabelRenderer = new ezcGraphAxisCenteredLabelRenderer(); + $this->elements['xAxis']->position = ezcGraph::LEFT; + + $this->addElement( 'yAxis', new ezcGraphChartElementLabeledAxis() ); + $this->elements['yAxis']->axisLabelRenderer = new ezcGraphAxisBoxedLabelRenderer(); + $this->elements['yAxis']->position = ezcGraph::BOTTOM; + + $this->renderer = new ezcGraphHorizontalRenderer(); + } + + /** + * Render the assigned data + * + * Will renderer all charts data in the remaining boundings after drawing + * all other chart elements. The data will be rendered depending on the + * settings in the dataset. + * + * @param ezcGraphRenderer $renderer Renderer + * @param ezcGraphBoundings $boundings Remaining boundings + * @return void + */ + protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings, ezcGraphBoundings $innerBoundings ) + { + // Use inner boundings for drawning chart data + $boundings = $innerBoundings; + + $yAxisNullPosition = $this->elements['xAxis']->getCoordinate( false ); + + // Initialize counters + $nr = array(); + $count = array(); + + foreach ( $this->data as $data ) + { + if ( !isset( $nr[$data->displayType->default] ) ) + { + $nr[$data->displayType->default] = 0; + $count[$data->displayType->default] = 0; + } + + $nr[$data->displayType->default]++; + $count[$data->displayType->default]++; + } + + $checkedRegularSteps = false; + + // Display data + foreach ( $this->data as $datasetName => $data ) + { + --$nr[$data->displayType->default]; + + // Check which axis should be used + $xAxis = ( $data->xAxis->default ? $data->xAxis->default: $this->elements['xAxis'] ); + $yAxis = ( $data->yAxis->default ? $data->yAxis->default: $this->elements['yAxis'] ); + + // Determine fill color for dataset + if ( $this->options->fillLines !== false ) + { + $fillColor = clone $data->color->default; + $fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) ); + } + else + { + $fillColor = null; + } + + // Ensure regular steps on axis when used with bar charts and + // precalculate some values use to render bar charts + // + // Called only once and only when bars should be rendered + if ( ( $checkedRegularSteps === false ) && + ( $data->displayType->default === ezcGraph::BAR ) ) + { + $height = $this->calculateStepWidth( $yAxis, $xAxis, $boundings->height )->y; + } + + // Draw lines for dataset + $lastPoint = false; + foreach ( $data as $key => $value ) + { + // Calculate point in chart + $point = $xAxis->axisLabelRenderer->modifyChartDataPosition( + $yAxis->axisLabelRenderer->modifyChartDataPosition( + new ezcGraphCoordinate( + $xAxis->getCoordinate( $value ), + $yAxis->getCoordinate( $key ) + ) + ) + ); + + // Render depending on display type of dataset + switch ( true ) + { + case $data->displayType->default === ezcGraph::BAR: + $renderer->drawHorizontalBar( + $boundings, + new ezcGraphContext( $datasetName, $key, $data->url[$key] ), + $data->color[$key], + $point, + $height, + $nr[$data->displayType->default], + $count[$data->displayType->default], + $data->symbol[$key], + $yAxisNullPosition + ); + + // Render highlight string if requested + if ( $data->highlight[$key] ) + { + $renderer->drawDataHighlightText( + $boundings, + new ezcGraphContext( $datasetName, $key, $data->url[$key] ), + $point, + $yAxisNullPosition, + $nr[$data->displayType->default], + $count[$data->displayType->default], + $this->options->highlightFont, + ( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ), + $this->options->highlightSize + $this->options->highlightFont->padding * 2, + ( $this->options->highlightLines ? $data->color[$key] : null ), + ( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ), + ( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ), + $height, + $data->displayType->default + ); + } + break; + default: + throw new ezcGraphInvalidDisplayTypeException( $data->displayType->default ); + break; + } + + // Store last point, used to connect lines in line chart. + $lastPoint = $point; + } + } + } + + /** + * Aggregate and calculate value boundings on axis. + * + * This function is nearly the same as in ezcGraphLineChart, but reverses + * the usage of keys and values for the axis. + * + * @return void + */ + protected function setAxisValues() + { + // Virtual data set build for agrregated values sums for bar charts + $virtualBarSumDataSet = array( array(), array() ); + + // Calculate axis scaling and labeling + foreach ( $this->data as $dataset ) + { + $nr = 0; + $labels = array(); + $values = array(); + foreach ( $dataset as $label => $value ) + { + $labels[] = $label; + $values[] = $value; + + // Build sum of all bars + if ( $this->options->stackBars && + ( $dataset->displayType->default === ezcGraph::BAR ) ) + { + if ( !isset( $virtualBarSumDataSet[(int) $value >= 0][$nr] ) ) + { + $virtualBarSumDataSet[(int) $value >= 0][$nr++] = $value; + } + else + { + $virtualBarSumDataSet[(int) $value >= 0][$nr++] += $value; + } + } + } + + // Check if data has been associated with another custom axis, use + // default axis otherwise. + if ( $dataset->xAxis->default ) + { + $dataset->xAxis->default->addData( $values ); + } + else + { + $this->elements['xAxis']->addData( $values ); + } + + if ( $dataset->yAxis->default ) + { + $dataset->yAxis->default->addData( array_reverse( $labels ) ); + } + else + { + $this->elements['yAxis']->addData( array_reverse( $labels ) ); + } + } + + // There should always be something assigned to the main x and y axis. + if ( !$this->elements['xAxis']->initialized || + !$this->elements['yAxis']->initialized ) + { + throw new ezcGraphNoDataException(); + } + + // Calculate boundings from assigned data + $this->elements['xAxis']->calculateAxisBoundings(); + $this->elements['yAxis']->calculateAxisBoundings(); + } +} +?> diff --git a/src/charts/line.php b/src/charts/line.php index 3348920..4c0ba14 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -174,6 +174,47 @@ class ezcGraphLineChart extends ezcGraphChart } /** + * Calculate bar chart step width + * + * @return void + */ + protected function calculateStepWidth( ezcGraphChartElementAxis $mainAxis, ezcGraphChartElementAxis $secondAxis, $width ) + { + $steps = $mainAxis->getSteps(); + + $stepWidth = null; + foreach ( $steps as $step ) + { + if ( $stepWidth === null ) + { + $stepWidth = $step->width; + } + elseif ( $step->width !== $stepWidth ) + { + throw new ezcGraphUnregularStepsException(); + } + } + + $step = reset( $steps ); + if ( count( $step->childs ) ) + { + // Keep this for BC reasons + $barCount = ( $mainAxis->getMajorStepCount() + 1 ) * ( $mainAxis->getMinorStepCount() - 1 ); + $stepWidth = 1 / $barCount; + } + + $checkedRegularSteps = true; + return $mainAxis->axisLabelRenderer->modifyChartDataPosition( + $secondAxis->axisLabelRenderer->modifyChartDataPosition( + new ezcGraphCoordinate( + $width * $stepWidth, + $width * $stepWidth + ) + ) + ); + } + + /** * Render the assigned data * * Will renderer all charts data in the remaining boundings after drawing @@ -236,38 +277,7 @@ class ezcGraphLineChart extends ezcGraphChart if ( ( $checkedRegularSteps === false ) && ( $data->displayType->default === ezcGraph::BAR ) ) { - $steps = $xAxis->getSteps(); - - $stepWidth = null; - foreach ( $steps as $step ) - { - if ( $stepWidth === null ) - { - $stepWidth = $step->width; - } - elseif ( $step->width !== $stepWidth ) - { - throw new ezcGraphUnregularStepsException(); - } - } - - $step = reset( $steps ); - if ( count( $step->childs ) ) - { - // Keep this for BC reasons - $barCount = ( $xAxis->getMajorStepCount() + 1 ) * ( $xAxis->getMinorStepCount() - 1 ); - $stepWidth = 1 / $barCount; - } - - $checkedRegularSteps = true; - $width = $xAxis->axisLabelRenderer->modifyChartDataPosition( - $yAxis->axisLabelRenderer->modifyChartDataPosition( - new ezcGraphCoordinate( - ( $boundings->x1 - $boundings->x0 ) * $stepWidth, - 0 - ) - ) - )->x; + $width = $this->calculateStepWidth( $xAxis, $yAxis, $boundings->width )->x; } // Draw lines for dataset diff --git a/src/graph_autoload.php b/src/graph_autoload.php index d1e2e61..c66e1a2 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -40,8 +40,14 @@ return array( 'ezcGraphChart' => 'Graph/interfaces/chart.php', 'ezcGraphChartElement' => 'Graph/interfaces/element.php', 'ezcGraphChartOptions' => 'Graph/options/chart.php', + 'ezcGraphLineChart' => 'Graph/charts/line.php', 'ezcGraphMatrix' => 'Graph/math/matrix.php', + 'ezcGraphOdometerRenderer' => 'Graph/interfaces/odometer_renderer.php', + 'ezcGraphRadarRenderer' => 'Graph/interfaces/radar_renderer.php', + 'ezcGraphRenderer' => 'Graph/interfaces/renderer.php', + 'ezcGraphStackedBarsRenderer' => 'Graph/interfaces/stacked_bar_renderer.php', 'ezcGraphAxisLabelRenderer' => 'Graph/interfaces/axis_label_renderer.php', + 'ezcGraphBarChart' => 'Graph/charts/bar.php', 'ezcGraphChartDataContainer' => 'Graph/data_container/base.php', 'ezcGraphChartElementAxis' => 'Graph/element/axis.php', 'ezcGraphColor' => 'Graph/colors/color.php', @@ -50,13 +56,10 @@ return array( 'ezcGraphDataSetProperty' => 'Graph/interfaces/dataset_property.php', 'ezcGraphDriver' => 'Graph/interfaces/driver.php', 'ezcGraphDriverOptions' => 'Graph/options/driver.php', - 'ezcGraphLineChart' => 'Graph/charts/line.php', - 'ezcGraphOdometerRenderer' => 'Graph/interfaces/odometer_renderer.php', + 'ezcGraphHorizontalBarRenderer' => 'Graph/interfaces/horizontal_bar_renderer.php', 'ezcGraphPalette' => 'Graph/interfaces/palette.php', - 'ezcGraphRadarRenderer' => 'Graph/interfaces/radar_renderer.php', - 'ezcGraphRenderer' => 'Graph/interfaces/renderer.php', + 'ezcGraphRenderer2d' => 'Graph/renderer/2d.php', 'ezcGraphRendererOptions' => 'Graph/options/renderer.php', - 'ezcGraphStackedBarsRenderer' => 'Graph/interfaces/stacked_bar_renderer.php', 'ezcGraphTransformation' => 'Graph/math/transformation.php', 'ezcGraph' => 'Graph/graph.php', 'ezcGraphArrayDataSet' => 'Graph/datasets/array.php', @@ -68,7 +71,6 @@ return array( 'ezcGraphAxisRadarLabelRenderer' => 'Graph/renderer/axis_label_radar.php', 'ezcGraphAxisRotatedLabelRenderer' => 'Graph/renderer/axis_label_rotated.php', 'ezcGraphAxisStep' => 'Graph/structs/step.php', - 'ezcGraphBarChart' => 'Graph/charts/bar.php', 'ezcGraphBoundings' => 'Graph/math/boundings.php', 'ezcGraphCairoDriver' => 'Graph/driver/cairo.php', 'ezcGraphCairoDriverOptions' => 'Graph/options/cairo_driver.php', @@ -92,6 +94,8 @@ return array( 'ezcGraphFontOptions' => 'Graph/options/font.php', 'ezcGraphGdDriver' => 'Graph/driver/gd.php', 'ezcGraphGdDriverOptions' => 'Graph/options/gd_driver.php', + 'ezcGraphHorizontalBarChart' => 'Graph/charts/horizontal_bar.php', + 'ezcGraphHorizontalRenderer' => 'Graph/renderer/horizontal_bar.php', 'ezcGraphLineChartOptions' => 'Graph/options/line_chart.php', 'ezcGraphLinearGradient' => 'Graph/colors/linear_gradient.php', 'ezcGraphNumericDataSet' => 'Graph/datasets/numeric.php', @@ -109,7 +113,6 @@ return array( 'ezcGraphRadarChart' => 'Graph/charts/radar.php', 'ezcGraphRadarChartOptions' => 'Graph/options/radar_chart.php', 'ezcGraphRadialGradient' => 'Graph/colors/radial_gradient.php', - 'ezcGraphRenderer2d' => 'Graph/renderer/2d.php', 'ezcGraphRenderer2dOptions' => 'Graph/options/renderer_2d.php', 'ezcGraphRenderer3d' => 'Graph/renderer/3d.php', 'ezcGraphRenderer3dOptions' => 'Graph/options/renderer_3d.php', diff --git a/src/interfaces/horizontal_bar_renderer.php b/src/interfaces/horizontal_bar_renderer.php new file mode 100644 index 0000000..e6e937f --- /dev/null +++ b/src/interfaces/horizontal_bar_renderer.php @@ -0,0 +1,48 @@ + diff --git a/src/renderer/horizontal_bar.php b/src/renderer/horizontal_bar.php new file mode 100644 index 0000000..7448741 --- /dev/null +++ b/src/renderer/horizontal_bar.php @@ -0,0 +1,177 @@ + + * @TODO: Add example + * + * + * @version //autogentag// + * @package Graph + * @mainclass + */ +class ezcGraphHorizontalRenderer + extends + ezcGraphRenderer2d + implements + ezcGraphHorizontalBarRenderer +{ + /** + * Draw horizontal bar + * + * Draws a horizontal bar as a data element in a line chart + * + * @param ezcGraphBoundings $boundings Chart boundings + * @param ezcGraphContext $context Context of call + * @param ezcGraphColor $color Color of line + * @param ezcGraphCoordinate $position Position of data point + * @param float $stepSize Space which can be used for bars + * @param int $dataNumber Number of dataset + * @param int $dataCount Count of datasets in chart + * @param int $symbol Symbol to draw for line + * @param float $axisPosition Position of axis for drawing filled lines + * @return void + */ + public function drawHorizontalBar( + ezcGraphBoundings $boundings, + ezcGraphContext $context, + ezcGraphColor $color, + ezcGraphCoordinate $position, + $stepSize, + $dataNumber = 1, + $dataCount = 1, + $symbol = ezcGraph::NO_SYMBOL, + $axisPosition = 0. ) + { + // Apply margin + $margin = $stepSize * $this->options->barMargin; + $padding = $stepSize * $this->options->barPadding; + $barHeight = ( $stepSize - $margin ) / $dataCount - $padding; + $offset = - $stepSize / 2 + $margin / 2 + ( $dataCount - $dataNumber - 1 ) * ( $padding + $barHeight ) + $padding / 2; + + $barPointArray = array( + new ezcGraphCoordinate( + $boundings->x0 + ( $boundings->width ) * $axisPosition, + $boundings->y0 + ( $boundings->height ) * $position->y + $offset + ), + new ezcGraphCoordinate( + $boundings->x0 + ( $boundings->width ) * $position->x, + $boundings->y0 + ( $boundings->height ) * $position->y + $offset + ), + new ezcGraphCoordinate( + $boundings->x0 + ( $boundings->width ) * $position->x, + $boundings->y0 + ( $boundings->height ) * $position->y + $offset + $barHeight + ), + new ezcGraphCoordinate( + $boundings->x0 + ( $boundings->width ) * $axisPosition, + $boundings->y0 + ( $boundings->height ) * $position->y + $offset + $barHeight + ), + ); + + $this->addElementReference( + $context, + $this->driver->drawPolygon( + $barPointArray, + $color, + true + ) + ); + + if ( $this->options->dataBorder > 0 ) + { + $darkened = $color->darken( $this->options->dataBorder ); + $this->driver->drawPolygon( + $barPointArray, + $darkened, + false, + 1 + ); + } + } + + /** + * Draw bar + * + * Draws a bar as a data element in a line chart + * + * @param ezcGraphBoundings $boundings Chart boundings + * @param ezcGraphContext $context Context of call + * @param ezcGraphColor $color Color of line + * @param ezcGraphCoordinate $position Position of data point + * @param float $stepSize Space which can be used for bars + * @param int $dataNumber Number of dataset + * @param int $dataCount Count of datasets in chart + * @param int $symbol Symbol to draw for line + * @param float $axisPosition Position of axis for drawing filled lines + * @return void + */ + public function drawBar( + ezcGraphBoundings $boundings, + ezcGraphContext $context, + ezcGraphColor $color, + ezcGraphCoordinate $position, + $stepSize, + $dataNumber = 1, + $dataCount = 1, + $symbol = ezcGraph::NO_SYMBOL, + $axisPosition = 0. ) + { + throw new ezcBaseFunctionalityNotSupportedException( + "A normal bar chart", + "Only horizontal bar charts can be renderered with the ezcGraphHorizontalRenderer" + ); + } + + /** + * Draw data line + * + * Draws a line as a data element in a line chart + * + * @param ezcGraphBoundings $boundings Chart boundings + * @param ezcGraphContext $context Context of call + * @param ezcGraphColor $color Color of line + * @param ezcGraphCoordinate $start Starting point + * @param ezcGraphCoordinate $end Ending point + * @param int $dataNumber Number of dataset + * @param int $dataCount Count of datasets in chart + * @param int $symbol Symbol to draw for line + * @param ezcGraphColor $symbolColor Color of the symbol, defaults to linecolor + * @param ezcGraphColor $fillColor Color to fill line with + * @param float $axisPosition Position of axis for drawing filled lines + * @param float $thickness Line thickness + * @return void + */ + public function drawDataLine( + ezcGraphBoundings $boundings, + ezcGraphContext $context, + ezcGraphColor $color, + ezcGraphCoordinate $start, + ezcGraphCoordinate $end, + $dataNumber = 1, + $dataCount = 1, + $symbol = ezcGraph::NO_SYMBOL, + ezcGraphColor $symbolColor = null, + ezcGraphColor $fillColor = null, + $axisPosition = 0., + $thickness = 1. ) + { + throw new ezcBaseFunctionalityNotSupportedException( + "A normal line chart", + "Only horizontal bar charts can be renderered with the ezcGraphHorizontalRenderer" + ); + } +} + +?> diff --git a/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderBasicHorizontalBarChart.svg b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderBasicHorizontalBarChart.svg new file mode 100644 index 0000000..ef3b7c3 --- /dev/null +++ b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderBasicHorizontalBarChart.svg @@ -0,0 +1,2 @@ + +Set 1100200300400sample5sample4sample3sample2sample1 diff --git a/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartMultipleBars.svg b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartMultipleBars.svg new file mode 100644 index 0000000..38465f8 --- /dev/null +++ b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartMultipleBars.svg @@ -0,0 +1,2 @@ + +Set 1Set 2250500750sample5sample4sample3sample2sample1 diff --git a/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartNegativeValues.svg b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartNegativeValues.svg new file mode 100644 index 0000000..8101a82 --- /dev/null +++ b/tests/data/compare/ezcGraphHorizontalBarRendererTests_testRenderHorizontalBarChartNegativeValues.svg @@ -0,0 +1,2 @@ + +Set 1Set 2-500-250250500750sample5sample4sample3sample2sample1 diff --git a/tests/horizontal_bar_chart_renderer.php b/tests/horizontal_bar_chart_renderer.php new file mode 100644 index 0000000..7353480 --- /dev/null +++ b/tests/horizontal_bar_chart_renderer.php @@ -0,0 +1,96 @@ +tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/'; + $this->basePath = dirname( __FILE__ ) . '/data/'; + } + + protected function tearDown() + { + if ( !$this->hasFailed() ) + { + $this->removeTempDir(); + } + } + + public function testRenderBasicHorizontalBarChart() + { + $filename = $this->tempDir . __FUNCTION__ . '.svg'; + + $chart = new ezcGraphHorizontalBarChart(); + + $chart->data['Set 1'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 151, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ) ); + + $chart->render( 500, 200, $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' + ); + } + + public function testRenderHorizontalBarChartMultipleBars() + { + $filename = $this->tempDir . __FUNCTION__ . '.svg'; + + $chart = new ezcGraphHorizontalBarChart(); + + $chart->data['Set 1'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => 151, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ) ); + $chart->data['Set 2'] = new ezcGraphArrayDataSet( array( 'sample 1' => 543, 'sample 2' => 234, 'sample 3' => 298, 'sample 4' => 5, 'sample 5' => 124) ); + + $chart->render( 500, 200, $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' + ); + } + + public function testRenderHorizontalBarChartNegativeValues() + { + $filename = $this->tempDir . __FUNCTION__ . '.svg'; + + $chart = new ezcGraphHorizontalBarChart(); + + $chart->data['Set 1'] = new ezcGraphArrayDataSet( array( 'sample 1' => 234, 'sample 2' => -151, 'sample 3' => -324, 'sample 4' => 120, 'sample 5' => 1 ) ); + $chart->data['Set 2'] = new ezcGraphArrayDataSet( array( 'sample 1' => 543, 'sample 2' => 234, 'sample 3' => -298, 'sample 4' => 5, 'sample 5' => -124) ); + + $chart->render( 500, 200, $filename ); + + $this->compare( + $filename, + $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.svg' + ); + } +} +?> diff --git a/tests/suite.php b/tests/suite.php index ff3c108..de5148f 100644 --- a/tests/suite.php +++ b/tests/suite.php @@ -49,6 +49,7 @@ require_once 'radar_chart.php'; require_once 'renderer_2d_test.php'; require_once 'renderer_2d_legacy_test.php'; require_once 'renderer_3d_test.php'; +require_once 'horizontal_bar_chart_renderer.php'; require_once 'struct_test.php'; require_once 'text_test.php'; require_once 'tools_test.php'; @@ -103,6 +104,7 @@ class ezcGraphSuite extends PHPUnit_Framework_TestSuite $this->addTest( ezcGraphRenderer2dTest::suite() ); $this->addTest( ezcGraphRenderer2dLegacyTest::suite() ); $this->addTest( ezcGraphRenderer3dTest::suite() ); + $this->addTest( ezcGraphHorizontalBarRendererTests::suite() ); $this->addTest( ezcGraphStructTest::suite() ); $this->addTest( ezcGraphSvgDriverTest::suite() ); $this->addTest( ezcGraphSvgSvgFontDriverTest::suite() ); -- cgit v1.1