From e6c95bf9b0400b2af07372dde2914c01b7880542 Mon Sep 17 00:00:00 2001 From: CrazyBite Date: Wed, 3 May 2017 15:53:07 +0300 Subject: [PATCH] Added fillColor for chart plot series For now it is only to write to Xlsx Closes #158 --- CHANGELOG.md | 2 + src/PhpSpreadsheet/Chart/DataSeriesValues.php | 39 ++++++++++++++++++- src/PhpSpreadsheet/Writer/Xlsx/Chart.php | 14 +++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94204fa6..28c6adcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- Support for chart fill color - @CrazyBite [#158](https://github.com/PHPOffice/PhpSpreadsheet/pull/158) + ### Changed - Merge data-validations to reduce written worksheet size - @billblume [#131](https://github.com/PHPOffice/PhpSpreadSheet/issues/131) diff --git a/src/PhpSpreadsheet/Chart/DataSeriesValues.php b/src/PhpSpreadsheet/Chart/DataSeriesValues.php index 4fee7c28..76c86793 100644 --- a/src/PhpSpreadsheet/Chart/DataSeriesValues.php +++ b/src/PhpSpreadsheet/Chart/DataSeriesValues.php @@ -81,17 +81,26 @@ class DataSeriesValues */ private $dataValues = []; + /** + * Fill color. + * + * @var string + */ + private $fillColor; + /** * Create a new DataSeriesValues object. * + * * @param mixed $dataType * @param string $dataSource * @param null|mixed $formatCode * @param mixed $pointCount * @param mixed $dataValues * @param null|mixed $marker + * @param null|string $fillColor */ - public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null) + public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null) { $this->setDataType($dataType); $this->dataSource = $dataSource; @@ -99,6 +108,7 @@ class DataSeriesValues $this->pointCount = $pointCount; $this->dataValues = $dataValues; $this->pointMarker = $marker; + $this->fillColor = $fillColor; } /** @@ -217,6 +227,33 @@ class DataSeriesValues return $this->pointCount; } + /** + * Get fill color. + * + * @return string HEX color + */ + public function getFillColor() + { + return $this->fillColor; + } + + /** + * Set fill color for series. + * + * @param string $color HEX color + * + * @return DataSeriesValues + */ + public function setFillColor($color) + { + if (!preg_match('/^[a-f0-9]{6}$/i', $color)) { + throw new Exception('Invalid hex color for chart series'); + } + $this->fillColor = $color; + + return $this; + } + /** * Identify if the Data Series is a multi-level or a simple series. * diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php index a86b1a88..6f7f58d5 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php @@ -1098,6 +1098,20 @@ class Chart extends WriterPart foreach ($plotSeriesOrder as $plotSeriesIdx => $plotSeriesRef) { $objWriter->startElement('c:ser'); + $plotLabel = $plotGroup->getPlotLabelByIndex($plotSeriesIdx); + if ($plotLabel) { + $fillColor = $plotLabel->getFillColor(); + if ($fillColor !== null) { + $objWriter->startElement('c:spPr'); + $objWriter->startElement('a:solidFill'); + $objWriter->startElement('a:srgbClr'); + $objWriter->writeAttribute('val', $fillColor); + $objWriter->endElement(); + $objWriter->endElement(); + $objWriter->endElement(); + } + } + $objWriter->startElement('c:idx'); $objWriter->writeAttribute('val', $this->_seriesIndex + $plotSeriesIdx); $objWriter->endElement();