summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2006-06-12 16:29:14 +0000
committerKore Nordmann <github@kore-nordmann.de>2006-06-12 16:29:14 +0000
commit4c71c431a983f0c1216981e2011e823d303848d7 (patch)
tree8fb31999ca1fb711e54c2eb8519f59116d9f05b6
parent3f1b5624719e557f8c4f7cb49f57d92fe3b37b4d (diff)
downloadzetacomponents-graph-4c71c431a983f0c1216981e2011e823d303848d7.zip
zetacomponents-graph-4c71c431a983f0c1216981e2011e823d303848d7.tar.gz
- Added tests and implementation for background images in charts
- Fixed bug with image sizes in gd driver
-rw-r--r--src/charts/line.php2
-rw-r--r--src/charts/pie.php3
-rw-r--r--src/driver/gd.php6
-rw-r--r--src/element/background.php128
-rw-r--r--src/graph_autoload.php1
-rw-r--r--src/options/chart.php24
-rw-r--r--tests/background_image_test.php185
-rw-r--r--tests/chart_test.php8
-rw-r--r--tests/driver_gd_test.php6
-rw-r--r--tests/suite.php2
10 files changed, 337 insertions, 28 deletions
diff --git a/src/charts/line.php b/src/charts/line.php
index a9509a9..c86ff29 100644
--- a/src/charts/line.php
+++ b/src/charts/line.php
@@ -162,8 +162,10 @@ class ezcGraphLineChart extends ezcGraphChart
// Render border and background
$boundings = $this->renderBorder( $boundings );
+ $boundings = $this->options->backgroundImage->render( $this->renderer, $boundings );
$boundings = $this->renderBackground( $boundings );
+ // Render subelements
foreach ( $this->elements as $name => $element )
{
// Special settings for special elements
diff --git a/src/charts/pie.php b/src/charts/pie.php
index e907b03..8645c08 100644
--- a/src/charts/pie.php
+++ b/src/charts/pie.php
@@ -200,15 +200,16 @@ class ezcGraphPieChart extends ezcGraphChart
$this->options->width = $width;
$this->options->height = $height;
- // Render subelements
$boundings = new ezcGraphBoundings();
$boundings->x1 = $this->options->width;
$boundings->y1 = $this->options->height;
// Render border and background
$boundings = $this->renderBorder( $boundings );
+ $boundings = $this->options->backgroundImage->render( $this->renderer, $boundings );
$boundings = $this->renderBackground( $boundings );
+ // Render subelements
foreach ( $this->elements as $name => $element )
{
$this->driver->options->font = $element->font;
diff --git a/src/driver/gd.php b/src/driver/gd.php
index 19064c2..6aa43f3 100644
--- a/src/driver/gd.php
+++ b/src/driver/gd.php
@@ -108,7 +108,7 @@ class ezcGraphGdDriver extends ezcGraphDriver
'image' => imagecreatefrompng( $file )
);
default:
- throw new ezcGraphGdDriverUnsupportedImageFormatException( $data[2] );
+ throw new ezcGraphGdDriverUnsupportedImageTypeException( $data[2] );
}
}
@@ -601,8 +601,8 @@ class ezcGraphGdDriver extends ezcGraphDriver
$this->supersample( $position->y ),
0,
0,
- $this->supersample( $position->x + $width ),
- $this->supersample( $position->y + $height ),
+ $this->supersample( $width ),
+ $this->supersample( $height ),
$imageFile['width'], $imageFile['height']
);
}
diff --git a/src/element/background.php b/src/element/background.php
new file mode 100644
index 0000000..a1a4f19
--- /dev/null
+++ b/src/element/background.php
@@ -0,0 +1,128 @@
+<?php
+/**
+ * File containing the abstract ezcGraphChartElementText class
+ *
+ * @package Graph
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ * Class to represent a legend as a chart element
+ *
+ * @package Graph
+ */
+class ezcGraphChartElementBackgroundImage extends ezcGraphChartElement
+{
+
+ /**
+ * Filename of the file to use for background
+ *
+ * @var string
+ */
+ protected $source = '';
+
+ /**
+ * __set
+ *
+ * @param mixed $propertyName
+ * @param mixed $propertyValue
+ * @throws ezcBaseValueException
+ * If a submitted parameter was out of range or type.
+ * @throws ezcBasePropertyNotFoundException
+ * If a the value for the property options is not an instance of
+ * @return void
+ */
+ public function __set( $propertyName, $propertyValue )
+ {
+ switch ( $propertyName )
+ {
+ case 'source':
+ // Check for existance of file
+ if ( !is_file( $propertyValue ) || !is_readable( $propertyValue ) )
+ {
+ throw new ezcBaseFileNotFoundException( $propertyValue );
+ }
+
+ // Check for beeing an image file
+ $data = getImageSize( $propertyValue );
+ if ( $data === false )
+ {
+ throw new ezcGraphInvalidImageFileException( $propertyValue );
+ }
+
+ // SWF files are useless..
+ if ( $data[2] === 4 )
+ {
+ throw new ezcGraphInvalidImageFileException( 'We cant use SWF files like <' . $propertyValue . '>.' );
+ }
+
+ $this->source = $propertyValue;
+ break;
+ case 'position':
+ $this->position = (int) $propertyValue;
+ break;
+ default:
+ return parent::__set( $propertyName, $propertyValue );
+ }
+ }
+
+ /**
+ * Render a legend
+ *
+ * @param ezcGraphRenderer $renderer
+ * @access public
+ * @return void
+ */
+ public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings )
+ {
+ if ( empty( $this->source ) )
+ {
+ return $boundings;
+ }
+
+ // Get background image boundings
+ $data = getimagesize( $this->source );
+
+ // Determine x position
+ switch ( true )
+ {
+ case ( $this->position & ezcGraph::LEFT ):
+ $xPosition = 0;
+ break;
+ case ( $this->position & ezcGraph::RIGHT ):
+ $xPosition = $boundings->x1 - $data[0];
+ break;
+ case ( $this->position & ezcGraph::CENTER ):
+ default:
+ $xPosition = (int) round( ( $boundings->x1 - $data[0] ) / 2 );
+ break;
+ }
+
+ // Determine y position
+ switch ( true )
+ {
+ case ( $this->position & ezcGraph::TOP ):
+ $yPosition = 0;
+ break;
+ case ( $this->position & ezcGraph::BOTTOM ):
+ $yPosition = $boundings->y1 - $data[1];
+ break;
+ case ( $this->position & ezcGraph::MIDDLE ):
+ default:
+ $yPosition = (int) round( ( $boundings->y1 - $data[1] ) / 2 );
+ break;
+ }
+
+ $renderer->drawBackgroundImage(
+ $this->source,
+ new ezcGraphCoordinate( $xPosition, $yPosition ),
+ $data[0],
+ $data[1]
+ );
+
+ return $boundings;
+ }
+}
+
+?>
diff --git a/src/graph_autoload.php b/src/graph_autoload.php
index 658bcec..525f7d9 100644
--- a/src/graph_autoload.php
+++ b/src/graph_autoload.php
@@ -47,6 +47,7 @@ return array(
'ezcGraphFontOptions' => 'Graph/options/font.php',
'ezcGraphChartElementText' => 'Graph/element/text.php',
'ezcGraphChartElementLegend' => 'Graph/element/legend.php',
+ 'ezcGraphChartElementBackgroundImage' => 'Graph/element/background.php',
'ezcGraphChartElementAxis' => 'Graph/element/axis.php',
'ezcGraphChartElementNumericAxis' => 'Graph/axis/numeric.php',
'ezcGraphChartElementLabeledAxis' => 'Graph/axis/labeled.php',
diff --git a/src/options/chart.php b/src/options/chart.php
index ada1f77..ccbb6c0 100644
--- a/src/options/chart.php
+++ b/src/options/chart.php
@@ -75,6 +75,9 @@ class ezcGraphChartOptions extends ezcBaseOptions
{
$this->font = new ezcGraphFontOptions();
+ $this->backgroundImage = new ezcGraphChartElementBackgroundImage();
+ $this->backgroundImage->position = ezcGraph::CENTER | ezcGraph::MIDDLE;
+
parent::__construct( $options );
}
@@ -101,26 +104,7 @@ class ezcGraphChartOptions extends ezcBaseOptions
$this->padding = max( 0, (int) $propertyValue );
break;
case 'backgroundImage':
- // Check for existance of file
- if ( !is_file( $propertyValue ) || !is_readable( $propertyValue ) )
- {
- throw new ezcBaseFileNotFoundException( $propertyValue );
- }
-
- // Check for beeing an image file
- $data = getImageSize( $propertyValue );
- if ( $data === false )
- {
- throw new ezcGraphInvalidImageFileException( $propertyValue );
- }
-
- // SWF files are useless..
- if ( $data[2] === 4 )
- {
- throw new ezcGraphInvalidImageFileException( 'We cant use SWF files like <' . $propertyValue . '>.' );
- }
-
- $this->backgroundImage = $propertyValue;
+ $this->backgroundImage->source = $propertyValue;
break;
case 'background':
$this->background = ezcGraphColor::create( $propertyValue );
diff --git a/tests/background_image_test.php b/tests/background_image_test.php
new file mode 100644
index 0000000..bee5451
--- /dev/null
+++ b/tests/background_image_test.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * ezcGraphBackgroundImageTest
+ *
+ * @package Graph
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Tests for ezcGraph class.
+ *
+ * @package ImageAnalysis
+ * @subpackage Tests
+ */
+class ezcGraphBackgroundImageTest extends ezcTestCase
+{
+
+ protected $testFiles = array(
+ 'png' => 'png.png',
+ );
+
+ protected $basePath;
+
+ protected $tempDir;
+
+ public static function suite()
+ {
+ return new ezcTestSuite( "ezcGraphBackgroundImageTest" );
+ }
+
+ /**
+ * setUp
+ *
+ * @access public
+ */
+ public function setUp()
+ {
+ static $i = 0;
+ $this->tempDir = $this->createTempDir( 'ezcGraphGdDriverTest' . sprintf( '_%03d_', ++$i ) ) . '/';
+ $this->basePath = dirname( __FILE__ ) . '/data/';
+ }
+
+ /**
+ * tearDown
+ *
+ * @access public
+ */
+ public function tearDown()
+ {
+ $this->removeTempDir();
+ }
+
+ public function testRenderStandard()
+ {
+ $chart = ezcGraph::create( 'line' );
+ $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+ $chart->options->backgroundImage = $this->basePath . $this->testFiles['png'];
+ $chart->options->background = '#000000FF';
+
+ $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+ 'drawBackgroundImage',
+ ) );
+
+ $mockedRenderer
+ ->expects( $this->at( 0 ) )
+ ->method( 'drawBackgroundImage' )
+ ->with(
+ $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+ $this->equalTo( new ezcGraphCoordinate( 162, 50 ) ),
+ $this->equalTo( 177 ),
+ $this->equalTo( 100 )
+ );
+
+ $chart->renderer = $mockedRenderer;
+ $chart->render( 500, 200 );
+ }
+
+ public function testRenderPieBottomRight()
+ {
+ $chart = ezcGraph::create( 'pie' );
+ $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+ $chart->options->backgroundImage = $this->basePath . $this->testFiles['png'];
+ $chart->options->backgroundImage->position = ezcGraph::BOTTOM | ezcGraph::RIGHT;
+ $chart->options->background = '#000000FF';
+
+ $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+ 'drawBackgroundImage',
+ ) );
+
+ $mockedRenderer
+ ->expects( $this->at( 0 ) )
+ ->method( 'drawBackgroundImage' )
+ ->with(
+ $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+ $this->equalTo( new ezcGraphCoordinate( 323, 100 ) ),
+ $this->equalTo( 177 ),
+ $this->equalTo( 100 )
+ );
+
+ $chart->renderer = $mockedRenderer;
+ $chart->render( 500, 200 );
+ }
+
+ public function testRenderTop()
+ {
+ $chart = ezcGraph::create( 'line' );
+ $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+ $chart->options->backgroundImage = $this->basePath . $this->testFiles['png'];
+ $chart->options->backgroundImage->position = ezcGraph::TOP;
+ $chart->options->background = '#000000FF';
+
+ $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+ 'drawBackgroundImage',
+ ) );
+
+ $mockedRenderer
+ ->expects( $this->at( 0 ) )
+ ->method( 'drawBackgroundImage' )
+ ->with(
+ $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+ $this->equalTo( new ezcGraphCoordinate( 162, 0 ) ),
+ $this->equalTo( 177 ),
+ $this->equalTo( 100 )
+ );
+
+ $chart->renderer = $mockedRenderer;
+ $chart->render( 500, 200 );
+ }
+
+ public function testRenderLeft()
+ {
+ $chart = ezcGraph::create( 'line' );
+ $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+ $chart->options->backgroundImage = $this->basePath . $this->testFiles['png'];
+ $chart->options->backgroundImage->position = ezcGraph::LEFT;
+ $chart->options->background = '#000000FF';
+
+ $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+ 'drawBackgroundImage',
+ ) );
+
+ $mockedRenderer
+ ->expects( $this->at( 0 ) )
+ ->method( 'drawBackgroundImage' )
+ ->with(
+ $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+ $this->equalTo( new ezcGraphCoordinate( 0, 50 ) ),
+ $this->equalTo( 177 ),
+ $this->equalTo( 100 )
+ );
+
+ $chart->renderer = $mockedRenderer;
+ $chart->render( 500, 200 );
+ }
+
+ public function testRenderWithTransparentBackground()
+ {
+ $filename = $this->tempDir . __FUNCTION__ . '.png';
+
+ $chart = ezcGraph::create( 'line' );
+ $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+ $chart->palette = 'Black';
+ $chart->options->backgroundImage = $this->basePath . $this->testFiles['png'];
+ $chart->options->background = '#2E343655';
+
+ $chart->driver = new ezcGraphGdDriver();
+ $chart->options->font = $this->basePath . 'font.ttf';
+ $chart->render( 500, 200, $filename );
+
+ $this->assertTrue(
+ file_exists( $filename ),
+ 'No image was generated.'
+ );
+
+ $this->assertEquals(
+ '17c3aac43de88a0a8ce620d5e72db40d',
+ md5_file( $filename ),
+ 'Incorrect image rendered.'
+ );
+ }
+}
+?>
diff --git a/tests/chart_test.php b/tests/chart_test.php
index 2b8d8ce..ad00ecd 100644
--- a/tests/chart_test.php
+++ b/tests/chart_test.php
@@ -69,7 +69,13 @@ class ezcGraphChartTest extends ezcTestCase
$pieChart = ezcGraph::create( 'Pie' );
$pieChart->options->backgroundImage = $this->basePath . $this->testFiles['jpeg'];
- $this->assertProtectedPropertySame( $pieChart->options, 'backgroundImage', $this->basePath . $this->testFiles['jpeg'] );
+ $background = $this->getNonPublicProperty( $pieChart->options, 'backgroundImage' );
+ $this->assertTrue(
+ $background instanceof ezcGraphChartElementBackgroundImage,
+ 'Background is not an ezcGraphChartElementBackgroundImage.'
+ );
+
+ $this->assertProtectedPropertySame( $background, 'source', $this->basePath . $this->testFiles['jpeg'] );
}
public function testSetOptionsInvalidBackgroundImage()
diff --git a/tests/driver_gd_test.php b/tests/driver_gd_test.php
index b6f2005..2e0552b 100644
--- a/tests/driver_gd_test.php
+++ b/tests/driver_gd_test.php
@@ -436,7 +436,7 @@ class ezcGraphGdDriverTest extends ezcTestCase
);
$this->assertEquals(
- '2c727bfabf917c5afa5144e63a6bf3c2',
+ '1f2603bbfd75fdc9cd325afd12398505',
md5_file( $filename ),
'Incorrect image rendered.'
);
@@ -461,7 +461,7 @@ class ezcGraphGdDriverTest extends ezcTestCase
);
$this->assertEquals(
- 'a384e438b0853ccaaacac94fb7977f2a',
+ 'dc436ebc6ceccaaac09562c7287938ae',
md5_file( $filename ),
'Incorrect image rendered.'
);
@@ -1053,7 +1053,7 @@ class ezcGraphGdDriverTest extends ezcTestCase
);
$this->assertEquals(
- 'ca9fec349aab02d6518406fa02a656c9',
+ '7de0f967001b35b6726b4f8f9dd2e7ae',
md5_file( $filename ),
'Incorrect image rendered.'
);
diff --git a/tests/suite.php b/tests/suite.php
index 9c17002..0cd4e11 100644
--- a/tests/suite.php
+++ b/tests/suite.php
@@ -30,6 +30,7 @@ require_once 'renderer_2d_test.php';
require_once 'driver_gd_test.php';
require_once 'font_test.php';
require_once 'palette_test.php';
+require_once 'background_image_test.php';
/**
* Test suite for ImageAnalysis package.
@@ -58,6 +59,7 @@ class ezcGraphSuite extends ezcTestSuite
$this->addTest( ezcGraphFontTest::suite() );
$this->addTest( ezcGraphTextTest::suite() );
$this->addTest( ezcGraphPaletteTest::suite() );
+ $this->addTest( ezcGraphBackgroundImageTest::suite() );
}
public static function suite()
OpenPOWER on IntegriCloud