Skip to content

Commit 79a780b

Browse files
committed
Some Fixes for Scatter Charts
Chart issues have been pouring in recently. This is a partial response to issue PHPOffice#2762. It implements "no joins" for scatter charts, as well as having the reader and writer handle "point size", "line width", and "color" for markers. A new boolean property `scatterLines`, with setter and getter, is added to DataSeriesValues to handle joins (default is true which means scatter plot points *are* joined by lines). Some, but not yet all, default font properties for the chart title are handled (color and, surprisingly, font name present challenges). With these changes, sample 32readwriteScatterChart1.xlsx now looks closer to its source. There are still some differences (x-axis changes), but I think this change is already large enough. I can work on the other problems later. The code for reading charts has not yet been converted to be namespace aware. Having a tiny island of aware code in a sea of unaware makes no sense to me, so some of the new code is likewise unaware. I hope to be able to get to it eventually, but, among other considerations, it is difficult to generate suitable test cases.
1 parent 070bc68 commit 79a780b

File tree

5 files changed

+152
-97
lines changed

5 files changed

+152
-97
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,26 +1285,6 @@ parameters:
12851285
count: 2
12861286
path: src/PhpSpreadsheet/Chart/DataSeries.php
12871287

1288-
-
1289-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\DataSeriesValues\\:\\:refresh\\(\\) has parameter \\$flatten with no type specified\\.$#"
1290-
count: 1
1291-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
1292-
1293-
-
1294-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\DataSeriesValues\\:\\:\\$dataSource \\(string\\) does not accept string\\|null\\.$#"
1295-
count: 1
1296-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
1297-
1298-
-
1299-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\DataSeriesValues\\:\\:\\$dataTypeValues has no type specified\\.$#"
1300-
count: 1
1301-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
1302-
1303-
-
1304-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\DataSeriesValues\\:\\:\\$fillColor \\(array\\<string\\>\\|string\\) does not accept array\\<string\\>\\|string\\|null\\.$#"
1305-
count: 1
1306-
path: src/PhpSpreadsheet/Chart/DataSeriesValues.php
1307-
13081288
-
13091289
message: "#^Parameter \\#1 \\$angle of method PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\:\\:setShadowAngle\\(\\) expects int, int\\|null given\\.$#"
13101290
count: 1
@@ -2630,56 +2610,6 @@ parameters:
26302610
count: 1
26312611
path: src/PhpSpreadsheet/Reader/Xlsx/BaseParserClass.php
26322612

2633-
-
2634-
message: "#^Cannot call method getFont\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\Run\\|null\\.$#"
2635-
count: 12
2636-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2637-
2638-
-
2639-
message: "#^Cannot call method setBold\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2640-
count: 1
2641-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2642-
2643-
-
2644-
message: "#^Cannot call method setColor\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2645-
count: 1
2646-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2647-
2648-
-
2649-
message: "#^Cannot call method setItalic\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2650-
count: 1
2651-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2652-
2653-
-
2654-
message: "#^Cannot call method setName\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2655-
count: 1
2656-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2657-
2658-
-
2659-
message: "#^Cannot call method setSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2660-
count: 1
2661-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2662-
2663-
-
2664-
message: "#^Cannot call method setStrikethrough\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2665-
count: 2
2666-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2667-
2668-
-
2669-
message: "#^Cannot call method setSubscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2670-
count: 1
2671-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2672-
2673-
-
2674-
message: "#^Cannot call method setSuperscript\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2675-
count: 1
2676-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2677-
2678-
-
2679-
message: "#^Cannot call method setUnderline\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
2680-
count: 3
2681-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2682-
26832613
-
26842614
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Chart\\:\\:chartDataSeries\\(\\) has no return type specified\\.$#"
26852615
count: 1
@@ -2705,11 +2635,6 @@ parameters:
27052635
count: 1
27062636
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
27072637

2708-
-
2709-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Chart\\:\\:chartDataSeriesValueSet\\(\\) has parameter \\$marker with no type specified\\.$#"
2710-
count: 1
2711-
path: src/PhpSpreadsheet/Reader/Xlsx/Chart.php
2712-
27132638
-
27142639
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\\\Chart\\:\\:chartDataSeriesValueSet\\(\\) has parameter \\$namespacesChartMeta with no type specified\\.$#"
27152640
count: 1
@@ -5087,7 +5012,7 @@ parameters:
50875012

50885013
-
50895014
message: "#^Cannot call method getSize\\(\\) on PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null\\.$#"
5090-
count: 1
5015+
count: 2
50915016
path: src/PhpSpreadsheet/Writer/Xlsx/StringTable.php
50925017

50935018
-

src/PhpSpreadsheet/Chart/DataSeriesValues.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DataSeriesValues
1212
const DATASERIES_TYPE_STRING = 'String';
1313
const DATASERIES_TYPE_NUMBER = 'Number';
1414

15-
private static $dataTypeValues = [
15+
private const DATA_TYPE_VALUES = [
1616
self::DATASERIES_TYPE_STRING,
1717
self::DATASERIES_TYPE_NUMBER,
1818
];
@@ -69,7 +69,7 @@ class DataSeriesValues
6969
/**
7070
* Fill color (can be array with colors if dataseries have custom colors).
7171
*
72-
* @var string|string[]
72+
* @var null|string|string[]
7373
*/
7474
private $fillColor;
7575

@@ -80,6 +80,9 @@ class DataSeriesValues
8080
*/
8181
private $lineWidth = 12700;
8282

83+
/** @var bool */
84+
private $scatterLines = true;
85+
8386
/**
8487
* Create a new DataSeriesValues object.
8588
*
@@ -90,8 +93,9 @@ class DataSeriesValues
9093
* @param mixed $dataValues
9194
* @param null|mixed $marker
9295
* @param null|string|string[] $fillColor
96+
* @param string $pointSize
9397
*/
94-
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null)
98+
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = '', $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null, $pointSize = '3')
9599
{
96100
$this->setDataType($dataType);
97101
$this->dataSource = $dataSource;
@@ -100,6 +104,9 @@ public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSourc
100104
$this->dataValues = $dataValues;
101105
$this->pointMarker = $marker;
102106
$this->fillColor = $fillColor;
107+
if (is_numeric($pointSize)) {
108+
$this->pointSize = (int) $pointSize;
109+
}
103110
}
104111

105112
/**
@@ -126,7 +133,7 @@ public function getDataType()
126133
*/
127134
public function setDataType($dataType)
128135
{
129-
if (!in_array($dataType, self::$dataTypeValues)) {
136+
if (!in_array($dataType, self::DATA_TYPE_VALUES)) {
130137
throw new Exception('Invalid datatype for chart data series values');
131138
}
132139
$this->dataType = $dataType;
@@ -239,7 +246,7 @@ public function getPointCount()
239246
/**
240247
* Get fill color.
241248
*
242-
* @return string|string[] HEX color or array with HEX colors
249+
* @return null|string|string[] HEX color or array with HEX colors
243250
*/
244251
public function getFillColor()
245252
{
@@ -249,7 +256,7 @@ public function getFillColor()
249256
/**
250257
* Set fill color for series.
251258
*
252-
* @param string|string[] $color HEX color or array with HEX colors
259+
* @param null|string|string[] $color HEX color or array with HEX colors
253260
*
254261
* @return DataSeriesValues
255262
*/
@@ -260,7 +267,7 @@ public function setFillColor($color)
260267
$this->validateColor($colorValue);
261268
}
262269
} else {
263-
$this->validateColor($color);
270+
$this->validateColor("$color");
264271
}
265272
$this->fillColor = $color;
266273

@@ -379,7 +386,7 @@ public function setDataValues($dataValues)
379386
return $this;
380387
}
381388

382-
public function refresh(Worksheet $worksheet, $flatten = true): void
389+
public function refresh(Worksheet $worksheet, bool $flatten = true): void
383390
{
384391
if ($this->dataSource !== null) {
385392
$calcEngine = Calculation::getInstance($worksheet->getParent());
@@ -421,4 +428,16 @@ public function refresh(Worksheet $worksheet, $flatten = true): void
421428
$this->pointCount = count($this->dataValues);
422429
}
423430
}
431+
432+
public function getScatterLines(): bool
433+
{
434+
return $this->scatterLines;
435+
}
436+
437+
public function setScatterLines(bool $scatterLines): self
438+
{
439+
$this->scatterLines = $scatterLines;
440+
441+
return $this;
442+
}
424443
}

0 commit comments

Comments
 (0)