Minor tweaks to charts, unit testing for charts, and pdf examples

This commit is contained in:
Mark Baker 2012-06-24 13:34:57 +01:00
parent d700807ed0
commit 6285555c52
9 changed files with 364 additions and 37 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
unitTests/codeCoverage

View File

@ -36,6 +36,14 @@
class PHPExcel_Chart_DataSeriesValues
{
const DATASERIES_TYPE_STRING = 'String';
const DATASERIES_TYPE_NUMBER = 'Number';
private static $_dataTypeValues = array(
self::DATASERIES_TYPE_STRING,
self::DATASERIES_TYPE_NUMBER,
);
/**
* Series Data Type
*
@ -81,9 +89,9 @@ class PHPExcel_Chart_DataSeriesValues
/**
* Create a new PHPExcel_Chart_DataSeriesValues object
*/
public function __construct($dataType = null, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = array(), $marker = null)
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = array(), $marker = null)
{
$this->_dataType = $dataType;
$this->setDataType($dataType);
$this->_dataSource = $dataSource;
$this->_formatCode = $formatCode;
$this->_pointCount = $pointCount;
@ -103,10 +111,18 @@ class PHPExcel_Chart_DataSeriesValues
/**
* Set Series Data Type
*
* @param string $dataType
* @param string $dataType Datatype of this data series
* Typical values are:
* PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_STRING
* Normally used for axis point values
* PHPExcel_Chart_DataSeriesValues::DATASERIES_TYPE_NUMBER
* Normally used for chart data values
* @return PHPExcel_Chart_DataSeriesValues
*/
public function setDataType($dataType = 'Number') {
public function setDataType($dataType = self::DATASERIES_TYPE_NUMBER) {
if (!in_array($dataType, self::$_dataTypeValues)) {
throw new PHPExcel_Chart_Exception('Invalid datatype for chart data series values');
}
$this->_dataType = $dataType;
return $this;

View File

@ -0,0 +1,52 @@
<?php
/**
* PHPExcel
*
* Copyright (c) 2006 - 2012 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel_Chart
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/**
* PHPExcel_Chart_Exception
*
* @category PHPExcel
* @package PHPExcel_Chart
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Chart_Exception extends Exception {
/**
* Error handler callback
*
* @param mixed $code
* @param mixed $string
* @param mixed $file
* @param mixed $line
* @param mixed $context
*/
public static function errorHandlerCallback($code, $string, $file, $line, $context) {
$e = new self($string, $code);
$e->line = $line;
$e->file = $file;
throw $e;
}
}

View File

@ -49,7 +49,7 @@ class PHPExcel_Chart_Legend
const POSITION_TOP = 't';
const POSITION_TOPRIGHT = 'tr';
private static $_positionXref = array( self::xlLegendPositionBottom => self::POSITION_BOTTOM,
private static $_positionXLref = array( self::xlLegendPositionBottom => self::POSITION_BOTTOM,
self::xlLegendPositionCorner => self::POSITION_TOPRIGHT,
self::xlLegendPositionCustom => '??',
self::xlLegendPositionLeft => self::POSITION_LEFT,
@ -69,24 +69,24 @@ class PHPExcel_Chart_Legend
*
* @var boolean
*/
private $_overlay = true;
private $_overlay = TRUE;
/**
* Legend Layout
*
* @var PHPExcel_Chart_Layout
*/
private $_layout = null;
private $_layout = NULL;
/**
* Create a new PHPExcel_Chart_Legend
*/
public function __construct($position = null, PHPExcel_Chart_Layout $layout = null, $overlay= false)
public function __construct($position = self::POSITION_RIGHT, PHPExcel_Chart_Layout $layout = NULL, $overlay = FALSE)
{
$this->_position = $position;
$this->setPosition($position);
$this->_layout = $layout;
$this->_overlay = $overlay;
$this->setOverlay($overlay);
}
/**
@ -104,12 +104,11 @@ class PHPExcel_Chart_Legend
* @param string $position
*/
public function setPosition($position = self::POSITION_RIGHT) {
if (!in_array($position,self::$positionXref)) {
if (!in_array($position,self::$_positionXLref)) {
return false;
}
$this->_position = $position;
return true;
}
@ -119,7 +118,7 @@ class PHPExcel_Chart_Legend
* @return number
*/
public function getPositionXL() {
return array_search($this->_position,self::$positionXref);
return array_search($this->_position,self::$_positionXLref);
}
/**
@ -128,11 +127,11 @@ class PHPExcel_Chart_Legend
* @param number $positionXL
*/
public function setPositionXL($positionXL = self::xlLegendPositionRight) {
if (!array_key_exists($positionXL,self::$positionXref)) {
if (!array_key_exists($positionXL,self::$_positionXLref)) {
return false;
}
$this->_position = self::$positionXref[$positionXL];
$this->_position = self::$_positionXLref[$positionXL];
return true;
}
@ -148,10 +147,16 @@ class PHPExcel_Chart_Legend
/**
* Set allow overlay of other elements?
*
* @param boolean $value
* @param boolean $overlay
* @return boolean
*/
public function setOverlay($value=false) {
$this->_overlay = $value;
public function setOverlay($overlay = FALSE) {
if (!is_bool($overlay)) {
return false;
}
$this->_overlay = $overlay;
return true;
}
/**

View File

@ -34,6 +34,17 @@ date_default_timezone_set('Europe/London');
require_once '../Classes/PHPExcel.php';
// Change these values to select the PDF Rendering library that you wish to use
// and its directory location on your server
//$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
//$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
//$rendererLibrary = 'tcPDF5.9';
$rendererLibrary = 'mPDF5.4';
//$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $rendererLibrary;
// Read from Excel2007 (.xlsx) template
echo date('H:i:s') , " Load Excel2007 template file" , PHP_EOL;
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
@ -61,9 +72,24 @@ echo date('H:i:s') , " File written to " , str_replace('.php', '.htm', __FILE__)
// Export to PDF (.pdf)
echo date('H:i:s') , " Write to PDF format" , PHP_EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save(str_replace('.php', '.pdf', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.pdf', __FILE__) , PHP_EOL;
try {
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
echo (
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
'at the top of this script as appropriate for your directory structure'
);
} else {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save(str_replace('.php', '.pdf', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.pdf', __FILE__) , PHP_EOL;
}
} catch (Exception $e) {
echo date('H:i:s') , ' EXCEPTION: ', $e->getMessage() , PHP_EOL;
}
// Remove first two rows with field headers before exporting to CSV
echo date('H:i:s') , " Removing first two heading rows for CSV export" , PHP_EOL;

View File

@ -49,18 +49,18 @@ $objWorksheet->fromArray(
)
);
// Set the Labels for each dataset we want to plot
$labels = array(
// Set the Labels for each data series we want to plot
$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1), // 2010
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), // 2011
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', null, 1), // 2012
);
// Set the X-Axis Labels
$categories = array(
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
);
// Set the Data values for each dataset we want to plot
$values = array(
// Set the Data values for each data series we want to plot
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', null, 4),
@ -70,10 +70,10 @@ $values = array(
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
array(0, 1, 2), // plotOrder
$labels, // plotLabel
$categories, // plotCategory
$values // plotValues
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
// Set additional dataseries parameters
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
@ -84,6 +84,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$title = new PHPExcel_Chart_Title('Test Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
// Create the chart
@ -95,7 +96,7 @@ $chart = new PHPExcel_Chart(
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet

View File

@ -0,0 +1,55 @@
<?php
class DataSeriesValuesTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT'))
{
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
public function testSetDataType()
{
$dataTypeValues = array(
'Number',
'String'
);
$testInstance = new PHPExcel_Chart_DataSeriesValues;
foreach($dataTypeValues as $dataTypeValue) {
$result = $testInstance->setDataType($dataTypeValue);
$this->assertTrue($result instanceof PHPExcel_Chart_DataSeriesValues);
}
}
public function testSetInvalidDataTypeThrowsException()
{
$testInstance = new PHPExcel_Chart_DataSeriesValues;
try {
$result = $testInstance->setDataType('BOOLEAN');
} catch (Exception $e) {
$this->assertEquals($e->getMessage(), 'Invalid datatype for chart data series values');
return;
}
$this->fail('An expected exception has not been raised.');
}
public function testGetDataType()
{
$dataTypeValue = 'String';
$testInstance = new PHPExcel_Chart_DataSeriesValues;
$setValue = $testInstance->setDataType($dataTypeValue);
$result = $testInstance->getDataType();
$this->assertEquals($dataTypeValue,$result);
}
}

View File

@ -0,0 +1,37 @@
<?php
class LayoutTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT'))
{
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
public function testSetLayoutTarget()
{
$LayoutTargetValue = 'String';
$testInstance = new PHPExcel_Chart_Layout;
$result = $testInstance->setLayoutTarget($LayoutTargetValue);
$this->assertTrue($result instanceof PHPExcel_Chart_Layout);
}
public function testGetLayoutTarget()
{
$LayoutTargetValue = 'String';
$testInstance = new PHPExcel_Chart_Layout;
$setValue = $testInstance->setLayoutTarget($LayoutTargetValue);
$result = $testInstance->getLayoutTarget();
$this->assertEquals($LayoutTargetValue,$result);
}
}

View File

@ -0,0 +1,134 @@
<?php
class LegendTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!defined('PHPEXCEL_ROOT'))
{
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}
public function testSetPosition()
{
$positionValues = array(
PHPExcel_Chart_Legend::POSITION_RIGHT,
PHPExcel_Chart_Legend::POSITION_LEFT,
PHPExcel_Chart_Legend::POSITION_TOP,
PHPExcel_Chart_Legend::POSITION_BOTTOM,
PHPExcel_Chart_Legend::POSITION_TOPRIGHT,
);
$testInstance = new PHPExcel_Chart_Legend;
foreach($positionValues as $positionValue) {
$result = $testInstance->setPosition($positionValue);
$this->assertTrue($result);
}
}
public function testSetInvalidPositionReturnsFalse()
{
$testInstance = new PHPExcel_Chart_Legend;
$result = $testInstance->setPosition('BottomLeft');
$this->assertFalse($result);
// Ensure that value is unchanged
$result = $testInstance->getPosition();
$this->assertEquals(PHPExcel_Chart_Legend::POSITION_RIGHT,$result);
}
public function testGetPosition()
{
$PositionValue = PHPExcel_Chart_Legend::POSITION_BOTTOM;
$testInstance = new PHPExcel_Chart_Legend;
$setValue = $testInstance->setPosition($PositionValue);
$result = $testInstance->getPosition();
$this->assertEquals($PositionValue,$result);
}
public function testSetPositionXL()
{
$positionValues = array(
PHPExcel_Chart_Legend::xlLegendPositionBottom,
PHPExcel_Chart_Legend::xlLegendPositionCorner,
PHPExcel_Chart_Legend::xlLegendPositionCustom,
PHPExcel_Chart_Legend::xlLegendPositionLeft,
PHPExcel_Chart_Legend::xlLegendPositionRight,
PHPExcel_Chart_Legend::xlLegendPositionTop,
);
$testInstance = new PHPExcel_Chart_Legend;
foreach($positionValues as $positionValue) {
$result = $testInstance->setPositionXL($positionValue);
$this->assertTrue($result);
}
}
public function testSetInvalidXLPositionReturnsFalse()
{
$testInstance = new PHPExcel_Chart_Legend;
$result = $testInstance->setPositionXL(999);
$this->assertFalse($result);
// Ensure that value is unchanged
$result = $testInstance->getPositionXL();
$this->assertEquals(PHPExcel_Chart_Legend::xlLegendPositionRight,$result);
}
public function testGetPositionXL()
{
$PositionValue = PHPExcel_Chart_Legend::xlLegendPositionCorner;
$testInstance = new PHPExcel_Chart_Legend;
$setValue = $testInstance->setPositionXL($PositionValue);
$result = $testInstance->getPositionXL();
$this->assertEquals($PositionValue,$result);
}
public function testSetOverlay()
{
$overlayValues = array(
TRUE,
FALSE,
);
$testInstance = new PHPExcel_Chart_Legend;
foreach($overlayValues as $overlayValue) {
$result = $testInstance->setOverlay($overlayValue);
$this->assertTrue($result);
}
}
public function testSetInvalidOverlayReturnsFalse()
{
$testInstance = new PHPExcel_Chart_Legend;
$result = $testInstance->setOverlay('INVALID');
$this->assertFalse($result);
$result = $testInstance->getOverlay();
$this->assertFalse($result);
}
public function testGetOverlay()
{
$OverlayValue = TRUE;
$testInstance = new PHPExcel_Chart_Legend;
$setValue = $testInstance->setOverlay($OverlayValue);
$result = $testInstance->getOverlay();
$this->assertEquals($OverlayValue,$result);
}
}