Skip to content

Commit 6a57c84

Browse files
authored
Merge pull request #667 from mindline-analytics/feat-bar-overlap
Added bar overlap property for 2d bar charts
2 parents 3ed8e2b + 0022f5b commit 6a57c84

File tree

7 files changed

+101
-23
lines changed

7 files changed

+101
-23
lines changed

docs/changes/1.0.0.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- PowerPoint2007 Reader : Load images in their initial format (and not by default in PNG) - [@polidog](https://github.com/polidog) GH-553
1818

1919
## Features
20+
- Support for bar overlap in 2D bar charts - [@mindline](https://github.com/mindline-analytics) GH-667
21+
- PowerPoint2007 Writer
2022
- Support for the position of Legend in ODPresentation Writer - [@Progi1984](https://github.com/Progi1984) GH-355
2123
- Support for DoughnutChart - [@Progi1984](https://github.com/Progi1984) GH-355
2224
- ODPresentation Writer
@@ -104,4 +106,4 @@
104106
* `PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault`
105107
* Removed class
106108
* `PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\TemplateBased`
107-
* Removed class
109+
* Removed class

docs/images/chart_bar_overlap.png

37.9 KB
Loading

docs/usage/shapes/chart.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $chartShape = $slide->createChartShape();
1414

1515
You can define how blank values are displayed with the method `setDisplayBlankAs`.
1616

17-
![Slideshow type](/images/libreoffice_chart_displayblankas.png)
17+
![Slideshow type](../../images/libreoffice_chart_displayblankas.png)
1818

1919
Differents types are available:
2020

@@ -321,16 +321,37 @@ TODO
321321

322322
#### Gap Width
323323

324-
You can define the gap width between bar or columns clusters. It is defined in percent.
324+
You can define the gap width between bar or columns clusters. It is relatively defined as percentage of a bars width.
325325
The default value is 150%. The value must be defined between 0 and 500.
326326

327-
``` php
327+
```php
328328
<?php
329329

330330
$barChart = new Bar();
331331
$barChart->setGapWidthPercent(250);
332332
```
333333

334+
#### Overlap
335+
336+
You can define the bar overlap within bar or column clusters. It is relatively defined as percentage of a bars width.
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 setting the bar grouping type, the default overlap values will be set. Any change to the overlap must be made after setting the bar grouping type through `setBarGrouping`.
340+
341+
```php
342+
$barChart = new Bar();
343+
// will set the overlap to the default value for grouped bars: 0
344+
$barChart->setBarGrouping(Bar::GROUPING_CLUSTERED);
345+
346+
// a positive value will result in an overlapping
347+
$barChart->setOverlapWidthPercent(25);
348+
349+
// a negative value will result in a gap
350+
$barChart->setOverlapWidthPercent(-25);
351+
```
352+
353+
![Bar Overlap](../../images/chart_bar_overlap.png)
354+
334355
#### Stacking
335356

336357
You can stack multiples series in a same chart. After adding multiples series, you can define the bar grouping with `setBarGrouping` method of AbstractTypeBar.
@@ -349,18 +370,11 @@ $barChart->setBarGrouping(Bar::GROUPING_STACKED);
349370
$barChart->setBarGrouping(Bar::GROUPING_PERCENTSTACKED);
350371
```
351372

352-
- Bar::GROUPING_CLUSTERED
353-
354-
![Bar::GROUPING_CLUSTERED](/images/chart_columns_52x60.png)
355-
356-
- Bar::GROUPING_STACKED
357-
358-
![Bar::GROUPING_STACKED](/images/chart_columnstack_52x60.png)
359-
360-
- Bar::GROUPING_PERCENTSTACKED
361-
362-
![Bar::GROUPING_PERCENTSTACKED](/images/chart_columnpercent_52x60.png)
363-
373+
| | Type | Constant |
374+
| --------------------------------------------------------------------------- | ------------ | ---------------------------- |
375+
| ![Bar::GROUPING_CLUSTERED](../../images/chart_columns_52x60.png) | Grouped Bars | Bar::GROUPING_CLUSTERED |
376+
| ![Bar::GROUPING_STACKED](../../images/chart_columnstack_52x60.png) | Stacked Bars | Bar::GROUPING_STACKED |
377+
| ![Bar::GROUPING_PERCENTSTACKED](../../images/chart_columnpercent_52x60.png) | Grouped Bars | Bar::GROUPING_PERCENTSTACKED |
364378

365379
### Line
366380

@@ -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(): int
147+
{
148+
return $this->overlapWidthPercent;
149+
}
150+
151+
/**
152+
* @param int $value overlap width percentage
153+
*
154+
* @return self
155+
*/
156+
public function setOverlapWidthPercent(int $value): self
157+
{
158+
if ($value < -100) {
159+
$value = -100;
160+
}
161+
if ($value > 100) {
162+
$value = 100;
163+
}
164+
$this->overlapWidthPercent = $value;
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(Bar::class, $object->setOverlapWidthPercent($value));
112+
$this->assertEquals($value, $object->getOverlapWidthPercent());
113+
$this->assertInstanceOf(Bar::class, $object->setOverlapWidthPercent(101));
114+
$this->assertEquals(100, $object->getOverlapWidthPercent());
115+
$this->assertInstanceOf(Bar::class, $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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ 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+
$oBar->setOverlapWidthPercent(-10);
585586
$oSeries = new Series('Downloads', $this->seriesData);
586587
$oSeries->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
587588
$oSeries->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKBLUE));
@@ -603,6 +604,8 @@ public function testTypeBar(): void
603604
$this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $oSeries->getTitle());
604605
$element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:gapWidth';
605606
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $valueGapWidthPercent);
607+
$element = '/c:chartSpace/c:chart/c:plotArea/c:barChart/c:overlap';
608+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', $oBar->getOverlapWidthPercent());
606609

607610
$this->assertIsSchemaECMA376Valid();
608611
}

0 commit comments

Comments
 (0)