Skip to content

Commit 7932eb1

Browse files
authored
Merge pull request #662 from Progi1984/lineSmooth
Support for line smooth for line and scatter chart
2 parents e2f670e + 72d48a4 commit 7932eb1

File tree

12 files changed

+286
-32
lines changed

12 files changed

+286
-32
lines changed

docs/changes/1.0.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
- Support for interval unit for Chart's Axis - @Progi1984 GH-546
5050
- ODPresentation Writer
5151
- PowerPoint2007 Writer
52+
- Support for line smooth for line and scatter chart - @ksmeeks0001 GH-626 & @Progi1984 GH-662
53+
- ODPresentation Writer
54+
- PowerPoint2007 Writer
5255

5356
## Project Management
5457
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635

docs/usage/shapes/chart.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,43 @@ $barChart->setBarGrouping(Bar::GROUPING_PERCENTSTACKED);
364364

365365
### Line
366366

367-
TODO
367+
#### Smooth line
368+
369+
You can enable or disable the smooth line with `setIsSmooth` method from `AbstractTypeLine`.
370+
By default, smooth line is disabled.
371+
372+
``` php
373+
<?php
374+
375+
$chart = new Line();
376+
// Enable the smooth line
377+
$chart->setIsSmooth(true);
378+
// Disable the smooth line
379+
$chart->setIsSmooth(false);
380+
// Get status of smooth line
381+
$chart->isSmooth();
382+
```
368383

369384
### Pie & Pie3D
370385

371386
TODO
372387

373388
### Scatter
374389

375-
TODO
390+
#### Smooth line
391+
392+
You can enable or disable the smooth line with `setIsSmooth` method from `AbstractTypeLine`.
393+
By default, smooth line is disabled.
394+
395+
``` php
396+
<?php
397+
398+
$chart = new Scatter();
399+
// Enable the smooth line
400+
$chart->setIsSmooth(true);
401+
// Disable the smooth line
402+
$chart->setIsSmooth(false);
403+
// Get status of smooth line
404+
$chart->isSmooth();
405+
```
376406

samples/Sample_05_Chart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ function fnSlide_Scatter(PhpPresentation $objPHPPresentation)
562562
// Create a scatter chart (that should be inserted in a shape)
563563
echo date('H:i:s') . ' Create a scatter chart (that should be inserted in a chart shape)' . EOL;
564564
$lineChart = new Scatter();
565+
$lineChart->setIsSmooth(true);
565566
$series = new Series('Downloads', $seriesData);
566567
$series->setShowSeriesName(true);
567568
$series->getMarker()->setSymbol(\PhpOffice\PhpPresentation\Shape\Chart\Marker::SYMBOL_CIRCLE);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This file is part of PHPPresentation - A pure PHP library for reading and writing
4+
* presentations documents.
5+
*
6+
* PHPPresentation is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPPresentation
14+
*
15+
* @copyright 2009-2015 PHPPresentation contributors
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpPresentation\Shape\Chart\Type;
20+
21+
class AbstractTypeLine extends AbstractType
22+
{
23+
/**
24+
* Is Line Smooth?
25+
*
26+
* @var bool
27+
*/
28+
protected $isSmooth = false;
29+
30+
/**
31+
* Is Line Smooth?
32+
*
33+
* @return bool
34+
*/
35+
public function isSmooth(): bool
36+
{
37+
return $this->isSmooth;
38+
}
39+
40+
/**
41+
* Set Line Smoothness
42+
*
43+
* @param bool $value
44+
*
45+
* @return AbstractTypeLine
46+
*/
47+
public function setIsSmooth(bool $value = true): AbstractTypeLine
48+
{
49+
$this->isSmooth = $value;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* Get hash code.
56+
*
57+
* @return string Hash code
58+
*/
59+
public function getHashCode(): string
60+
{
61+
return md5($this->isSmooth() ? '1' : '0');
62+
}
63+
}

src/PhpPresentation/Shape/Chart/Type/Line.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020

2121
use PhpOffice\PhpPresentation\ComparableInterface;
2222

23-
/**
24-
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Line.
25-
*/
26-
class Line extends AbstractType implements ComparableInterface
23+
class Line extends AbstractTypeLine implements ComparableInterface
2724
{
2825
/**
2926
* Get hash code.
@@ -37,6 +34,6 @@ public function getHashCode(): string
3734
$hash .= $series->getHashCode();
3835
}
3936

40-
return md5($hash . __CLASS__);
37+
return md5(parent::getHashCode() . $hash . __CLASS__);
4138
}
4239
}

src/PhpPresentation/Shape/Chart/Type/Scatter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020

2121
use PhpOffice\PhpPresentation\ComparableInterface;
2222

23-
/**
24-
* \PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter.
25-
*/
26-
class Scatter extends AbstractType implements ComparableInterface
23+
class Scatter extends AbstractTypeLine implements ComparableInterface
2724
{
2825
/**
2926
* Get hash code.
@@ -37,6 +34,6 @@ public function getHashCode(): string
3734
$hash .= $series->getHashCode();
3835
}
3936

40-
return md5($hash . __CLASS__);
37+
return md5(parent::getHashCode() . $hash . __CLASS__);
4138
}
4239
}

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
1111
use PhpOffice\PhpPresentation\Shape\Chart\Title;
1212
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeBar;
13+
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeLine;
1314
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypePie;
1415
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;
1516
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar;
@@ -560,6 +561,8 @@ private function writePlotAreaStyle(Chart $chart): void
560561
} elseif ($chartType instanceof Pie3D) {
561562
$this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
562563
$this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
564+
} elseif ($chartType instanceof AbstractTypeLine) {
565+
$this->xmlContent->writeAttributeIf($chartType->isSmooth(), 'chart:interpolation', 'cubic-spline');
563566
}
564567
switch ($chart->getDisplayBlankAs()) {
565568
case Chart::BLANKAS_ZERO:

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,11 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl
18261826
$this->writeMultipleValuesOrReference($objWriter, $includeSheet, $axisYData, $coords);
18271827
$objWriter->endElement();
18281828

1829+
// c:smooth
1830+
$objWriter->startElement('c:smooth');
1831+
$objWriter->writeAttribute('val', $subject->isSmooth() ? '1' : '0');
1832+
$objWriter->endElement();
1833+
18291834
$objWriter->endElement();
18301835

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

1839-
// c:smooth
1840-
$objWriter->startElement('c:smooth');
1841-
$objWriter->writeAttribute('val', '0');
1842-
$objWriter->endElement();
1843-
18441844
// c:axId
18451845
$objWriter->startElement('c:axId');
18461846
$objWriter->writeAttribute('val', '52743552');
@@ -2012,7 +2012,7 @@ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool
20122012

20132013
// c:smooth
20142014
$objWriter->startElement('c:smooth');
2015-
$objWriter->writeAttribute('val', '0');
2015+
$objWriter->writeAttribute('val', $subject->isSmooth() ? '1' : '0');
20162016
$objWriter->endElement();
20172017

20182018
$objWriter->endElement();

tests/PhpPresentation/Tests/Shape/Chart/Type/LineTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,39 @@ public function testData(): void
4141
new Series(),
4242
];
4343

44-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Line', $object->setSeries());
44+
$this->assertInstanceOf(Line::class, $object->setSeries());
4545
$this->assertEmpty($object->getSeries());
46-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Line', $object->setSeries($array));
46+
$this->assertInstanceOf(Line::class, $object->setSeries($array));
4747
$this->assertCount(count($array), $object->getSeries());
4848
}
4949

50-
public function testSerties(): void
50+
public function testSeries(): void
5151
{
5252
$object = new Line();
5353

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

58+
public function testSmooth(): void
59+
{
60+
$object = new Line();
61+
62+
$this->assertFalse($object->isSmooth());
63+
$this->assertInstanceOf(Line::class, $object->setIsSmooth(true));
64+
$this->assertTrue($object->isSmooth());
65+
}
66+
5867
public function testHashCode(): void
5968
{
60-
$oSeries = new Series();
69+
$series = new Series();
6170

6271
$object = new Line();
63-
$object->addSeries($oSeries);
72+
$object->addSeries($series);
6473

65-
$this->assertEquals(md5($oSeries->getHashCode() . get_class($object)), $object->getHashCode());
74+
$this->assertEquals(
75+
md5(md5($object->isSmooth() ? '1' : '0') . $series->getHashCode() . get_class($object)),
76+
$object->getHashCode()
77+
);
6678
}
6779
}

tests/PhpPresentation/Tests/Shape/Chart/Type/ScatterTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,39 @@ public function testData(): void
4141
new Series(),
4242
];
4343

44-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Scatter', $object->setSeries());
44+
$this->assertInstanceOf(Scatter::class, $object->setSeries());
4545
$this->assertEmpty($object->getSeries());
46-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Scatter', $object->setSeries($array));
46+
$this->assertInstanceOf(Scatter::class, $object->setSeries($array));
4747
$this->assertCount(count($array), $object->getSeries());
4848
}
4949

50-
public function testSerties(): void
50+
public function testSeries(): void
5151
{
5252
$object = new Scatter();
5353

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

58+
public function testSmooth(): void
59+
{
60+
$object = new Scatter();
61+
62+
$this->assertFalse($object->isSmooth());
63+
$this->assertInstanceOf(Scatter::class, $object->setIsSmooth(true));
64+
$this->assertTrue($object->isSmooth());
65+
}
66+
5867
public function testHashCode(): void
5968
{
60-
$oSeries = new Series();
69+
$series = new Series();
6170

6271
$object = new Scatter();
63-
$object->addSeries($oSeries);
72+
$object->addSeries($series);
6473

65-
$this->assertEquals(md5($oSeries->getHashCode() . get_class($object)), $object->getHashCode());
74+
$this->assertEquals(
75+
md5(md5($object->isSmooth() ? '1' : '0') . $series->getHashCode() . get_class($object)),
76+
$object->getHashCode()
77+
);
6678
}
6779
}

0 commit comments

Comments
 (0)