diff --git a/docs/changes/1.0.0.md b/docs/changes/1.0.0.md index aea01fef3..230992c33 100644 --- a/docs/changes/1.0.0.md +++ b/docs/changes/1.0.0.md @@ -40,6 +40,9 @@ - Support for rotation for axis label - @Progi1986 GH-410 - ODPresentation Writer - PowerPoint2007 Writer +- Support for managing missing values in Chart - @TonisOrmisson GH-581 & @Progi1986 GH-659 + - ODPresentation Writer + - PowerPoint2007 Writer ## Project Management - Migrated from Travis CI to Github Actions - @Progi1984 GH-635 diff --git a/docs/images/libreoffice_chart_displayblankas.png b/docs/images/libreoffice_chart_displayblankas.png new file mode 100644 index 000000000..c37331d77 Binary files /dev/null and b/docs/images/libreoffice_chart_displayblankas.png differ diff --git a/docs/usage/presentation.md b/docs/usage/presentation.md index 2680474c1..7e34e4684 100644 --- a/docs/usage/presentation.md +++ b/docs/usage/presentation.md @@ -138,7 +138,7 @@ $state = $presentation->isMarkedAsFinal(); You can define the type of slideshow you can with the method `setSlideshowType`. -![Slideshow type](/PHPPresentation/images/presentation_slideshow_type.png) +![Slideshow type](/images/presentation_slideshow_type.png) Differents types are available: diff --git a/docs/usage/shapes/chart.md b/docs/usage/shapes/chart.md index ace7e3f12..0e6c8b315 100644 --- a/docs/usage/shapes/chart.md +++ b/docs/usage/shapes/chart.md @@ -8,6 +8,31 @@ To create a chart, use `createChartShape` method of Slide. $chartShape = $slide->createChartShape(); ``` +## Customization + +### Manage how blank values are displayed + +You can define how blank values are displayed with the method `setDisplayBlankAs`. + +![Slideshow type](/images/libreoffice_chart_displayblankas.png) + +Differents types are available: + +* `Chart::BLANKAS_GAP` for **Leave a gap** +* `Chart::BLANKAS_ZERO` for **Assume zero** (default) +* `Chart::BLANKAS_SPAN` for **Continue line** + +``` php +setDisplayBlankAs(Chart::BLANKAS_GAP); +// Get the behavior +echo $chart->getDisplayBlankAs(); +``` + ## Parts ### Axis diff --git a/samples/Sample_05_Chart_Line.php b/samples/Sample_05_Chart_Line.php index 84b232b64..c0c59ef44 100644 --- a/samples/Sample_05_Chart_Line.php +++ b/samples/Sample_05_Chart_Line.php @@ -3,11 +3,15 @@ include_once 'Sample_Header.php'; use PhpOffice\PhpPresentation\PhpPresentation; +use PhpOffice\PhpPresentation\Shape\Chart; +use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; +use PhpOffice\PhpPresentation\Shape\Chart\Marker; use PhpOffice\PhpPresentation\Shape\Chart\Series; use PhpOffice\PhpPresentation\Shape\Chart\Type\Line; use PhpOffice\PhpPresentation\Style\Border; use PhpOffice\PhpPresentation\Style\Color; use PhpOffice\PhpPresentation\Style\Fill; +use PhpOffice\PhpPresentation\Style\Outline; use PhpOffice\PhpPresentation\Style\Shadow; // Create new PHPPresentation object @@ -73,7 +77,7 @@ $currentSlide = createTemplatedSlide($objPHPPresentation); // Create a line chart (that should be inserted in a shape) -$oOutline = new \PhpOffice\PhpPresentation\Style\Outline(); +$oOutline = new Outline(); $oOutline->getFill()->setFillType(Fill::FILL_SOLID); $oOutline->getFill()->setStartColor(new Color(Color::COLOR_YELLOW)); $oOutline->setWidth(2); @@ -82,7 +86,7 @@ $lineChart1 = clone $lineChart; $series1 = $lineChart1->getSeries(); $series1[0]->setOutline($oOutline); -$series1[0]->getMarker()->setSymbol(\PhpOffice\PhpPresentation\Shape\Chart\Marker::SYMBOL_DIAMOND); +$series1[0]->getMarker()->setSymbol(Marker::SYMBOL_DIAMOND); $series1[0]->getMarker()->setSize(7); $lineChart1->setSeries($series1); @@ -106,7 +110,7 @@ $lineChart2 = clone $lineChart; $series2 = $lineChart2->getSeries(); $series2[0]->getFont()->setSize(25); -$series2[0]->getMarker()->setSymbol(\PhpOffice\PhpPresentation\Shape\Chart\Marker::SYMBOL_TRIANGLE); +$series2[0]->getMarker()->setSymbol(Marker::SYMBOL_TRIANGLE); $series2[0]->getMarker()->setSize(10); $lineChart2->setSeries($series2); @@ -129,11 +133,11 @@ echo date('H:i:s') . ' Create a line chart (that should be inserted in a chart shape)' . EOL; $lineChart3 = clone $lineChart; -$oGridLines1 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines(); +$oGridLines1 = new Gridlines(); $oGridLines1->getOutline()->setWidth(10); $oGridLines1->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE)); -$oGridLines2 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines(); +$oGridLines2 = new Gridlines(); $oGridLines2->getOutline()->setWidth(1); $oGridLines2->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKGREEN)); @@ -156,12 +160,12 @@ echo date('H:i:s') . ' Create a line chart (that should be inserted in a chart shape)' . EOL; $lineChart4 = clone $lineChart; -$oOutlineAxisX = new \PhpOffice\PhpPresentation\Style\Outline(); +$oOutlineAxisX = new Outline(); $oOutlineAxisX->setWidth(2); $oOutlineAxisX->getFill()->setFillType(Fill::FILL_SOLID); $oOutlineAxisX->getFill()->getStartColor()->setRGB('012345'); -$oOutlineAxisY = new \PhpOffice\PhpPresentation\Style\Outline(); +$oOutlineAxisY = new Outline(); $oOutlineAxisY->setWidth(5); $oOutlineAxisY->getFill()->setFillType(Fill::FILL_SOLID); $oOutlineAxisY->getFill()->getStartColor()->setRGB('ABCDEF'); @@ -190,6 +194,26 @@ $shape5->getPlotArea()->getAxisY()->setMinBounds(5); $shape5->getPlotArea()->getAxisY()->setMaxBounds(20); $currentSlide->addShape($shape5); + +// Create templated slide +echo EOL . date('H:i:s') . ' Create templated slide #6' . EOL; +$currentSlide = createTemplatedSlide($objPHPPresentation); + +// Create a shape (chart) +echo date('H:i:s') . ' Create a shape (chart6)' . EOL; +echo date('H:i:s') . ' Feature : DisplayBlankAs' . EOL; +$shape6 = clone $shape; +$lineChart6 = clone $lineChart; +$series6 = clone $series; +$seriesData6 = $seriesData; +$seriesData6['Thursday'] = null; + +$series6->setValues($seriesData6); +$lineChart6->setSeries([$series6]); +$shape6->getPlotArea()->setType($lineChart6); +$shape6->setDisplayBlankAs(Chart::BLANKAS_GAP); +$currentSlide->addShape($shape6); + // Save file echo EOL . write($objPHPPresentation, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpPresentation/Shape/Chart.php b/src/PhpPresentation/Shape/Chart.php index d840ccb3b..7cebcb3b2 100644 --- a/src/PhpPresentation/Shape/Chart.php +++ b/src/PhpPresentation/Shape/Chart.php @@ -29,6 +29,10 @@ */ class Chart extends AbstractGraphic implements ComparableInterface { + public const BLANKAS_GAP = 'gap'; + public const BLANKAS_ZERO = 'zero'; + public const BLANKAS_SPAN = 'span'; + /** * Title. * @@ -64,6 +68,13 @@ class Chart extends AbstractGraphic implements ComparableInterface */ private $includeSpreadsheet = false; + /** + * How to display blank (missing) values? Not set by default. + * + * @var string + */ + private $displayBlankAs = self::BLANKAS_ZERO; + /** * Create a new Chart. */ @@ -89,6 +100,16 @@ public function __clone() $this->view3D = clone $this->view3D; } + /** + * How missing/blank values are displayed on chart (dispBlanksAs property) + * + * @return string + */ + public function getDisplayBlankAs(): string + { + return $this->displayBlankAs; + } + /** * Get Title. * @@ -139,6 +160,22 @@ public function hasIncludedSpreadsheet(): bool return $this->includeSpreadsheet; } + /** + * Define a way to display missing/blank values (dispBlanksAs property) + * + * @param string $value + * + * @return self + */ + public function setDisplayBlankAs(string $value): self + { + if (in_array($value, [self::BLANKAS_GAP, self::BLANKAS_SPAN, self::BLANKAS_ZERO])) { + $this->displayBlankAs = $value; + } + + return $this; + } + /** * Is the spreadsheet included for editing data ? * diff --git a/src/PhpPresentation/Shape/Chart/Series.php b/src/PhpPresentation/Shape/Chart/Series.php index 15616fa2d..b60d03f24 100644 --- a/src/PhpPresentation/Shape/Chart/Series.php +++ b/src/PhpPresentation/Shape/Chart/Series.php @@ -132,7 +132,7 @@ class Series implements ComparableInterface /** * Values (key/value). * - * @var array + * @var array */ private $values = []; @@ -144,7 +144,8 @@ class Series implements ComparableInterface private $hashIndex; /** - * @param array $values + * @param string $title + * @param array $values */ public function __construct(string $title = 'Series Title', array $values = []) { @@ -152,9 +153,10 @@ public function __construct(string $title = 'Series Title', array $values = []) $this->font = new Font(); $this->font->setName('Calibri'); $this->font->setSize(9); + $this->marker = new Marker(); + $this->title = $title; $this->values = $values; - $this->marker = new Marker(); } /** @@ -239,7 +241,7 @@ public function getDataPointFills(): array /** * Get Values. * - * @return array + * @return array */ public function getValues(): array { @@ -249,7 +251,7 @@ public function getValues(): array /** * Set Values. * - * @param array $values + * @param array $values */ public function setValues(array $values = []): self { @@ -260,8 +262,13 @@ public function setValues(array $values = []): self /** * Add Value. + * + * @param string $key + * @param string|null $value + * + * @return self */ - public function addValue(string $key, string $value): self + public function addValue(string $key, ?string $value): self { $this->values[$key] = $value; diff --git a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php index ce2473c08..fc53d0e23 100644 --- a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php +++ b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php @@ -544,6 +544,17 @@ private function writePlotAreaStyle(Chart $chart): void $this->xmlContent->writeAttribute('chart:three-dimensional', 'true'); $this->xmlContent->writeAttribute('chart:right-angled-axes', 'true'); } + switch ($chart->getDisplayBlankAs()) { + case Chart::BLANKAS_ZERO: + $this->xmlContent->writeAttribute('chart:treat-empty-cells', 'use-zero'); + break; + case Chart::BLANKAS_GAP: + $this->xmlContent->writeAttribute('chart:treat-empty-cells', 'leave-gap'); + break; + case Chart::BLANKAS_SPAN: + $this->xmlContent->writeAttribute('chart:treat-empty-cells', 'ignore'); + break; + } if ($chartType instanceof AbstractTypeBar) { $chartVertical = 'false'; if (AbstractTypeBar::DIRECTION_HORIZONTAL == $chartType->getBarDirection()) { @@ -809,40 +820,6 @@ private function writeTable(): void // > table:table-header-columns $this->xmlContent->endElement(); - // table:table-rows - $this->xmlContent->startElement('table:table-rows'); - if (empty($this->arrayData)) { - $this->xmlContent->startElement('table:table-row'); - $this->xmlContent->startElement('table:table-cell'); - $this->xmlContent->endElement(); - $this->xmlContent->endElement(); - } else { - foreach ($this->arrayData as $row) { - // table:table-row - $this->xmlContent->startElement('table:table-row'); - foreach ($row as $cell) { - // table:table-cell - $this->xmlContent->startElement('table:table-cell'); - - $cellNumeric = is_numeric($cell); - $this->xmlContent->writeAttributeIf(!$cellNumeric, 'office:value-type', 'string'); - $this->xmlContent->writeAttributeIf($cellNumeric, 'office:value-type', 'float'); - $this->xmlContent->writeAttributeIf($cellNumeric, 'office:value', $cell); - // text:p - $this->xmlContent->startElement('text:p'); - $this->xmlContent->text($cell); - // > text:p - $this->xmlContent->endElement(); - // > table:table-cell - $this->xmlContent->endElement(); - } - // > table:table-row - $this->xmlContent->endElement(); - } - } - // > table:table-rows - $this->xmlContent->endElement(); - // table:table-header-rows $this->xmlContent->startElement('table:table-header-rows'); // table:table-row @@ -874,6 +851,39 @@ private function writeTable(): void // > table:table-header-rows $this->xmlContent->endElement(); + // table:table-rows + $this->xmlContent->startElement('table:table-rows'); + if (empty($this->arrayData)) { + $this->xmlContent->startElement('table:table-row'); + $this->xmlContent->startElement('table:table-cell'); + $this->xmlContent->endElement(); + $this->xmlContent->endElement(); + } else { + foreach ($this->arrayData as $row) { + // table:table-row + $this->xmlContent->startElement('table:table-row'); + foreach ($row as $cell) { + // table:table-cell + $this->xmlContent->startElement('table:table-cell'); + + $cellValueTypeFloat = is_null($cell) ? true : is_numeric($cell); + $this->xmlContent->writeAttributeIf(!$cellValueTypeFloat, 'office:value-type', 'string'); + $this->xmlContent->writeAttributeIf($cellValueTypeFloat, 'office:value-type', 'float'); + $this->xmlContent->writeAttributeIf($cellValueTypeFloat, 'office:value', is_null($cell) ? 'NaN' : $cell); + // text:p + $this->xmlContent->startElement('text:p'); + $this->xmlContent->text(is_null($cell) ? 'NaN' : $cell); + $this->xmlContent->endElement(); + // > table:table-cell + $this->xmlContent->endElement(); + } + // > table:table-row + $this->xmlContent->endElement(); + } + } + // > table:table-rows + $this->xmlContent->endElement(); + // > table:table $this->xmlContent->endElement(); } diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index a90b16ade..a031d0bc6 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -146,6 +146,11 @@ public function writeChart(Chart $chart): string $objWriter->writeAttribute('val', '1'); $objWriter->endElement(); + // c:dispBlanksAs + $objWriter->startElement('c:dispBlanksAs'); + $objWriter->writeAttribute('val', $chart->getDisplayBlankAs()); + $objWriter->endElement(); + $objWriter->endElement(); // c:spPr diff --git a/tests/PhpPresentation/Tests/Shape/ChartTest.php b/tests/PhpPresentation/Tests/Shape/ChartTest.php index 0195aa19f..a042fa8df 100644 --- a/tests/PhpPresentation/Tests/Shape/ChartTest.php +++ b/tests/PhpPresentation/Tests/Shape/ChartTest.php @@ -32,10 +32,10 @@ public function testConstruct(): void { $object = new Chart(); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Title', $object->getTitle()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->getLegend()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->getPlotArea()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\View3D', $object->getView3D()); + $this->assertInstanceOf(Chart\Title::class, $object->getTitle()); + $this->assertInstanceOf(Chart\Legend::class, $object->getLegend()); + $this->assertInstanceOf(Chart\PlotArea::class, $object->getPlotArea()); + $this->assertInstanceOf(Chart\View3D::class, $object->getView3D()); } public function testClone(): void @@ -44,11 +44,26 @@ public function testClone(): void $oClone = clone $object; - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $oClone); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Title', $oClone->getTitle()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $oClone->getLegend()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $oClone->getPlotArea()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\View3D', $oClone->getView3D()); + $this->assertInstanceOf(Chart::class, $oClone); + $this->assertInstanceOf(Chart\Title::class, $oClone->getTitle()); + $this->assertInstanceOf(Chart\Legend::class, $oClone->getLegend()); + $this->assertInstanceOf(Chart\PlotArea::class, $oClone->getPlotArea()); + $this->assertInstanceOf(Chart\View3D::class, $oClone->getView3D()); + } + + public function testDisplayBlankAs(): void + { + $object = new Chart(); + + $this->assertEquals(Chart::BLANKAS_ZERO, $object->getDisplayBlankAs()); + $this->assertInstanceOf(Chart::class, $object->setDisplayBlankAs(Chart::BLANKAS_GAP)); + $this->assertEquals(Chart::BLANKAS_GAP, $object->getDisplayBlankAs()); + $this->assertInstanceOf(Chart::class, $object->setDisplayBlankAs(Chart::BLANKAS_ZERO)); + $this->assertEquals(Chart::BLANKAS_ZERO, $object->getDisplayBlankAs()); + $this->assertInstanceOf(Chart::class, $object->setDisplayBlankAs(Chart::BLANKAS_SPAN)); + $this->assertEquals(Chart::BLANKAS_SPAN, $object->getDisplayBlankAs()); + $this->assertInstanceOf(Chart::class, $object->setDisplayBlankAs('Unauthorized value')); + $this->assertEquals(Chart::BLANKAS_SPAN, $object->getDisplayBlankAs()); } public function testIncludeSpreadsheet(): void @@ -56,11 +71,11 @@ public function testIncludeSpreadsheet(): void $object = new Chart(); $this->assertFalse($object->hasIncludedSpreadsheet()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $object->setIncludeSpreadsheet()); + $this->assertInstanceOf(Chart::class, $object->setIncludeSpreadsheet()); $this->assertFalse($object->hasIncludedSpreadsheet()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $object->setIncludeSpreadsheet(false)); + $this->assertInstanceOf(Chart::class, $object->setIncludeSpreadsheet(false)); $this->assertFalse($object->hasIncludedSpreadsheet()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart', $object->setIncludeSpreadsheet(true)); + $this->assertInstanceOf(Chart::class, $object->setIncludeSpreadsheet(true)); $this->assertTrue($object->hasIncludedSpreadsheet()); } } diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php index 3ca0b8c0d..89b149341 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpPresentation\Tests\Writer\ODPresentation; use PhpOffice\Common\Drawing as CommonDrawing; +use PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Shape\Chart\Legend; use PhpOffice\PhpPresentation\Shape\Chart\Marker; @@ -166,6 +167,46 @@ public function testAxisVisibility(): void $this->assertIsSchemaOpenDocumentNotValid('1.2'); } + public function testChartDisplayBlankAs(): void + { + $oSeries = new Series('Downloads', $this->seriesData); + + $oLine = new Line(); + $oLine->addSeries($oSeries); + + $oShape = $this->oPresentation->getActiveSlide()->createChartShape(); + $oShape->getPlotArea()->setType($oLine); + $oShape->setDisplayBlankAs(Chart::BLANKAS_ZERO); + + $element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'stylePlotArea\']/style:chart-properties'; + $this->assertZipFileExists('Object 1/content.xml'); + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:treat-empty-cells'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:treat-empty-cells', 'use-zero'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $this->resetPresentationFile(); + $oShape->setDisplayBlankAs(Chart::BLANKAS_SPAN); + + $this->assertZipFileExists('Object 1/content.xml'); + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:treat-empty-cells'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:treat-empty-cells', 'ignore'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $this->resetPresentationFile(); + $oShape->setDisplayBlankAs(Chart::BLANKAS_GAP); + + $this->assertZipFileExists('Object 1/content.xml'); + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:treat-empty-cells'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:treat-empty-cells', 'leave-gap'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + } + public function testLegend(): void { $oSeries = new Series('Series', ['Jan' => '1', 'Feb' => '5', 'Mar' => '2']); @@ -238,7 +279,72 @@ public function testLegend(): void $this->assertIsSchemaOpenDocumentNotValid('1.2'); } - public function testSeries(): void + public function testSeriesValues(): void + { + $series = new Series('Series', ['Jan' => null]); + + $pie = new Pie(); + $pie->addSeries($series); + + $chart = $this->oPresentation->getActiveSlide()->createChartShape(); + $chart->getPlotArea()->setType($pie); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'office:value-type'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'office:value-type', 'float'); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'office:value'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'office:value', 'NaN'); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]/text:p'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlElementEquals('Object 1/content.xml', $element, 'NaN'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $this->resetPresentationFile(); + + $series = new Series('Series', ['Jan' => '12.3']); + $chart->getPlotArea()->getType()->setSeries([$series]); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'office:value-type'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'office:value-type', 'float'); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'office:value'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'office:value', '12.3'); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]/text:p'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlElementEquals('Object 1/content.xml', $element, '12.3'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + + $this->resetPresentationFile(); + + $series = new Series('Series', ['Jan' => 'data']); + $chart->getPlotArea()->getType()->setSeries([$series]); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'office:value-type'); + $this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'office:value-type', 'string'); + $this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'office:value'); + + $element = '/office:document-content/office:body/office:chart/chart:chart/table:table/table:table-rows/table:table-row/table:table-cell[2]/text:p'; + + $this->assertZipXmlElementExists('Object 1/content.xml', $element); + $this->assertZipXmlElementEquals('Object 1/content.xml', $element, 'data'); + // chart:title : Element chart failed to validate attributes + $this->assertIsSchemaOpenDocumentNotValid('1.2'); + } + + public function testSeriesShowConfig(): void { $oSeries = new Series('Series', ['Jan' => '1', 'Feb' => '5', 'Mar' => '2']); $oPie = new Pie(); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index fbb794f3e..41de5a90d 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -4,6 +4,7 @@ use Exception; use PhpOffice\Common\Drawing; +use PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\Shape\Chart\Axis; use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; use PhpOffice\PhpPresentation\Shape\Chart\Marker; @@ -39,6 +40,47 @@ class PptChartsTest extends PhpPresentationTestCase 'E' => '2', ]; + public function testChartDisplayBlankAs(): void + { + $oSeries = new Series('Downloads', $this->seriesData); + + $oLine = new Line(); + $oLine->addSeries($oSeries); + + $oShape = $this->oPresentation->getActiveSlide()->createChartShape(); + $oShape->getPlotArea()->setType($oLine); + $oShape->setDisplayBlankAs(Chart::BLANKAS_ZERO); + + $element = '/c:chartSpace/c:chart/c:dispBlanksAs'; + + $this->assertZipFileExists('ppt/charts/' . $oShape->getIndexedFilename()); + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $this->assertZipXmlAttributeExists('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val'); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Chart::BLANKAS_ZERO); + + $this->assertIsSchemaECMA376Valid(); + + $this->resetPresentationFile(); + $oShape->setDisplayBlankAs(Chart::BLANKAS_SPAN); + + $this->assertZipFileExists('ppt/charts/' . $oShape->getIndexedFilename()); + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $this->assertZipXmlAttributeExists('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val'); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Chart::BLANKAS_SPAN); + + $this->assertIsSchemaECMA376Valid(); + + $this->resetPresentationFile(); + $oShape->setDisplayBlankAs(Chart::BLANKAS_GAP); + + $this->assertZipFileExists('ppt/charts/' . $oShape->getIndexedFilename()); + $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); + $this->assertZipXmlAttributeExists('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val'); + $this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Chart::BLANKAS_GAP); + + $this->assertIsSchemaECMA376Valid(); + } + public function testChartIncludeSpreadsheet(): void { $oSlide = $this->oPresentation->getActiveSlide();