Skip to content

Commit 6fae406

Browse files
authored
New Class ChartColor and Refactoring (#2898)
* New Class ChartColor and Refactoring Chart colors are written to Xml in a different manner than font colors, and there are several variations. It will simplify things to create a new class for them. This PR will make use of the new class in Property/Axis/Gridline glow, shadow, and line colors; in Axis fill color; and in Font underline color (used only in charts). It will be used elsewhere in future; in particular, DataSeriesValues, which I will tackle next, will use it for at least one existing and two new properties. This PR is a refactoring; no functionality is added. Some public functions are moved from Properties to ChartColor, but all of these have been introduced after the last release 1.23, so there isn't really any compatibility break. No tests needed to be revised as a result of the source changes. * Simplify Logic in Xlsx/Writer/Chart Minor change.
1 parent 4ae947c commit 6fae406

File tree

7 files changed

+388
-263
lines changed

7 files changed

+388
-263
lines changed

src/PhpSpreadsheet/Chart/Axis.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
*/
1111
class Axis extends Properties
1212
{
13+
public function __construct()
14+
{
15+
parent::__construct();
16+
$this->fillColor = new ChartColor();
17+
}
18+
1319
/**
1420
* Axis Number.
1521
*
@@ -42,13 +48,9 @@ class Axis extends Properties
4248
/**
4349
* Fill Properties.
4450
*
45-
* @var mixed[]
51+
* @var ChartColor
4652
*/
47-
private $fillProperties = [
48-
'type' => self::EXCEL_COLOR_TYPE_ARGB,
49-
'value' => null,
50-
'alpha' => 0,
51-
];
53+
private $fillColor;
5254

5355
private const NUMERIC_FORMAT = [
5456
Properties::FORMAT_CODE_NUMBER,
@@ -163,7 +165,7 @@ public function setAxisOrientation($orientation): void
163165
*/
164166
public function setFillParameters($color, $alpha = null, $AlphaType = self::EXCEL_COLOR_TYPE_ARGB): void
165167
{
166-
$this->fillProperties = $this->setColorProperties($color, $alpha, $AlphaType);
168+
$this->fillColor->setColorProperties($color, $alpha, $AlphaType);
167169
}
168170

169171
/**
@@ -175,19 +177,29 @@ public function setFillParameters($color, $alpha = null, $AlphaType = self::EXCE
175177
*/
176178
public function getFillProperty($property)
177179
{
178-
return (string) $this->fillProperties[$property];
180+
return (string) $this->fillColor->getColorProperty($property);
181+
}
182+
183+
public function getFillColorObject(): ChartColor
184+
{
185+
return $this->fillColor;
179186
}
180187

181188
/**
182189
* Get Line Color Property.
183190
*
191+
* @Deprecated 1.24.0
192+
*
193+
* @See Properties::getLineColorProperty()
194+
* Use the getLineColor property in the Properties class instead
195+
*
184196
* @param string $propertyName
185197
*
186198
* @return null|int|string
187199
*/
188200
public function getLineProperty($propertyName)
189201
{
190-
return $this->lineProperties['color'][$propertyName];
202+
return $this->getLineColorProperty($propertyName);
191203
}
192204

193205
/** @var string */
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Chart;
4+
5+
class ChartColor
6+
{
7+
const EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
8+
const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
9+
const EXCEL_COLOR_TYPE_ARGB = 'srgbClr';
10+
const EXCEL_COLOR_TYPES = [
11+
self::EXCEL_COLOR_TYPE_ARGB,
12+
self::EXCEL_COLOR_TYPE_SCHEME,
13+
self::EXCEL_COLOR_TYPE_STANDARD,
14+
];
15+
16+
/** @var string */
17+
private $value = '';
18+
19+
/** @var string */
20+
private $type = '';
21+
22+
/** @var ?int */
23+
private $alpha;
24+
25+
public function getValue(): string
26+
{
27+
return $this->value;
28+
}
29+
30+
public function setValue(string $value): self
31+
{
32+
$this->value = $value;
33+
34+
return $this;
35+
}
36+
37+
public function getType(): string
38+
{
39+
return $this->type;
40+
}
41+
42+
public function setType(string $type): self
43+
{
44+
$this->type = $type;
45+
46+
return $this;
47+
}
48+
49+
public function getAlpha(): ?int
50+
{
51+
return $this->alpha;
52+
}
53+
54+
public function setAlpha(?int $alpha): self
55+
{
56+
$this->alpha = $alpha;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @param null|float|int|string $alpha
63+
*/
64+
public function setColorProperties(?string $color, $alpha, ?string $type): self
65+
{
66+
if ($color !== null) {
67+
$this->setValue($color);
68+
}
69+
if ($type !== null) {
70+
$this->setType($type);
71+
}
72+
if ($alpha === null) {
73+
$this->setAlpha(null);
74+
} elseif (is_numeric($alpha)) {
75+
$this->setAlpha((int) $alpha);
76+
}
77+
78+
return $this;
79+
}
80+
81+
public function setColorPropertiesArray(array $color): self
82+
{
83+
if (array_key_exists('value', $color) && is_string($color['value'])) {
84+
$this->setValue($color['value']);
85+
}
86+
if (array_key_exists('type', $color) && is_string($color['type'])) {
87+
$this->setType($color['type']);
88+
}
89+
if (array_key_exists('alpha', $color)) {
90+
if ($color['alpha'] === null) {
91+
$this->setAlpha(null);
92+
} elseif (is_numeric($color['alpha'])) {
93+
$this->setAlpha((int) $color['alpha']);
94+
}
95+
}
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* Get Color Property.
102+
*
103+
* @param string $propertyName
104+
*
105+
* @return null|int|string
106+
*/
107+
public function getColorProperty($propertyName)
108+
{
109+
$retVal = null;
110+
if ($propertyName === 'value') {
111+
$retVal = $this->value;
112+
} elseif ($propertyName === 'type') {
113+
$retVal = $this->type;
114+
} elseif ($propertyName === 'alpha') {
115+
$retVal = $this->alpha;
116+
}
117+
118+
return $retVal;
119+
}
120+
121+
public static function alphaToXml(int $alpha): string
122+
{
123+
return (string) (100 - $alpha) . '000';
124+
}
125+
126+
/**
127+
* @param float|int|string $alpha
128+
*/
129+
public static function alphaFromXml($alpha): int
130+
{
131+
return 100 - ((int) $alpha / 1000);
132+
}
133+
}

0 commit comments

Comments
 (0)