diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7d3ffccc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +unitTests/codeCoverage diff --git a/Classes/PHPExcel/Chart/DataSeriesValues.php b/Classes/PHPExcel/Chart/DataSeriesValues.php index ea6c4925..90ef6687 100644 --- a/Classes/PHPExcel/Chart/DataSeriesValues.php +++ b/Classes/PHPExcel/Chart/DataSeriesValues.php @@ -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; diff --git a/Classes/PHPExcel/Chart/Exception.php b/Classes/PHPExcel/Chart/Exception.php new file mode 100644 index 00000000..0c296ba1 --- /dev/null +++ b/Classes/PHPExcel/Chart/Exception.php @@ -0,0 +1,52 @@ +line = $line; + $e->file = $file; + throw $e; + } +} diff --git a/Classes/PHPExcel/Chart/Legend.php b/Classes/PHPExcel/Chart/Legend.php index 5e914aae..80b54ff8 100644 --- a/Classes/PHPExcel/Chart/Legend.php +++ b/Classes/PHPExcel/Chart/Legend.php @@ -49,13 +49,13 @@ class PHPExcel_Chart_Legend const POSITION_TOP = 't'; const POSITION_TOPRIGHT = 'tr'; - private static $_positionXref = array( self::xlLegendPositionBottom => self::POSITION_BOTTOM, - self::xlLegendPositionCorner => self::POSITION_TOPRIGHT, - self::xlLegendPositionCustom => '??', - self::xlLegendPositionLeft => self::POSITION_LEFT, - self::xlLegendPositionRight => self::POSITION_RIGHT, - self::xlLegendPositionTop => self::POSITION_TOP - ); + private static $_positionXLref = array( self::xlLegendPositionBottom => self::POSITION_BOTTOM, + self::xlLegendPositionCorner => self::POSITION_TOPRIGHT, + self::xlLegendPositionCustom => '??', + self::xlLegendPositionLeft => self::POSITION_LEFT, + self::xlLegendPositionRight => self::POSITION_RIGHT, + self::xlLegendPositionTop => self::POSITION_TOP + ); /** * Legend position @@ -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; } /** diff --git a/Tests/26utf8.php b/Tests/26utf8.php index 41e85af2..b2e69ec0 100644 --- a/Tests/26utf8.php +++ b/Tests/26utf8.php @@ -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; diff --git a/Tests/33chartcreate.php b/Tests/33chartcreate.php index 978b9102..f688b01c 100644 --- a/Tests/33chartcreate.php +++ b/Tests/33chartcreate.php @@ -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 diff --git a/unitTests/PHPExcel/Chart/DataSeriesValuesTest.php b/unitTests/PHPExcel/Chart/DataSeriesValuesTest.php new file mode 100644 index 00000000..38284de9 --- /dev/null +++ b/unitTests/PHPExcel/Chart/DataSeriesValuesTest.php @@ -0,0 +1,55 @@ +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); + } + +} diff --git a/unitTests/PHPExcel/Chart/LayoutTest.php b/unitTests/PHPExcel/Chart/LayoutTest.php new file mode 100644 index 00000000..f24e01c3 --- /dev/null +++ b/unitTests/PHPExcel/Chart/LayoutTest.php @@ -0,0 +1,37 @@ +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); + } + +} diff --git a/unitTests/PHPExcel/Chart/LegendTest.php b/unitTests/PHPExcel/Chart/LegendTest.php new file mode 100644 index 00000000..4c4f8df8 --- /dev/null +++ b/unitTests/PHPExcel/Chart/LegendTest.php @@ -0,0 +1,134 @@ +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); + } + +}