Skip to content

Commit d77db89

Browse files
Feat: adds bar overlap property for 2d bar charts
1 parent 6ecc77c commit d77db89

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

docs/usage/shapes/chart.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,20 @@ $barChart = new Bar();
331331
$barChart->setGapWidthPercent(250);
332332
```
333333

334+
#### Overlap
335+
336+
You can define the bar overlap within bars or columns clusters. It is defined in percent.
337+
The default value is 100% for stacked and 0% for grouped bar charts. The value must be defined between -100 and 100.
338+
339+
When defining the bar grouping type, the default overlap values will be set. Any changes to the overlap must be made after setting the bar grouping type through `setBarGrouping`.
340+
341+
``` php
342+
<?php
343+
344+
$barChart = new Bar();
345+
$barChart->setOverlapWidthPercent(-10);
346+
```
347+
334348
#### Stacking
335349

336350
You can stack multiples series in a same chart. After adding multiples series, you can define the bar grouping with `setBarGrouping` method of AbstractTypeBar.
@@ -418,4 +432,3 @@ $chart->setIsSmooth(false);
418432
// Get status of smooth line
419433
$chart->isSmooth();
420434
```
421-

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class AbstractTypeBar extends AbstractType
5353
*/
5454
protected $gapWidthPercent = 150;
5555

56+
/**
57+
* Overlap within bar or columns clusters. Value between 100 and -100 percent.
58+
* For stacked bar charts, the default overlap will be 100, for grouped bar charts 0.
59+
*
60+
* @var int
61+
*/
62+
protected $overlapWidthPercent = 0;
63+
5664
/**
5765
* Set bar orientation.
5866
*
@@ -87,6 +95,11 @@ public function getBarDirection()
8795
public function setBarGrouping($value = self::GROUPING_CLUSTERED)
8896
{
8997
$this->barGrouping = $value;
98+
$this->overlapWidthPercent = 0;
99+
100+
if( $value === self::GROUPING_STACKED || $value === self::GROUPING_PERCENTSTACKED ) {
101+
$this->overlapWidthPercent = 100;
102+
}
90103

91104
return $this;
92105
}
@@ -127,6 +140,32 @@ public function setGapWidthPercent($gapWidthPercent)
127140
return $this;
128141
}
129142

143+
/**
144+
* @return int
145+
*/
146+
public function getOverlapWidthPercent()
147+
{
148+
return $this->overlapWidthPercent;
149+
}
150+
151+
/**
152+
* @param int $overlapWidthPercent
153+
*
154+
* @return $this
155+
*/
156+
public function setOverlapWidthPercent($overlapWidthPercent)
157+
{
158+
if ($overlapWidthPercent < -100) {
159+
$overlapWidthPercent = -100;
160+
}
161+
if ($overlapWidthPercent > 100) {
162+
$overlapWidthPercent = 100;
163+
}
164+
$this->overlapWidthPercent = $overlapWidthPercent;
165+
166+
return $this;
167+
}
168+
130169
/**
131170
* Get hash code.
132171
*

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,8 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ
985985
$objWriter->endElement();
986986

987987
// c:overlap
988-
$barGrouping = $subject->getBarGrouping();
989988
$objWriter->startElement('c:overlap');
990-
if (Bar::GROUPING_CLUSTERED === $barGrouping) {
991-
$objWriter->writeAttribute('val', '0');
992-
} elseif (Bar::GROUPING_STACKED === $barGrouping || Bar::GROUPING_PERCENTSTACKED === $barGrouping) {
993-
$objWriter->writeAttribute('val', '100');
994-
}
989+
$objWriter->writeAttribute('val', $subject->getOverlapWidthPercent());
995990
$objWriter->endElement();
996991

997992
// c:axId

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ public function testGapWidthPercent(): void
9090
$this->assertEquals(500, $object->getGapWidthPercent());
9191
}
9292

93+
public function testOverlapWidthPercentDefaults(): void
94+
{
95+
$object = new Bar();
96+
$this->assertEquals(0, $object->getOverlapWidthPercent());
97+
98+
$object->setBarGrouping(Bar::GROUPING_STACKED);
99+
$this->assertEquals(100, $object->getOverlapWidthPercent());
100+
$object->setBarGrouping(Bar::GROUPING_CLUSTERED);
101+
$this->assertEquals(0, $object->getOverlapWidthPercent());
102+
$object->setBarGrouping(Bar::GROUPING_PERCENTSTACKED);
103+
$this->assertEquals(100, $object->getOverlapWidthPercent());
104+
}
105+
106+
public function testOverlapWidthPercent(): void
107+
{
108+
$value = mt_rand(-100, 100);
109+
$object = new Bar();
110+
$this->assertEquals(0, $object->getOverlapWidthPercent());
111+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setOverlapWidthPercent($value));
112+
$this->assertEquals($value, $object->getOverlapWidthPercent());
113+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setOverlapWidthPercent(101));
114+
$this->assertEquals(100, $object->getOverlapWidthPercent());
115+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\Bar', $object->setOverlapWidthPercent(-101));
116+
$this->assertEquals(-100, $object->getOverlapWidthPercent());
117+
}
118+
93119
public function testHashCode(): void
94120
{
95121
$oSeries = new Series();

tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ public function testTypeBar(): void
582582
$oShape->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80);
583583
$oBar = new Bar();
584584
$oBar->setGapWidthPercent($valueGapWidthPercent);
585+
$valueOverlapWidthPercent = -10;
586+
$oBar->setOverlapWidthPercent($valueOverlapWidthPercent);
585587
$oSeries = new Series('Downloads', $this->seriesData);
586588
$oSeries->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
587589
$oSeries->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKBLUE));
@@ -603,6 +605,8 @@ public function testTypeBar(): void
603605
$this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $oSeries->getTitle());
604606
$element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:gapWidth';
605607
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $valueGapWidthPercent);
608+
$element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:overlap';
609+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $valueOverlapWidthPercent);
606610

607611
$this->assertIsSchemaECMA376Valid();
608612
}

0 commit comments

Comments
 (0)