Skip to content

Commit d32359c

Browse files
committed
PowerPoint2007 Writer : Visibility of the Title doesn't work - @Progi1984 GH-107
1 parent 6556874 commit d32359c

File tree

6 files changed

+102
-33
lines changed

6 files changed

+102
-33
lines changed

docs/shapes_chart.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@ Example:
1010
.. code-block:: php
1111
1212
$chartShape = $slide->createChartShape();
13+
14+
Parts
15+
-------
16+
17+
Title
18+
^^^^^
19+
20+
By default, the title of a chart is displayed.
21+
For hiding it, you define its visibility to false.
1322

23+
.. code-block:: php
24+
25+
$chartShape = $slide->createChartShape();
26+
$oLine = new Line();
27+
$oShape->getPlotArea()->setType($oLine);
28+
// Hide the title
29+
$oShape->getTitle()->setVisible(false);
30+
1431
Types
1532
-------
1633

samples/Sample_05_Chart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function fnSlide_Area(PhpPowerpoint $objPHPPowerPoint) {
5050
// Create a shape (chart)
5151
echo date('H:i:s') . ' Create a shape (chart)' . EOL;
5252
$shape = $currentSlide->createChartShape();
53+
$shape->getTitle()->setVisible(false);
5354
$shape->setName('PHPPowerPoint Daily Downloads')->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80);
5455
$shape->setShadow($oShadow);
5556
$shape->setFill($oFill);

src/PhpPowerpoint/Writer/ODPresentation/ObjectsChart.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpOffice\Common\XMLWriter;
2323
use PhpOffice\PhpPowerpoint\PhpPowerpoint;
2424
use PhpOffice\PhpPowerpoint\Shape\Chart;
25+
use PhpOffice\PhpPowerpoint\Shape\Chart\Title;
2526
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\AbstractTypeBar;
2627
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\AbstractTypePie;
2728
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\Area;
@@ -184,7 +185,7 @@ private function writeContentPart(Chart $chart)
184185
$this->writePlotAreaStyle($chart);
185186

186187
// Title
187-
$this->writeTitleStyle($chart);
188+
$this->writeTitleStyle($chart->getTitle());
188189

189190
// Wall
190191
$this->writeWallStyle($chart);
@@ -216,7 +217,7 @@ private function writeContentPart(Chart $chart)
216217
}
217218

218219
//**** Title ****
219-
$this->writeTitle($chart);
220+
$this->writeTitle($chart->getTitle());
220221

221222
//**** Legend ****
222223
$this->writeLegend($chart);
@@ -756,43 +757,47 @@ private function writeTable()
756757
}
757758

758759
/**
759-
* @param Chart $chart
760+
* @param Title $oTitle
760761
*/
761-
private function writeTitle(Chart $chart)
762+
private function writeTitle(Title $oTitle)
762763
{
763-
// chart:title
764-
$this->xmlContent->startElement('chart:title');
765-
$this->xmlContent->writeAttribute('svg:x', String::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getTitle()->getOffsetX()), 3) . 'cm');
766-
$this->xmlContent->writeAttribute('svg:y', String::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getTitle()->getOffsetY()), 3) . 'cm');
767-
$this->xmlContent->writeAttribute('chart:style-name', 'styleTitle');
768-
// > text:p
769-
$this->xmlContent->startElement('text:p');
770-
$this->xmlContent->text($chart->getTitle()->getText());
771-
// > text:p
772-
$this->xmlContent->endElement();
773-
// > chart:title
774-
$this->xmlContent->endElement();
764+
if ($oTitle->isVisible()) {
765+
// chart:title
766+
$this->xmlContent->startElement('chart:title');
767+
$this->xmlContent->writeAttribute('svg:x', String::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetX()), 3) . 'cm');
768+
$this->xmlContent->writeAttribute('svg:y', String::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetY()), 3) . 'cm');
769+
$this->xmlContent->writeAttribute('chart:style-name', 'styleTitle');
770+
// > text:p
771+
$this->xmlContent->startElement('text:p');
772+
$this->xmlContent->text($oTitle->getText());
773+
// > text:p
774+
$this->xmlContent->endElement();
775+
// > chart:title
776+
$this->xmlContent->endElement();
777+
}
775778
}
776779

777780
/**
778-
* @param Chart $chart
781+
* @param Title $oTitle
779782
*/
780-
private function writeTitleStyle(Chart $chart)
783+
private function writeTitleStyle(Title $oTitle)
781784
{
782-
// style:style
783-
$this->xmlContent->startElement('style:style');
784-
$this->xmlContent->writeAttribute('style:name', 'styleTitle');
785-
$this->xmlContent->writeAttribute('style:family', 'chart');
786-
// style:text-properties
787-
$this->xmlContent->startElement('style:text-properties');
788-
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getTitle()->getFont()->getColor()->getRGB());
789-
$this->xmlContent->writeAttribute('fo:font-family', $chart->getTitle()->getFont()->getName());
790-
$this->xmlContent->writeAttribute('fo:font-size', $chart->getTitle()->getFont()->getSize().'pt');
791-
$this->xmlContent->writeAttribute('fo:font-style', $chart->getTitle()->getFont()->isItalic() ? 'italic' : 'normal');
792-
// > style:text-properties
793-
$this->xmlContent->endElement();
794-
// > style:style
795-
$this->xmlContent->endElement();
785+
if ($oTitle->isVisible()) {
786+
// style:style
787+
$this->xmlContent->startElement('style:style');
788+
$this->xmlContent->writeAttribute('style:name', 'styleTitle');
789+
$this->xmlContent->writeAttribute('style:family', 'chart');
790+
// style:text-properties
791+
$this->xmlContent->startElement('style:text-properties');
792+
$this->xmlContent->writeAttribute('fo:color', '#'.$oTitle->getFont()->getColor()->getRGB());
793+
$this->xmlContent->writeAttribute('fo:font-family', $oTitle->getFont()->getName());
794+
$this->xmlContent->writeAttribute('fo:font-size', $oTitle->getFont()->getSize().'pt');
795+
$this->xmlContent->writeAttribute('fo:font-style', $oTitle->getFont()->isItalic() ? 'italic' : 'normal');
796+
// > style:text-properties
797+
$this->xmlContent->endElement();
798+
// > style:style
799+
$this->xmlContent->endElement();
800+
}
796801
}
797802

798803
private function writeWall()

src/PhpPowerpoint/Writer/PowerPoint2007/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function writeChart(ShapeChart $chart = null)
8787

8888
// c:autoTitleDeleted
8989
$objWriter->startElement('c:autoTitleDeleted');
90-
$objWriter->writeAttribute('val', '0');
90+
$objWriter->writeAttribute('val', $chart->getTitle()->isVisible() ? '0' : '1');
9191
$objWriter->endElement();
9292

9393
// c:view3D

tests/PhpPowerpoint/Tests/Writer/ODPresentation/ChartsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ public function testNoChart()
5959

6060
TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');
6161
}
62+
63+
public function testTitleVisibility()
64+
{
65+
66+
$oPHPPowerPoint = new PhpPowerpoint();
67+
$oSlide = $oPHPPowerPoint->getActiveSlide();
68+
$oShape = $oSlide->createChartShape();
69+
$oLine = new Line();
70+
$oShape->getPlotArea()->setType($oLine);
71+
72+
$elementTitle = '/office:document-content/office:body/office:chart/chart:chart/chart:title';
73+
$elementStyle = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleTitle\']';
74+
75+
$this->assertTrue($oShape->getTitle()->isVisible());
76+
$this->assertInstanceOf('PhpOffice\PhpPowerpoint\Shape\Chart\Title', $oShape->getTitle()->setVisible(true));
77+
$oXMLDoc = TestHelperDOCX::getDocument($oPHPPowerPoint, 'ODPresentation');
78+
$this->assertTrue($oXMLDoc->elementExists($elementTitle, 'Object 1/content.xml'));
79+
$this->assertTrue($oXMLDoc->elementExists($elementStyle, 'Object 1/content.xml'));
80+
81+
$this->assertInstanceOf('PhpOffice\PhpPowerpoint\Shape\Chart\Title', $oShape->getTitle()->setVisible(false));
82+
$oXMLDoc = TestHelperDOCX::getDocument($oPHPPowerPoint, 'ODPresentation');
83+
$this->assertFalse($oXMLDoc->elementExists($elementTitle, 'Object 1/content.xml'));
84+
$this->assertFalse($oXMLDoc->elementExists($elementStyle, 'Object 1/content.xml'));
85+
}
6286

6387
public function testChartBar3D()
6488
{

tests/PhpPowerpoint/Tests/Writer/PowerPoint2007/ChartTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ public function testPlotAreaBadType()
7171
TestHelperDOCX::getDocument($oPHPPowerPoint, 'PowerPoint2007');
7272
}
7373

74+
public function testTitleVisibility()
75+
{
76+
$element = '/c:chartSpace/c:chart/c:autoTitleDeleted';
77+
78+
$oPHPPowerPoint = new PhpPowerpoint();
79+
$oSlide = $oPHPPowerPoint->getActiveSlide();
80+
$oShape = $oSlide->createChartShape();
81+
$oLine = new Line();
82+
$oShape->getPlotArea()->setType($oLine);
83+
84+
$this->assertTrue($oShape->getTitle()->isVisible());
85+
$this->assertInstanceOf('PhpOffice\PhpPowerpoint\Shape\Chart\Title', $oShape->getTitle()->setVisible(true));
86+
$oXMLDoc = TestHelperDOCX::getDocument($oPHPPowerPoint, 'PowerPoint2007');
87+
$this->assertTrue($oXMLDoc->elementExists($element, 'ppt/charts/'.$oShape->getIndexedFilename()));
88+
$this->assertEquals('0', $oXMLDoc->getElementAttribute($element, 'val', 'ppt/charts/'.$oShape->getIndexedFilename()));
89+
90+
$this->assertInstanceOf('PhpOffice\PhpPowerpoint\Shape\Chart\Title', $oShape->getTitle()->setVisible(false));
91+
$oXMLDoc = TestHelperDOCX::getDocument($oPHPPowerPoint, 'PowerPoint2007');
92+
$this->assertTrue($oXMLDoc->elementExists($element, 'ppt/charts/'.$oShape->getIndexedFilename()));
93+
$this->assertEquals('1', $oXMLDoc->getElementAttribute($element, 'val', 'ppt/charts/'.$oShape->getIndexedFilename()));
94+
}
95+
7496
public function testTypeBar3D()
7597
{
7698
$seriesData = array(

0 commit comments

Comments
 (0)