From 40ee1adeff66bbd925324a2bc6e84cb2c098e894 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 30 May 2017 13:45:23 +0200 Subject: [PATCH] #171 : PowerPoint2007 : Interval Unit in Axis-X --- docs/changes/1.0.0.md | 3 + samples/Sample_05_Chart_Line.php | 23 +++++--- .../Writer/ODPresentation/ObjectsChart.php | 2 + .../Writer/PowerPoint2007/PptCharts.php | 7 +++ .../ODPresentation/ObjectsChartTest.php | 55 ++++++++++++++++++- .../Writer/PowerPoint2007/PptChartsTest.php | 28 +++++++++- 6 files changed, 107 insertions(+), 11 deletions(-) diff --git a/docs/changes/1.0.0.md b/docs/changes/1.0.0.md index 14b9572c3..828751b69 100644 --- a/docs/changes/1.0.0.md +++ b/docs/changes/1.0.0.md @@ -46,6 +46,9 @@ - Support for defining ticks label position for Axis in Chart - @Web-Mobiledev GH-591 & @Progi1986 GH-660 - ODPresentation Writer - PowerPoint2007 Writer +- Support for interval unit for Chart's Axis - @Progi1984 GH-546 + - ODPresentation Writer + - PowerPoint2007 Writer ## Project Management - Migrated from Travis CI to Github Actions - @Progi1984 GH-635 diff --git a/samples/Sample_05_Chart_Line.php b/samples/Sample_05_Chart_Line.php index c0c59ef44..f87aad8c3 100644 --- a/samples/Sample_05_Chart_Line.php +++ b/samples/Sample_05_Chart_Line.php @@ -36,13 +36,20 @@ // Generate sample data for chart echo date('H:i:s') . ' Generate sample data for chart' . EOL; $seriesData = [ - 'Monday' => 12, - 'Tuesday' => 15, - 'Wednesday' => 13, - 'Thursday' => 17, - 'Friday' => 14, - 'Saturday' => 9, - 'Sunday' => 7, + 'Monday 01' => 12, + 'Tuesday 02' => 15, + 'Wednesday 03' => 13, + 'Thursday 04' => 17, + 'Friday 05' => 14, + 'Saturday 06' => 9, + 'Sunday 07' => 7, + 'Monday 08' => 8, + 'Tuesday 09' => 8, + 'Wednesday 10' => 15, + 'Thursday 11' => 16, + 'Friday 12' => 14, + 'Saturday 13' => 14, + 'Sunday 14' => 13, ]; // Create templated slide @@ -71,6 +78,8 @@ $shape->getView3D()->setPerspective(30); $shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE); $shape->getLegend()->getFont()->setItalic(true); +$shape->getPlotArea()->getAxisX()->setMajorUnit(3); +$shape->getPlotArea()->getAxisY()->setMajorUnit(5); // Create templated slide echo EOL . date('H:i:s') . ' Create templated slide' . EOL; diff --git a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php index 1a48fd2e9..13759a560 100644 --- a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php +++ b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php @@ -310,6 +310,8 @@ private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, bool $i $this->xmlContent->writeAttributeIf($isPieChart, 'chart:reverse-direction', 'true'); $this->xmlContent->writeAttributeIf(null !== $axis->getMinBounds(), 'chart:minimum', $axis->getMinBounds()); $this->xmlContent->writeAttributeIf(null !== $axis->getMaxBounds(), 'chart:maximum', $axis->getMaxBounds()); + $this->xmlContent->writeAttributeIf(null !== $axis->getMajorUnit(), 'chart:interval-major', $axis->getMajorUnit()); + $this->xmlContent->writeAttributeIf(null !== $axis->getMinorUnit(), 'chart:interval-minor-divisor', $axis->getMinorUnit()); switch ($axis->getTickLabelPosition()) { case Axis::TICK_LABEL_POSITION_NEXT_TO: $this->xmlContent->writeAttribute('chart:axis-label-position', 'near-axis'); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index eab8f4b1f..ada5fb77b 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -2314,6 +2314,13 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty $objWriter->startElement('c:lblOffset'); $objWriter->writeAttribute('val', '100'); $objWriter->endElement(); + + // c:majorUnit + if ($oAxis->getMajorUnit() != null) { + $objWriter->startElement('c:tickLblSkip'); + $objWriter->writeAttribute('val', $oAxis->getMajorUnit()); + $objWriter->endElement(); + } } if (Chart\Axis::AXIS_Y == $typeAxis) { diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php index 20eb4bb2d..ef3d266d3 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php @@ -463,8 +463,7 @@ public function testTypeAxisBounds(): void { $value = mt_rand(0, 100); - $oSeries = new Series('Downloads', ['A' => '1', 'B' => '2', 'C' => '4', 'D' => '3', 'E' => '2']); - $oSeries->getFill()->setStartColor(new Color('FFAABBCC')); + $oSeries = new Series('Downloads', $this->seriesData); $oLine = new Line(); $oLine->addSeries($oSeries); $oShape = $this->oPresentation->getActiveSlide()->createChartShape(); @@ -512,6 +511,58 @@ public function testTypeAxisBounds(): void $this->assertIsSchemaOpenDocumentNotValid('1.2'); } + public function testTypeAxisUnit(): void + { + $value = mt_rand(0, 100); + + $series = new Series('Downloads', $this->seriesData); + $line = new Line(); + $line->addSeries($series); + $shape = $this->oPresentation->getActiveSlide()->createChartShape(); + $shape->getPlotArea()->setType($line); + + $element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties'; + + $this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor'); + $this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major'); + + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $shape->getPlotArea()->getAxisX()->setMinorUnit($value); + $this->resetPresentationFile(); + + $this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-major'); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value); + + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $shape->getPlotArea()->getAxisX()->setMinorUnit(null); + $shape->getPlotArea()->getAxisX()->setMajorUnit($value); + $this->resetPresentationFile(); + + $this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor'); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value); + + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $shape->getPlotArea()->getAxisX()->setMinorUnit($value); + $shape->getPlotArea()->getAxisX()->setMajorUnit($value); + $this->resetPresentationFile(); + + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-minor-divisor'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-minor-divisor', $value); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interval-major'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interval-major', $value); + + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + } + public function testTypeAxisTickLabelPosition(): void { $element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties'; diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index 0803585b3..0df4119b8 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -499,12 +499,36 @@ public function testTypeAxisTickMark(): void $this->assertIsSchemaECMA376Valid(); } - public function testTypeAxisUnit(): void + public function testTypeAxisXUnit(): void + { + $value = mt_rand(0, 100); + + $oSeries = new Series('Downloads', $this->seriesData); + $oLine = new Line(); + $oLine->addSeries($oSeries); + $oShape = $this->oPresentation->getActiveSlide()->createChartShape(); + $oShape->getPlotArea()->setType($oLine); + + $elementMax = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:tickLblSkip'; + + $this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax); + + $this->assertIsSchemaECMA376Valid(); + + $oShape->getPlotArea()->getAxisX()->setMajorUnit($value); + $this->resetPresentationFile(); + + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value); + + $this->assertIsSchemaECMA376Valid(); + } + + public function testTypeAxisYUnit(): void { $value = mt_rand(0, 100); $oSeries = new Series('Downloads', $this->seriesData); - $oSeries->getFill()->setStartColor(new Color('FFAABBCC')); $oLine = new Line(); $oLine->addSeries($oSeries); $oShape = $this->oPresentation->getActiveSlide()->createChartShape();