Skip to content

Commit

Permalink
Add Support to Chart/Axis and Gridlines for Shadow (#2872)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
oleibman authored Jun 10, 2022
1 parent 1f1fc36 commit 4f22b39
Show file tree
Hide file tree
Showing 17 changed files with 1,278 additions and 531 deletions.
49 changes: 2 additions & 47 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1170,21 +1170,11 @@ parameters:
count: 2
path: src/PhpSpreadsheet/Chart/DataSeries.php

-
message: "#^Parameter \\#1 \\$angle of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setShadowAngle\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/GridLines.php

-
message: "#^Parameter \\#1 \\$color of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setGlowColor\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/GridLines.php

-
message: "#^Parameter \\#1 \\$distance of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setShadowDistance\\(\\) expects float, float\\|null given\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/GridLines.php

-
message: "#^Parameter \\#2 \\$alpha of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setGlowColor\\(\\) expects int, int\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -1275,36 +1265,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:getTrueAlpha\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:getTrueAlpha\\(\\) has parameter \\$alpha with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$alpha with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$color with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Properties\\:\\:setColorProperties\\(\\) has parameter \\$colorType with no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Properties.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Renderer\\\\JpGraph\\:\\:formatDataSetLabels\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -4477,12 +4437,12 @@ parameters:

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

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

-
Expand Down Expand Up @@ -4525,11 +4485,6 @@ parameters:
count: 2
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php

-
message: "#^Part \\$xAxis\\-\\>getShadowProperty\\('effect'\\) \\(array\\|int\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/PhpSpreadsheet/Writer/Xlsx/Chart.php

-
message: "#^Part \\$xAxis\\-\\>getShadowProperty\\(\\['color', 'type'\\]\\) \\(array\\|int\\|string\\|null\\) of encapsed string cannot be cast to string\\.$#"
count: 1
Expand Down
56 changes: 26 additions & 30 deletions src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,7 @@ class Axis extends Properties
*
* @var mixed[]
*/
private $shadowProperties = [
'presets' => self::SHADOW_PRESETS_NOSHADOW,
'effect' => null,
'color' => [
'type' => self::EXCEL_COLOR_TYPE_STANDARD,
'value' => 'black',
'alpha' => 40,
],
'size' => [
'sx' => null,
'sy' => null,
'kx' => null,
],
'blur' => null,
'direction' => null,
'distance' => null,
'algn' => null,
'rotWithShape' => null,
];
private $shadowProperties = Properties::PRESETS_OPTIONS[0];

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

/**
* @param mixed $value
*/
public function setShadowProperty(string $propertyName, $value): self
{
if ($propertyName === 'color' && is_array($value)) {
$this->setShadowColor($value['value'], $value['alpha'], $value['type']);
} else {
$this->shadowProperties[$propertyName] = $value;
}

return $this;
}

/**
* Set Shadow Properties.
*
Expand Down Expand Up @@ -379,6 +375,8 @@ private function setShadowPresetsProperties($presets)
return $this;
}

private const SHADOW_ARRAY_KEYS = ['size', 'color'];

/**
* Set Shadow Properties from Mapped Values.
*
Expand All @@ -391,12 +389,10 @@ private function setShadowPropertiesMapValues(array $propertiesMap, &$reference
$base_reference = $reference;
foreach ($propertiesMap as $property_key => $property_val) {
if (is_array($property_val)) {
if ($reference === null) {
if (in_array($property_key, self::SHADOW_ARRAY_KEYS, true)) {
$reference = &$this->shadowProperties[$property_key];
} else {
$reference = &$reference[$property_key];
$this->setShadowPropertiesMapValues($property_val, $reference);
}
$this->setShadowPropertiesMapValues($property_val, $reference);
} else {
if ($base_reference === null) {
$this->shadowProperties[$property_key] = $property_val;
Expand Down Expand Up @@ -435,7 +431,7 @@ private function setShadowColor($color, $alpha, $alphaType)
private function setShadowBlur($blur)
{
if ($blur !== null) {
$this->shadowProperties['blur'] = (string) $this->getExcelPointsWidth($blur);
$this->shadowProperties['blur'] = $blur;
}

return $this;
Expand All @@ -444,14 +440,14 @@ private function setShadowBlur($blur)
/**
* Set Shadow Angle.
*
* @param null|int $angle
* @param null|float|int $angle
*
* @return $this
*/
private function setShadowAngle($angle)
{
if ($angle !== null) {
$this->shadowProperties['direction'] = (string) $this->getExcelPointsAngle($angle);
if (is_numeric($angle)) {
$this->shadowProperties['direction'] = $angle;
}

return $this;
Expand All @@ -467,7 +463,7 @@ private function setShadowAngle($angle)
private function setShadowDistance($distance)
{
if ($distance !== null) {
$this->shadowProperties['distance'] = (string) $this->getExcelPointsWidth($distance);
$this->shadowProperties['distance'] = $distance;
}

return $this;
Expand Down Expand Up @@ -525,7 +521,7 @@ public function getGlowProperty($property)
private function setGlowSize($size)
{
if ($size !== null) {
$this->glowProperties['size'] = $this->getExcelPointsWidth($size);
$this->glowProperties['size'] = $size;
}

return $this;
Expand Down Expand Up @@ -555,7 +551,7 @@ private function setGlowColor($color, $alpha, $colorType)
public function setSoftEdges($size): void
{
if ($size !== null) {
$this->softEdges['size'] = (string) $this->getExcelPointsWidth($size);
$this->softEdges['size'] = $size;
}
}

Expand Down
Loading

0 comments on commit 4f22b39

Please sign in to comment.