From 28bd7e8f002ce3b5a24e11359c54a4fb194d5579 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Sun, 21 May 2017 15:25:52 +0200 Subject: [PATCH] #358 : PowerPoint2007 Writer : Implement gap width in Bar(3D) Charts --- CHANGELOG.md | 1 + docs/shapes_chart.rst | 11 +++++++ samples/Sample_05_Chart.php | 1 + .../Shape/Chart/Type/AbstractTypeBar.php | 32 +++++++++++++++++++ .../Writer/PowerPoint2007/PptCharts.php | 4 +-- .../Tests/Shape/Chart/Type/Bar3DTest.php | 13 ++++++++ .../Tests/Shape/Chart/Type/BarTest.php | 13 ++++++++ .../Writer/PowerPoint2007/PptChartsTest.php | 8 +++++ 8 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6937396c8..26ba2350d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Features - PowerPoint2007 Writer : Implemented XSD validation test case according to the ECMA/ISO standard - @k42b3 GH-307 +- PowerPoint2007 Writer : Implement gap width in Bar(3D) Charts - @Progi1984 GH-358 ## 0.8.0 - 2017-04-03 diff --git a/docs/shapes_chart.rst b/docs/shapes_chart.rst index 8838cfda7..61be333d3 100644 --- a/docs/shapes_chart.rst +++ b/docs/shapes_chart.rst @@ -176,6 +176,17 @@ TODO Bar & Bar3D ^^^^^^^^^^^ +Gap Width +""""""""" + +You can define the gap width between bar or columns clusters. It is defined in percent. +The default value is 150%. The value must be defined between 0 and 500. + +.. code-block:: php + + $oBarChart = new Bar(); + $oBarChart->setGapWidthPercent(250); + Stacking """""""" diff --git a/samples/Sample_05_Chart.php b/samples/Sample_05_Chart.php index e80ca0403..bb28ec65c 100644 --- a/samples/Sample_05_Chart.php +++ b/samples/Sample_05_Chart.php @@ -82,6 +82,7 @@ function fnSlide_Bar(PhpPresentation $objPHPPresentation) { // Create a bar chart (that should be inserted in a shape) echo date('H:i:s') . ' Create a bar chart (that should be inserted in a chart shape)'.EOL; $barChart = new Bar(); + $barChart->setGapWidthPercent(158); $series1 = new Series('2009', $series1Data); $series1->setShowSeriesName(true); $series1->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4F81BD')); diff --git a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php index e4015b3a3..4b2357b69 100644 --- a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php +++ b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php @@ -48,6 +48,14 @@ class AbstractTypeBar extends AbstractType protected $barGrouping = self::GROUPING_CLUSTERED; + /** + * Space between bar or columns clusters + * + * @var int + */ + protected $gapWidthPercent = 150; + + /** * Set bar orientation * @@ -91,6 +99,30 @@ public function getBarGrouping() { return $this->barGrouping; } + + /** + * @return int + */ + public function getGapWidthPercent() + { + return $this->gapWidthPercent; + } + + /** + * @param int $gapWidthPercent + * @return $this + */ + public function setGapWidthPercent($gapWidthPercent) + { + if ($gapWidthPercent < 0) { + $gapWidthPercent = 0; + } + if ($gapWidthPercent > 500) { + $gapWidthPercent = 500; + } + $this->gapWidthPercent = $gapWidthPercent; + return $this; + } /** * Get hash code diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index ef1fd6d94..1bdde2194 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -972,7 +972,7 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, $includeShee // c:gapWidth $objWriter->startElement('c:gapWidth'); - $objWriter->writeAttribute('val', '75'); + $objWriter->writeAttribute('val', $subject->getGapWidthPercent()); $objWriter->endElement(); // c:shape @@ -1174,7 +1174,7 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, $include // c:gapWidth $objWriter->startElement('c:gapWidth'); - $objWriter->writeAttribute('val', '75'); + $objWriter->writeAttribute('val', $subject->getGapWidthPercent()); $objWriter->endElement(); // c:shape diff --git a/tests/PhpPresentation/Tests/Shape/Chart/Type/Bar3DTest.php b/tests/PhpPresentation/Tests/Shape/Chart/Type/Bar3DTest.php index 1853a1bad..1fdbc092f 100644 --- a/tests/PhpPresentation/Tests/Shape/Chart/Type/Bar3DTest.php +++ b/tests/PhpPresentation/Tests/Shape/Chart/Type/Bar3DTest.php @@ -75,6 +75,19 @@ public function testBarGrouping() $this->assertEquals(Bar3D::GROUPING_PERCENTSTACKED, $object->getBarGrouping()); } + public function testGapWidthPercent() + { + $value = rand(0, 500); + $object = new Bar3D(); + $this->assertEquals(150, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar3D', $object->setGapWidthPercent($value)); + $this->assertEquals($value, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar3D', $object->setGapWidthPercent(-1)); + $this->assertEquals(0, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar3D', $object->setGapWidthPercent(501)); + $this->assertEquals(500, $object->getGapWidthPercent()); + } + public function testHashCode() { $oSeries = new Series(); diff --git a/tests/PhpPresentation/Tests/Shape/Chart/Type/BarTest.php b/tests/PhpPresentation/Tests/Shape/Chart/Type/BarTest.php index 302991811..382684ead 100644 --- a/tests/PhpPresentation/Tests/Shape/Chart/Type/BarTest.php +++ b/tests/PhpPresentation/Tests/Shape/Chart/Type/BarTest.php @@ -75,6 +75,19 @@ public function testBarGrouping() $this->assertEquals(Bar::GROUPING_PERCENTSTACKED, $object->getBarGrouping()); } + public function testGapWidthPercent() + { + $value = rand(0, 500); + $object = new Bar(); + $this->assertEquals(150, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setGapWidthPercent($value)); + $this->assertEquals($value, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setGapWidthPercent(-1)); + $this->assertEquals(0, $object->getGapWidthPercent()); + $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setGapWidthPercent(501)); + $this->assertEquals(500, $object->getGapWidthPercent()); + } + public function testHashCode() { $oSeries = new Series(); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index d67e46905..c14cde667 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -316,10 +316,12 @@ public function testTypeAxisUnit() public function testTypeBar() { + $valueGapWidthPercent = rand(0, 500); $oSlide = $this->oPresentation->getActiveSlide(); $oShape = $oSlide->createChartShape(); $oShape->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80); $oBar = new Bar(); + $oBar->setGapWidthPercent($valueGapWidthPercent); $oSeries = new Series('Downloads', $this->seriesData); $oSeries->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); $oSeries->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKBLUE)); @@ -339,14 +341,18 @@ public function testTypeBar() $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:ser/c:tx/c:v'; $this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $oSeries->getTitle()); + $element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:gapWidth'; + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $valueGapWidthPercent); } public function testTypeBar3D() { + $valueGapWidthPercent = rand(0, 500); $oSlide = $this->oPresentation->getActiveSlide(); $oShape = $oSlide->createChartShape(); $oShape->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80); $oBar3D = new Bar3D(); + $oBar3D->setGapWidthPercent($valueGapWidthPercent); $oSeries = new Series('Downloads', $this->seriesData); $oSeries->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); $oSeries->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKBLUE)); @@ -366,6 +372,8 @@ public function testTypeBar3D() $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $element = '/c:chartSpace/c:chart/c:plotArea/c:bar3DChart/c:ser/c:tx/c:v'; $this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $oSeries->getTitle()); + $element = '/c:chartSpace/c:chart/c:plotArea/c:bar3DChart/c:gapWidth'; + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $valueGapWidthPercent); } public function testTypeBar3DSubScript()