Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fillColor for charts (only xlsx) #158

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/PhpSpreadsheet/Chart/DataSeriesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,32 @@ class DataSeriesValues
* Create a new DataSeriesValues object.
*
* @param mixed $dataType
* Fill color.
*
* @var string
*/
private $fillColor;

/**
* Create a new DataSeriesValues object.
*
* @param string $dataSource
* @param null|mixed $formatCode
* @param mixed $pointCount
* @param mixed $dataValues
* @param null|mixed $marker
* @param mixed $dataType
* @param null|mixed $color
*/
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, $color = null)
{
$this->setDataType($dataType);
$this->dataSource = $dataSource;
$this->formatCode = $formatCode;
$this->pointCount = $pointCount;
$this->dataValues = $dataValues;
$this->pointMarker = $marker;
$this->fillColor = $color;
}

/**
Expand Down Expand Up @@ -212,6 +224,35 @@ public function getPointCount()
return $this->pointCount;
}

/**
* Identify if the Data Series is a multi-level or a simple series.
* Get fill color.
*
* @return string
*/
public function getFillColor()
{
return $this->fillColor;
}

/**
* Set fill color for series.
*
* @param string HEX color
* @param mixed $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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,19 @@ private function writePlotGroup($plotGroup, $groupType, $objWriter, &$catIsMulti
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();
Expand Down