Skip to content

Commit

Permalink
Additional Support for Chart DataSeriesValues
Browse files Browse the repository at this point in the history
Fix PHPOffice#2863. DataSeriesValues now extends Properties, allowing it to share code in common with Axis and Gridlines. This causes some minor breakages; in particular line width is now initialized to null instead of Excel's default value, and is specified in points, as the user would expect from Excel, rather than the value stored in Xml.

This change:
- adds support for 1 or 2 marker colors.
- adds support for `smoothLine` to DataSeriesValues.
- will determine `catAx` or `valAx` for Axis based on what is read from the Xml when available, rather than guessing based on format. (Another minor break.)
- reads `formatCode` and `sourceLinked` for Axis.
- correct 2 uses of `$plotSeriesRef` to `$plotSeriesIndex` in Writer/Xlsx/Chart.
- pushes coverage over 90% for Chart (88.70% overall).
  • Loading branch information
oleibman committed Jun 22, 2022
1 parent 177a362 commit 415bbc1
Show file tree
Hide file tree
Showing 17 changed files with 571 additions and 160 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4087,7 +4087,7 @@ parameters:

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

-
Expand Down
4 changes: 3 additions & 1 deletion samples/Chart/33_Chart_create_line.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
Expand Down Expand Up @@ -35,6 +36,7 @@
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
];
$dataSeriesLabels[0]->setFillColor('FF0000');
// Set the X-Axis Labels
// Datatype
// Cell reference for data
Expand All @@ -57,7 +59,7 @@
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
];
$dataSeriesValues[2]->setLineWidth(60000);
$dataSeriesValues[2]->setLineWidth(60000 / Properties::POINTS_WIDTH_MULTIPLIER);

// Build the dataseries
$series = new DataSeries(
Expand Down
Binary file added samples/templates/32readwriteScatterChart8.xlsx
Binary file not shown.
23 changes: 21 additions & 2 deletions src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct()
'numeric' => null,
];

/** @var string */
private $axisType = '';

/**
* Axis Options.
*
Expand Down Expand Up @@ -62,11 +65,11 @@ public function __construct()
*
* @param mixed $format_code
*/
public function setAxisNumberProperties($format_code, ?bool $numeric = null): void
public function setAxisNumberProperties($format_code, ?bool $numeric = null, int $sourceLinked = 0): void
{
$format = (string) $format_code;
$this->axisNumber['format'] = $format;
$this->axisNumber['source_linked'] = 0;
$this->axisNumber['source_linked'] = $sourceLinked;
if (is_bool($numeric)) {
$this->axisNumber['numeric'] = $numeric;
} elseif (in_array($format, self::NUMERIC_FORMAT, true)) {
Expand Down Expand Up @@ -156,6 +159,22 @@ public function setAxisOrientation($orientation): void
$this->axisOptions['orientation'] = (string) $orientation;
}

public function getAxisType(): string
{
return $this->axisType;
}

public function setAxisType(string $type): self
{
if ($type === 'catAx' || $type === 'valAx') {
$this->axisType = $type;
} else {
$this->axisType = '';
}

return $this;
}

/**
* Set Fill Property.
*
Expand Down
4 changes: 0 additions & 4 deletions src/PhpSpreadsheet/Chart/DataSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ public function getPlotLabelByIndex($index)
$keys = array_keys($this->plotLabel);
if (in_array($index, $keys)) {
return $this->plotLabel[$index];
} elseif (isset($keys[$index])) {
return $this->plotLabel[$keys[$index]];
}

return false;
Expand Down Expand Up @@ -339,8 +337,6 @@ public function getPlotValuesByIndex($index)
$keys = array_keys($this->plotValues);
if (in_array($index, $keys)) {
return $this->plotValues[$index];
} elseif (isset($keys[$index])) {
return $this->plotValues[$keys[$index]];
}

return false;
Expand Down
68 changes: 55 additions & 13 deletions src/PhpSpreadsheet/Chart/DataSeriesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class DataSeriesValues
class DataSeriesValues extends Properties
{
const DATASERIES_TYPE_STRING = 'String';
const DATASERIES_TYPE_NUMBER = 'Number';
Expand Down Expand Up @@ -45,6 +45,12 @@ class DataSeriesValues
*/
private $pointMarker;

/** @var ChartColor */
private $markerColor1;

/** @var ChartColor */
private $markerColor2;

/**
* Series Point Size.
*
Expand Down Expand Up @@ -79,13 +85,6 @@ class DataSeriesValues
/** @var string */
private $prstClr = '';

/**
* Line Width.
*
* @var int
*/
private $lineWidth = 12700;

/** @var bool */
private $scatterLines = true;

Expand All @@ -106,6 +105,9 @@ class DataSeriesValues
*/
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null, $pointSize = '3')
{
parent::__construct();
$this->markerColor1 = new ChartColor();
$this->markerColor2 = new ChartColor();
$this->setDataType($dataType);
$this->dataSource = $dataSource;
$this->formatCode = $formatCode;
Expand Down Expand Up @@ -198,6 +200,16 @@ public function setPointMarker($marker)
return $this;
}

public function getMarkerColor1(): ChartColor
{
return $this->markerColor1;
}

public function getMarkerColor2(): ChartColor
{
return $this->markerColor2;
}

/**
* Get Point Size.
*/
Expand Down Expand Up @@ -306,24 +318,23 @@ private function validateColor($color)
/**
* Get line width for series.
*
* @return int
* @return null|float|int
*/
public function getLineWidth()
{
return $this->lineWidth;
return $this->lineStyleProperties['width'];
}

/**
* Set line width for the series.
*
* @param int $width
* @param null|float|int $width
*
* @return $this
*/
public function setLineWidth($width)
{
$minWidth = 12700;
$this->lineWidth = max($minWidth, $width);
$this->lineStyleProperties['width'] = $width;

return $this;
}
Expand Down Expand Up @@ -489,4 +500,35 @@ public function setPrstClr(string $prstClr): self

return $this;
}

/**
* Smooth Line.
*
* @var bool
*/
private $smoothLine;

/**
* Get Smooth Line.
*
* @return bool
*/
public function getSmoothLine()
{
return $this->smoothLine;
}

/**
* Set Smooth Line.
*
* @param bool $smoothLine
*
* @return $this
*/
public function setSmoothLine($smoothLine)
{
$this->smoothLine = $smoothLine;

return $this;
}
}
30 changes: 6 additions & 24 deletions src/PhpSpreadsheet/Chart/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,30 +643,6 @@ protected function setShadowPropertiesMapValues(array $propertiesMap, &$referenc
return $this;
}

/**
* Set Shadow Color.
*
* @param string $color
* @param int $alpha
* @param string $colorType
*
* @return $this
*/
protected function setShadowColor($color, $alpha, $colorType)
{
if ($color !== null) {
$this->shadowProperties['color']['value'] = (string) $color;
}
if ($alpha !== null) {
$this->shadowProperties['color']['alpha'] = (int) $alpha;
}
if ($colorType !== null) {
$this->shadowProperties['color']['type'] = (string) $colorType;
}

return $this;
}

/**
* Set Shadow Blur.
*
Expand Down Expand Up @@ -766,6 +742,12 @@ public function getShadowProperty($elements)
],
];

public function copyLineStyles(self $otherProperties): void
{
$this->lineStyleProperties = $otherProperties->lineStyleProperties;
$this->lineColor = $otherProperties->lineColor;
}

public function getLineColor(): ChartColor
{
return $this->lineColor;
Expand Down
Loading

0 comments on commit 415bbc1

Please sign in to comment.