Skip to content

Commit

Permalink
Support for line smooth for line and scatter chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jul 29, 2021
1 parent e2f670e commit 72d48a4
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 32 deletions.
3 changes: 3 additions & 0 deletions docs/changes/1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
- Support for interval unit for Chart's Axis - @Progi1984 GH-546
- ODPresentation Writer
- PowerPoint2007 Writer
- Support for line smooth for line and scatter chart - @ksmeeks0001 GH-626 & @Progi1984 GH-662
- ODPresentation Writer
- PowerPoint2007 Writer

## Project Management
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635
Expand Down
34 changes: 32 additions & 2 deletions docs/usage/shapes/chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,43 @@ $barChart->setBarGrouping(Bar::GROUPING_PERCENTSTACKED);

### Line

TODO
#### Smooth line

You can enable or disable the smooth line with `setIsSmooth` method from `AbstractTypeLine`.
By default, smooth line is disabled.

``` php
<?php

$chart = new Line();
// Enable the smooth line
$chart->setIsSmooth(true);
// Disable the smooth line
$chart->setIsSmooth(false);
// Get status of smooth line
$chart->isSmooth();
```

### Pie & Pie3D

TODO

### Scatter

TODO
#### Smooth line

You can enable or disable the smooth line with `setIsSmooth` method from `AbstractTypeLine`.
By default, smooth line is disabled.

``` php
<?php

$chart = new Scatter();
// Enable the smooth line
$chart->setIsSmooth(true);
// Disable the smooth line
$chart->setIsSmooth(false);
// Get status of smooth line
$chart->isSmooth();
```

1 change: 1 addition & 0 deletions samples/Sample_05_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ function fnSlide_Scatter(PhpPresentation $objPHPPresentation)
// Create a scatter chart (that should be inserted in a shape)
echo date('H:i:s') . ' Create a scatter chart (that should be inserted in a chart shape)' . EOL;
$lineChart = new Scatter();
$lineChart->setIsSmooth(true);
$series = new Series('Downloads', $seriesData);
$series->setShowSeriesName(true);
$series->getMarker()->setSymbol(\PhpOffice\PhpPresentation\Shape\Chart\Marker::SYMBOL_CIRCLE);
Expand Down
63 changes: 63 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @see https://github.com/PHPOffice/PHPPresentation
*
* @copyright 2009-2015 PHPPresentation contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpPresentation\Shape\Chart\Type;

class AbstractTypeLine extends AbstractType
{
/**
* Is Line Smooth?
*
* @var bool
*/
protected $isSmooth = false;

/**
* Is Line Smooth?
*
* @return bool
*/
public function isSmooth(): bool
{
return $this->isSmooth;
}

/**
* Set Line Smoothness
*
* @param bool $value
*
* @return AbstractTypeLine
*/
public function setIsSmooth(bool $value = true): AbstractTypeLine
{
$this->isSmooth = $value;

return $this;
}

/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode(): string
{
return md5($this->isSmooth() ? '1' : '0');
}
}
7 changes: 2 additions & 5 deletions src/PhpPresentation/Shape/Chart/Type/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

use PhpOffice\PhpPresentation\ComparableInterface;

/**
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Line.
*/
class Line extends AbstractType implements ComparableInterface
class Line extends AbstractTypeLine implements ComparableInterface
{
/**
* Get hash code.
Expand All @@ -37,6 +34,6 @@ public function getHashCode(): string
$hash .= $series->getHashCode();
}

return md5($hash . __CLASS__);
return md5(parent::getHashCode() . $hash . __CLASS__);
}
}
7 changes: 2 additions & 5 deletions src/PhpPresentation/Shape/Chart/Type/Scatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

use PhpOffice\PhpPresentation\ComparableInterface;

/**
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter.
*/
class Scatter extends AbstractType implements ComparableInterface
class Scatter extends AbstractTypeLine implements ComparableInterface
{
/**
* Get hash code.
Expand All @@ -37,6 +34,6 @@ public function getHashCode(): string
$hash .= $series->getHashCode();
}

return md5($hash . __CLASS__);
return md5(parent::getHashCode() . $hash . __CLASS__);
}
}
3 changes: 3 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
use PhpOffice\PhpPresentation\Shape\Chart\Title;
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeBar;
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeLine;
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypePie;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar;
Expand Down Expand Up @@ -560,6 +561,8 @@ private function writePlotAreaStyle(Chart $chart): void
} elseif ($chartType instanceof Pie3D) {
$this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
$this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
} elseif ($chartType instanceof AbstractTypeLine) {
$this->xmlContent->writeAttributeIf($chartType->isSmooth(), 'chart:interpolation', 'cubic-spline');
}
switch ($chart->getDisplayBlankAs()) {
case Chart::BLANKAS_ZERO:
Expand Down
12 changes: 6 additions & 6 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,11 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl
$this->writeMultipleValuesOrReference($objWriter, $includeSheet, $axisYData, $coords);
$objWriter->endElement();

// c:smooth
$objWriter->startElement('c:smooth');
$objWriter->writeAttribute('val', $subject->isSmooth() ? '1' : '0');
$objWriter->endElement();

$objWriter->endElement();

++$seriesIndex;
Expand All @@ -1836,11 +1841,6 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl
$objWriter->writeAttribute('val', '1');
$objWriter->endElement();

// c:smooth
$objWriter->startElement('c:smooth');
$objWriter->writeAttribute('val', '0');
$objWriter->endElement();

// c:axId
$objWriter->startElement('c:axId');
$objWriter->writeAttribute('val', '52743552');
Expand Down Expand Up @@ -2012,7 +2012,7 @@ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool

// c:smooth
$objWriter->startElement('c:smooth');
$objWriter->writeAttribute('val', '0');
$objWriter->writeAttribute('val', $subject->isSmooth() ? '1' : '0');
$objWriter->endElement();

$objWriter->endElement();
Expand Down
26 changes: 19 additions & 7 deletions tests/PhpPresentation/Tests/Shape/Chart/Type/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,39 @@ public function testData(): void
new Series(),
];

$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Line', $object->setSeries());
$this->assertInstanceOf(Line::class, $object->setSeries());
$this->assertEmpty($object->getSeries());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Line', $object->setSeries($array));
$this->assertInstanceOf(Line::class, $object->setSeries($array));
$this->assertCount(count($array), $object->getSeries());
}

public function testSerties(): void
public function testSeries(): void
{
$object = new Line();

$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Line', $object->addSeries(new Series()));
$this->assertInstanceOf(Line::class, $object->addSeries(new Series()));
$this->assertCount(1, $object->getSeries());
}

public function testSmooth(): void
{
$object = new Line();

$this->assertFalse($object->isSmooth());
$this->assertInstanceOf(Line::class, $object->setIsSmooth(true));
$this->assertTrue($object->isSmooth());
}

public function testHashCode(): void
{
$oSeries = new Series();
$series = new Series();

$object = new Line();
$object->addSeries($oSeries);
$object->addSeries($series);

$this->assertEquals(md5($oSeries->getHashCode() . get_class($object)), $object->getHashCode());
$this->assertEquals(
md5(md5($object->isSmooth() ? '1' : '0') . $series->getHashCode() . get_class($object)),
$object->getHashCode()
);
}
}
26 changes: 19 additions & 7 deletions tests/PhpPresentation/Tests/Shape/Chart/Type/ScatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,39 @@ public function testData(): void
new Series(),
];

$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Scatter', $object->setSeries());
$this->assertInstanceOf(Scatter::class, $object->setSeries());
$this->assertEmpty($object->getSeries());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Scatter', $object->setSeries($array));
$this->assertInstanceOf(Scatter::class, $object->setSeries($array));
$this->assertCount(count($array), $object->getSeries());
}

public function testSerties(): void
public function testSeries(): void
{
$object = new Scatter();

$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Scatter', $object->addSeries(new Series()));
$this->assertInstanceOf(Scatter::class, $object->addSeries(new Series()));
$this->assertCount(1, $object->getSeries());
}

public function testSmooth(): void
{
$object = new Scatter();

$this->assertFalse($object->isSmooth());
$this->assertInstanceOf(Scatter::class, $object->setIsSmooth(true));
$this->assertTrue($object->isSmooth());
}

public function testHashCode(): void
{
$oSeries = new Series();
$series = new Series();

$object = new Scatter();
$object->addSeries($oSeries);
$object->addSeries($series);

$this->assertEquals(md5($oSeries->getHashCode() . get_class($object)), $object->getHashCode());
$this->assertEquals(
md5(md5($object->isSmooth() ? '1' : '0') . $series->getHashCode() . get_class($object)),
$object->getHashCode()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,37 @@ public function testTypeLineSeriesOutline(): void
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}

public function testTypeLineSmooth(): void
{
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'stylePlotArea\']/style:chart-properties';

$oSeries = new Series('Downloads', $this->seriesData);

$oLine = new Line();
$oLine->addSeries($oSeries);
$oLine->setIsSmooth(false);

$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
$oShape->getPlotArea()->setType($oLine);

$this->assertZipFileExists('Object 1/content.xml');
$this->assertZipXmlElementExists('Object 1/content.xml', $element);
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interpolation');
// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');

$this->resetPresentationFile();
$oLine->setIsSmooth(true);
$oShape->getPlotArea()->setType($oLine);

$this->assertZipFileExists('Object 1/content.xml');
$this->assertZipXmlElementExists('Object 1/content.xml', $element);
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interpolation');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interpolation', 'cubic-spline');
// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}

public function testTypePie(): void
{
$oSeries = new Series('Series', ['Jan' => '1', 'Feb' => '5', 'Mar' => '2']);
Expand Down Expand Up @@ -1225,4 +1256,35 @@ public function testTypeScatterSeriesOutline(): void
// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}

public function testTypeScatterSmooth(): void
{
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'stylePlotArea\']/style:chart-properties';

$oSeries = new Series('Downloads', $this->seriesData);

$scatter = new Scatter();
$scatter->addSeries($oSeries);
$scatter->setIsSmooth(false);

$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
$oShape->getPlotArea()->setType($scatter);

$this->assertZipFileExists('Object 1/content.xml');
$this->assertZipXmlElementExists('Object 1/content.xml', $element);
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:interpolation');
// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');

$this->resetPresentationFile();
$scatter->setIsSmooth(true);
$oShape->getPlotArea()->setType($scatter);

$this->assertZipFileExists('Object 1/content.xml');
$this->assertZipXmlElementExists('Object 1/content.xml', $element);
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:interpolation');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:interpolation', 'cubic-spline');
// chart:title : Element chart failed to validate attributes
$this->assertIsSchemaOpenDocumentNotValid('1.2');
}
}
Loading

0 comments on commit 72d48a4

Please sign in to comment.