Skip to content

Commit fc8966e

Browse files
authored
Support for configuring a Chart Title's overlay (#3325)
* Update PR template * Trim trailing space * Support for configuring a Chart Title's overlay * Provide test coverage for getting/setting a Chart Legend's overlay Add missing docblock
1 parent c2f806b commit fc8966e

File tree

9 files changed

+227
-19
lines changed

9 files changed

+227
-19
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
This is:
22

3-
```
43
- [ ] a bugfix
54
- [ ] a new feature
65
- [ ] refactoring
76
- [ ] additional unit tests
8-
```
97

108
Checklist:
119

@@ -14,7 +12,7 @@ Checklist:
1412
- [ ] New unit tests have been added
1513
- [ ] Code style is respected
1614
- [ ] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
17-
- [ ] CHANGELOG.md contains a short summary of the change
15+
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
1816
- [ ] Documentation is updated as necessary
1917

2018
### Why this change is needed?

CHANGELOG.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12-
- Nothing
12+
- Support for configuring a Chart Title's overlay [PR #3325](https://github.com/PHPOffice/PhpSpreadsheet/pull/3325)
1313

1414
### Changed
1515

@@ -167,7 +167,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
167167

168168
### Fixed
169169

170-
- Fix update to defined names when inserting/deleting rows/columns [Issue #3076](https://github.com/PHPOffice/PhpSpreadsheet/issues/3076) [PR #3077](https://github.com/PHPOffice/PhpSpreadsheet/pull/3077)
170+
- Fix update to defined names when inserting/deleting rows/columns [Issue #3076](https://github.com/PHPOffice/PhpSpreadsheet/issues/3076) [PR #3077](https://github.com/PHPOffice/PhpSpreadsheet/pull/3077)
171171
- Fix DataValidation sqRef when inserting/deleting rows/columns [Issue #3056](https://github.com/PHPOffice/PhpSpreadsheet/issues/3056) [PR #3074](https://github.com/PHPOffice/PhpSpreadsheet/pull/3074)
172172
- Named ranges not usable as anchors in OFFSET function [Issue #3013](https://github.com/PHPOffice/PhpSpreadsheet/issues/3013)
173173
- Fully flatten an array [Issue #2955](https://github.com/PHPOffice/PhpSpreadsheet/issues/2955) [PR #2956](https://github.com/PHPOffice/PhpSpreadsheet/pull/2956)
@@ -357,7 +357,7 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
357357
- Use color palette if supplied [Issue #2499](https://github.com/PHPOffice/PhpSpreadsheet/issues/2499) [PR #2595](https://github.com/PHPOffice/PhpSpreadsheet/pull/2595)
358358
- Xls reader treat drawing offsets as int rather than float [PR #2648](https://github.com/PHPOffice/PhpSpreadsheet/pull/2648)
359359
- Handle booleans in conditional styles properly [PR #2654](https://github.com/PHPOffice/PhpSpreadsheet/pull/2654)
360-
- Fix for reading files in the root directory of a ZipFile, which should not be prefixed by relative paths ("./") as dirname($filename) does by default.
360+
- Fix for reading files in the root directory of a ZipFile, which should not be prefixed by relative paths ("./") as dirname($filename) does by default.
361361
- Fix invalid style of cells in empty columns with columnDimensions and rows with rowDimensions in added external sheet. [PR #2739](https://github.com/PHPOffice/PhpSpreadsheet/pull/2739)
362362
- Time Interval Formatting [Issue #2768](https://github.com/PHPOffice/PhpSpreadsheet/issues/2768) [PR #2772](https://github.com/PHPOffice/PhpSpreadsheet/pull/2772)
363363

@@ -370,9 +370,9 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
370370

371371
- Improved support for passing of array arguments to Excel function implementations to return array results (where appropriate). [Issue #2551](https://github.com/PHPOffice/PhpSpreadsheet/issues/2551)
372372

373-
This is the first stage in an ongoing process of adding array support to all appropriate function implementations,
373+
This is the first stage in an ongoing process of adding array support to all appropriate function implementations,
374374
- Support for the Excel365 Math/Trig SEQUENCE() function [PR #2536](https://github.com/PHPOffice/PhpSpreadsheet/pull/2536)
375-
- Support for the Excel365 Math/Trig RANDARRAY() function [PR #2540](https://github.com/PHPOffice/PhpSpreadsheet/pull/2540)
375+
- Support for the Excel365 Math/Trig RANDARRAY() function [PR #2540](https://github.com/PHPOffice/PhpSpreadsheet/pull/2540)
376376

377377
Note that the Spill Operator is not yet supported in the Calculation Engine; but this can still be useful for defining array constants.
378378
- Improved support for Conditional Formatting Rules [PR #2491](https://github.com/PHPOffice/PhpSpreadsheet/pull/2491)
@@ -386,7 +386,7 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
386386
- Expression
387387
- Provision of CF Wizards (for all the above listed rule types) to help create/modify CF Rules without having to manage all the combinations of types/operators, and the complexities of formula expressions, or the text/timePeriod attributes.
388388

389-
See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/conditional-formatting/) for details
389+
See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/conditional-formatting/) for details
390390

391391
- Full support of the above CF Rules for the Xlsx Reader and Writer; even when the file being loaded has CF rules listed in the `<extLst><ext><ConditionalFormattings>` element for the worksheet rather than the `<ConditionalFormatting>` element.
392392
- Provision of a CellMatcher to identify if rules are matched for a cell, and which matching style will be applied.
@@ -576,7 +576,7 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
576576
- Xls Reader changing grey background to black in Excel template [Issue #2147](https://github.com/PHPOffice/PhpSpreadsheet/issues/2147) [PR #2156](https://github.com/PHPOffice/PhpSpreadsheet/pull/2156)
577577
- Column width and Row height styles in the Html Reader when the value includes a unit of measure [Issue #2145](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145).
578578
- Data Validation flags not set correctly when reading XLSX files [Issue #2224](https://github.com/PHPOffice/PhpSpreadsheet/issues/2224) [PR #2225](https://github.com/PHPOffice/PhpSpreadsheet/pull/2225)
579-
- Reading XLSX files without styles.xml throws an exception [Issue #2246](https://github.com/PHPOffice/PhpSpreadsheet/issues/2246)
579+
- Reading XLSX files without styles.xml throws an exception [Issue #2246](https://github.com/PHPOffice/PhpSpreadsheet/issues/2246)
580580
- Improved performance of `Style::applyFromArray()` when applied to several cells [PR #1785](https://github.com/PHPOffice/PhpSpreadsheet/issues/1785).
581581
- Improve XLSX parsing speed if no readFilter is applied (again) - [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772)
582582

@@ -641,7 +641,7 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
641641
`ABS()`, `ACOS()`, `ACOSH()`, `ASIN()`, `ASINH()`, `ATAN()`, `ATANH()`,
642642
`COS()`, `COSH()`, `DEGREES()` (rad2deg), `EXP()`, `LN()` (log), `LOG10()`,
643643
`RADIANS()` (deg2rad), `SIN()`, `SINH()`, `SQRT()`, `TAN()`, `TANH()`.
644-
644+
645645
One TextData function is also affected: `REPT()` (str_repeat).
646646
- `formatAsDate` correctly matches language metadata, reverting c55272e
647647
- Formulae that previously crashed on sub function call returning excel error value now return said value.
@@ -1271,7 +1271,7 @@ Note that this will be the last 1.x branch release before the 2.x release. We wi
12711271
- Ignore inlineStr type if formula element exists - @ncrypthic [#570](https://github.com/PHPOffice/PHPExcel/issues/570)
12721272
- Excel 2007 Reader freezes because of conditional formatting - @rentalhost [#575](https://github.com/PHPOffice/PHPExcel/issues/575)
12731273
- Readers will now parse files containing worksheet titles over 31 characters [#176](https://github.com/PHPOffice/PhpSpreadsheet/pull/176)
1274-
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625)
1274+
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625)
12751275

12761276
### General
12771277

15.6 KB
Binary file not shown.

src/PhpSpreadsheet/Chart/Legend.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Legend
5252
* Create a new Legend.
5353
*
5454
* @param string $position
55+
* @param ?Layout $layout
5556
* @param bool $overlay
5657
*/
5758
public function __construct($position = self::POSITION_RIGHT, ?Layout $layout = null, $overlay = false)

src/PhpSpreadsheet/Chart/Title.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class Title
1313
*/
1414
private $caption = '';
1515

16+
/**
17+
* Allow overlay of other elements?
18+
*
19+
* @var bool
20+
*/
21+
private $overlay = true;
22+
1623
/**
1724
* Title Layout.
1825
*
@@ -24,11 +31,14 @@ class Title
2431
* Create a new Title.
2532
*
2633
* @param array|RichText|string $caption
34+
* @param ?Layout $layout
35+
* @param bool $overlay
2736
*/
28-
public function __construct($caption = '', ?Layout $layout = null)
37+
public function __construct($caption = '', ?Layout $layout = null, $overlay = false)
2938
{
3039
$this->caption = $caption;
3140
$this->layout = $layout;
41+
$this->setOverlay($overlay);
3242
}
3343

3444
/**
@@ -78,6 +88,26 @@ public function setCaption($caption)
7888
return $this;
7989
}
8090

91+
/**
92+
* Get allow overlay of other elements?
93+
*
94+
* @return bool
95+
*/
96+
public function getOverlay()
97+
{
98+
return $this->overlay;
99+
}
100+
101+
/**
102+
* Set allow overlay of other elements?
103+
*
104+
* @param bool $overlay
105+
*/
106+
public function setOverlay($overlay): void
107+
{
108+
$this->overlay = $overlay;
109+
}
110+
81111
public function getLayout(): ?Layout
82112
{
83113
return $this->layout;

src/PhpSpreadsheet/Reader/Xlsx/Chart.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ private function chartTitle(SimpleXMLElement $titleDetails): Title
402402
{
403403
$caption = [];
404404
$titleLayout = null;
405+
$titleOverlay = false;
405406
foreach ($titleDetails as $titleDetailKey => $chartDetail) {
406407
$chartDetail = Xlsx::testSimpleXml($chartDetail);
407408
switch ($titleDetailKey) {
@@ -424,6 +425,10 @@ private function chartTitle(SimpleXMLElement $titleDetails): Title
424425
}
425426
}
426427

428+
break;
429+
case 'overlay':
430+
$titleOverlay = self::getAttribute($chartDetail, 'val', 'boolean');
431+
427432
break;
428433
case 'layout':
429434
$titleLayout = $this->chartLayoutDetails($chartDetail);
@@ -432,7 +437,7 @@ private function chartTitle(SimpleXMLElement $titleDetails): Title
432437
}
433438
}
434439

435-
return new Title($caption, $titleLayout);
440+
return new Title($caption, $titleLayout, (bool) $titleOverlay);
436441
}
437442

438443
private function chartLayoutDetails(SimpleXMLElement $chartDetail): ?Layout

src/PhpSpreadsheet/Writer/Xlsx/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function writeTitle(XMLWriter $objWriter, ?Title $title = null): void
174174
$this->writeLayout($objWriter, $title->getLayout());
175175

176176
$objWriter->startElement('c:overlay');
177-
$objWriter->writeAttribute('val', '0');
177+
$objWriter->writeAttribute('val', ($title->getOverlay()) ? '1' : '0');
178178
$objWriter->endElement();
179179

180180
$objWriter->endElement();

tests/PhpSpreadsheetTests/Chart/LegendTest.php

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,84 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Chart;
44

55
use PhpOffice\PhpSpreadsheet\Chart\Legend;
6-
use PHPUnit\Framework\TestCase;
6+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
7+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
8+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
79

8-
class LegendTest extends TestCase
10+
class LegendTest extends AbstractFunctional
911
{
12+
private const DIRECTORY = 'samples' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
13+
14+
public function readCharts(XlsxReader $reader): void
15+
{
16+
$reader->setIncludeCharts(true);
17+
}
18+
19+
public function writeCharts(XlsxWriter $writer): void
20+
{
21+
$writer->setIncludeCharts(true);
22+
}
23+
24+
public function testLegendWithOverlay(): void
25+
{
26+
$file = self::DIRECTORY . 'chart-with-and-without-overlays.xlsx';
27+
$reader = new XlsxReader();
28+
$reader->setIncludeCharts(true);
29+
$spreadsheet = $reader->load($file);
30+
$sheetName = 'With Overlay';
31+
$sheet = $spreadsheet->getSheetByName($sheetName);
32+
self::assertNotNull($sheet);
33+
self::assertSame(1, $sheet->getChartCount());
34+
/** @var callable */
35+
$callableReader = [$this, 'readCharts'];
36+
/** @var callable */
37+
$callableWriter = [$this, 'writeCharts'];
38+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
39+
$spreadsheet->disconnectWorksheets();
40+
41+
$sheet = $reloadedSpreadsheet->getSheetByName($sheetName);
42+
self::assertNotNull($sheet);
43+
$charts = $sheet->getChartCollection();
44+
self::assertCount(1, $charts);
45+
$chart = $charts[0];
46+
self::assertNotNull($chart);
47+
$legend = $chart->getLegend();
48+
self::assertNotNull($legend);
49+
self::assertTrue($legend->getOverlay());
50+
51+
$reloadedSpreadsheet->disconnectWorksheets();
52+
}
53+
54+
public function testLegendWithoutOverlay(): void
55+
{
56+
$file = self::DIRECTORY . 'chart-with-and-without-overlays.xlsx';
57+
$reader = new XlsxReader();
58+
$reader->setIncludeCharts(true);
59+
$spreadsheet = $reader->load($file);
60+
$sheetName = 'Without Overlay';
61+
$sheet = $spreadsheet->getSheetByName($sheetName);
62+
self::assertNotNull($sheet);
63+
self::assertSame(1, $sheet->getChartCount());
64+
/** @var callable */
65+
$callableReader = [$this, 'readCharts'];
66+
/** @var callable */
67+
$callableWriter = [$this, 'writeCharts'];
68+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
69+
$spreadsheet->disconnectWorksheets();
70+
71+
$sheet = $reloadedSpreadsheet->getSheetByName($sheetName);
72+
self::assertNotNull($sheet);
73+
$charts = $sheet->getChartCollection();
74+
self::assertCount(1, $charts);
75+
$chart = $charts[0];
76+
self::assertNotNull($chart);
77+
$legend = $chart->getLegend();
78+
self::assertNotNull($legend);
79+
self::assertFalse($legend->getOverlay());
80+
81+
$reloadedSpreadsheet->disconnectWorksheets();
82+
}
83+
1084
public function testSetPosition(): void
1185
{
1286
$positionValues = [

0 commit comments

Comments
 (0)