Skip to content

Commit 4f22b39

Browse files
authored
Add Support to Chart/Axis and Gridlines for Shadow (#2872)
* Add Support to Chart/Axis and Gridlines for Shadow Continuing the work from #2865. Support is added for Shadow properties for Axis and Gridlines, and Glow and SoftEdges are extended to Gridlines. Tests are added. Some chart tests are moved from Reader/Xlsx and Writer/Xlsx so that most chart tests are under a single directory. This is a minor breaking change. Since the support for these properties was just added, it can't really affect much in userland. Some properties had been stored in the form which the XML requires them rather than as the user would enter them to Excel. So, for example, setting the Glow size to 10 points would have caused it to be stored internally as 127,000. This change will store the size internally as 10, obviously making the appropriate conversion when reading from or writing to XML. This makes unit tests much simpler, and I think this is also what a user would expect, especially considering the difficulties in keeping track of the trailing zeros. * More Tests Confirm value change between internal and xml. * Still More Tests Add a little more coverage, and use a neat trick suggested by @MarkBaker in the discussion of PR #2724 to greatly simplify MultiplierTest.
1 parent 1f1fc36 commit 4f22b39

17 files changed

+1278
-531
lines changed

phpstan-baseline.neon

+2-47
Original file line numberDiff line numberDiff line change
@@ -1170,21 +1170,11 @@ parameters:
11701170
count: 2
11711171
path: src/PhpSpreadsheet/Chart/DataSeries.php
11721172

1173-
-
1174-
message: "#^Parameter \\#1 \\$angle of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setShadowAngle\\(\\) expects int, int\\|null given\\.$#"
1175-
count: 1
1176-
path: src/PhpSpreadsheet/Chart/GridLines.php
1177-
11781173
-
11791174
message: "#^Parameter \\#1 \\$color of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setGlowColor\\(\\) expects string, string\\|null given\\.$#"
11801175
count: 1
11811176
path: src/PhpSpreadsheet/Chart/GridLines.php
11821177

1183-
-
1184-
message: "#^Parameter \\#1 \\$distance of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setShadowDistance\\(\\) expects float, float\\|null given\\.$#"
1185-
count: 1
1186-
path: src/PhpSpreadsheet/Chart/GridLines.php
1187-
11881178
-
11891179
message: "#^Parameter \\#2 \\$alpha of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setGlowColor\\(\\) expects int, int\\|null given\\.$#"
11901180
count: 1
@@ -1275,36 +1265,6 @@ parameters:
12751265
count: 1
12761266
path: src/PhpSpreadsheet/Chart/Properties.php
12771267

1278-
-
1279-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:getTrueAlpha\\(\\) has no return type specified\\.$#"
1280-
count: 1
1281-
path: src/PhpSpreadsheet/Chart/Properties.php
1282-
1283-
-
1284-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:getTrueAlpha\\(\\) has parameter \\$alpha with no type specified\\.$#"
1285-
count: 1
1286-
path: src/PhpSpreadsheet/Chart/Properties.php
1287-
1288-
-
1289-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has no return type specified\\.$#"
1290-
count: 1
1291-
path: src/PhpSpreadsheet/Chart/Properties.php
1292-
1293-
-
1294-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$alpha with no type specified\\.$#"
1295-
count: 1
1296-
path: src/PhpSpreadsheet/Chart/Properties.php
1297-
1298-
-
1299-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$color with no type specified\\.$#"
1300-
count: 1
1301-
path: src/PhpSpreadsheet/Chart/Properties.php
1302-
1303-
-
1304-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$colorType with no type specified\\.$#"
1305-
count: 1
1306-
path: src/PhpSpreadsheet/Chart/Properties.php
1307-
13081268
-
13091269
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Renderer\\\\JpGraph\\:\\:formatDataSetLabels\\(\\) has no return type specified\\.$#"
13101270
count: 1
@@ -4477,12 +4437,12 @@ parameters:
44774437

44784438
-
44794439
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, array\\|int\\|string given\\.$#"
4480-
count: 8
4440+
count: 2
44814441
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
44824442

44834443
-
44844444
message: "#^Parameter \\#2 \\$value of method XMLWriter\\:\\:writeAttribute\\(\\) expects string, array\\|int\\|string\\|null given\\.$#"
4485-
count: 2
4445+
count: 1
44864446
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
44874447

44884448
-
@@ -4525,11 +4485,6 @@ parameters:
45254485
count: 2
45264486
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
45274487

4528-
-
4529-
message: "#^Part \\$xAxis\\-\\>getShadowProperty\\('effect'\\) \\(array\\|int\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#"
4530-
count: 1
4531-
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php
4532-
45334488
-
45344489
message: "#^Part \\$xAxis\\-\\>getShadowProperty\\(\\['color', 'type'\\]\\) \\(array\\|int\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#"
45354490
count: 1

src/PhpSpreadsheet/Chart/Axis.php

+26-30
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,7 @@ class Axis extends Properties
8989
*
9090
* @var mixed[]
9191
*/
92-
private $shadowProperties = [
93-
'presets' => self::SHADOW_PRESETS_NOSHADOW,
94-
'effect' => null,
95-
'color' => [
96-
'type' => self::EXCEL_COLOR_TYPE_STANDARD,
97-
'value' => 'black',
98-
'alpha' => 40,
99-
],
100-
'size' => [
101-
'sx' => null,
102-
'sy' => null,
103-
'kx' => null,
104-
],
105-
'blur' => null,
106-
'direction' => null,
107-
'distance' => null,
108-
'algn' => null,
109-
'rotWithShape' => null,
110-
];
92+
private $shadowProperties = Properties::PRESETS_OPTIONS[0];
11193

11294
/**
11395
* Glow Properties.
@@ -340,6 +322,20 @@ public function getLineStyleArrowLength($arrow)
340322
return $this->getLineStyleArrowSize($this->lineStyleProperties['arrow'][$arrow]['size'], 'len');
341323
}
342324

325+
/**
326+
* @param mixed $value
327+
*/
328+
public function setShadowProperty(string $propertyName, $value): self
329+
{
330+
if ($propertyName === 'color' && is_array($value)) {
331+
$this->setShadowColor($value['value'], $value['alpha'], $value['type']);
332+
} else {
333+
$this->shadowProperties[$propertyName] = $value;
334+
}
335+
336+
return $this;
337+
}
338+
343339
/**
344340
* Set Shadow Properties.
345341
*
@@ -379,6 +375,8 @@ private function setShadowPresetsProperties($presets)
379375
return $this;
380376
}
381377

378+
private const SHADOW_ARRAY_KEYS = ['size', 'color'];
379+
382380
/**
383381
* Set Shadow Properties from Mapped Values.
384382
*
@@ -391,12 +389,10 @@ private function setShadowPropertiesMapValues(array $propertiesMap, &$reference
391389
$base_reference = $reference;
392390
foreach ($propertiesMap as $property_key => $property_val) {
393391
if (is_array($property_val)) {
394-
if ($reference === null) {
392+
if (in_array($property_key, self::SHADOW_ARRAY_KEYS, true)) {
395393
$reference = &$this->shadowProperties[$property_key];
396-
} else {
397-
$reference = &$reference[$property_key];
394+
$this->setShadowPropertiesMapValues($property_val, $reference);
398395
}
399-
$this->setShadowPropertiesMapValues($property_val, $reference);
400396
} else {
401397
if ($base_reference === null) {
402398
$this->shadowProperties[$property_key] = $property_val;
@@ -435,7 +431,7 @@ private function setShadowColor($color, $alpha, $alphaType)
435431
private function setShadowBlur($blur)
436432
{
437433
if ($blur !== null) {
438-
$this->shadowProperties['blur'] = (string) $this->getExcelPointsWidth($blur);
434+
$this->shadowProperties['blur'] = $blur;
439435
}
440436

441437
return $this;
@@ -444,14 +440,14 @@ private function setShadowBlur($blur)
444440
/**
445441
* Set Shadow Angle.
446442
*
447-
* @param null|int $angle
443+
* @param null|float|int $angle
448444
*
449445
* @return $this
450446
*/
451447
private function setShadowAngle($angle)
452448
{
453-
if ($angle !== null) {
454-
$this->shadowProperties['direction'] = (string) $this->getExcelPointsAngle($angle);
449+
if (is_numeric($angle)) {
450+
$this->shadowProperties['direction'] = $angle;
455451
}
456452

457453
return $this;
@@ -467,7 +463,7 @@ private function setShadowAngle($angle)
467463
private function setShadowDistance($distance)
468464
{
469465
if ($distance !== null) {
470-
$this->shadowProperties['distance'] = (string) $this->getExcelPointsWidth($distance);
466+
$this->shadowProperties['distance'] = $distance;
471467
}
472468

473469
return $this;
@@ -525,7 +521,7 @@ public function getGlowProperty($property)
525521
private function setGlowSize($size)
526522
{
527523
if ($size !== null) {
528-
$this->glowProperties['size'] = $this->getExcelPointsWidth($size);
524+
$this->glowProperties['size'] = $size;
529525
}
530526

531527
return $this;
@@ -555,7 +551,7 @@ private function setGlowColor($color, $alpha, $colorType)
555551
public function setSoftEdges($size): void
556552
{
557553
if ($size !== null) {
558-
$this->softEdges['size'] = (string) $this->getExcelPointsWidth($size);
554+
$this->softEdges['size'] = $size;
559555
}
560556
}
561557

0 commit comments

Comments
 (0)