Expand Chart Support for schemeClr and prstClr (#2879)
* Expand Chart Support for schemeClr and prstClr Fix #2219. Address some, but not all, issues mentioned in #2863. For Pie Charts, fill color is stored in XML as part of dPt node, which had been ignored by Reader/Xlsx/Chart. Add support for it, including when specified as schemeClr or prstClr rather than srgbClr. Add support for prstClr in other cases where schemeClr is supported. * Update Change Log Add this PR.
This commit is contained in:
parent
90bdc7c12e
commit
04f4667658
|
|
@ -48,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
- Time interval formatting [Issue #2768](https://github.com/PHPOffice/PhpSpreadsheet/issues/2768) [PR #2772](https://github.com/PHPOffice/PhpSpreadsheet/pull/2772)
|
||||
- Copy from Xls(x) to Html/Pdf loses drawings [PR #2788](https://github.com/PHPOffice/PhpSpreadsheet/pull/2788)
|
||||
- Html Reader converting cell containing 0 to null string [Issue #2810](https://github.com/PHPOffice/PhpSpreadsheet/issues/2810) [PR #2813](https://github.com/PHPOffice/PhpSpreadsheet/pull/2813)
|
||||
- Many fixes for Charts, especially, but not limited to, Scatter, Bubble, and Surface charts. [Issue #2762](https://github.com/PHPOffice/PhpSpreadsheet/issues/2762) [Issue #2299](https://github.com/PHPOffice/PhpSpreadsheet/issues/2299) [Issue #2700](https://github.com/PHPOffice/PhpSpreadsheet/issues/2700) [Issue #2817](https://github.com/PHPOffice/PhpSpreadsheet/issues/2817) [Issue #2763](https://github.com/PHPOffice/PhpSpreadsheet/issues/2763) [PR #2828](https://github.com/PHPOffice/PhpSpreadsheet/pull/2828) [PR #2841](https://github.com/PHPOffice/PhpSpreadsheet/pull/2841) [PR #2846](https://github.com/PHPOffice/PhpSpreadsheet/pull/2846) [PR #2852](https://github.com/PHPOffice/PhpSpreadsheet/pull/2852)
|
||||
- Many fixes for Charts, especially, but not limited to, Scatter, Bubble, and Surface charts. [Issue #2762](https://github.com/PHPOffice/PhpSpreadsheet/issues/2762) [Issue #2299](https://github.com/PHPOffice/PhpSpreadsheet/issues/2299) [Issue #2700](https://github.com/PHPOffice/PhpSpreadsheet/issues/2700) [Issue #2817](https://github.com/PHPOffice/PhpSpreadsheet/issues/2817) [Issue #2763](https://github.com/PHPOffice/PhpSpreadsheet/issues/2763) [Issue #2219](https://github.com/PHPOffice/PhpSpreadsheet/issues/2219) [PR #2828](https://github.com/PHPOffice/PhpSpreadsheet/pull/2828) [PR #2841](https://github.com/PHPOffice/PhpSpreadsheet/pull/2841) [PR #2846](https://github.com/PHPOffice/PhpSpreadsheet/pull/2846) [PR #2852](https://github.com/PHPOffice/PhpSpreadsheet/pull/2852) [PR #2856](https://github.com/PHPOffice/PhpSpreadsheet/pull/2856) [PR #2865](https://github.com/PHPOffice/PhpSpreadsheet/pull/2865) [PR #2872](https://github.com/PHPOffice/PhpSpreadsheet/pull/2872) [PR #2879](https://github.com/PHPOffice/PhpSpreadsheet/pull/2879)
|
||||
|
||||
## 1.23.0 - 2022-04-24
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -76,6 +76,9 @@ class DataSeriesValues
|
|||
/** @var string */
|
||||
private $schemeClr = '';
|
||||
|
||||
/** @var string */
|
||||
private $prstClr = '';
|
||||
|
||||
/**
|
||||
* Line Width.
|
||||
*
|
||||
|
|
@ -262,7 +265,7 @@ class DataSeriesValues
|
|||
/**
|
||||
* Set fill color for series.
|
||||
*
|
||||
* @param null|string|string[] $color HEX color or array with HEX colors
|
||||
* @param string|string[] $color HEX color or array with HEX colors
|
||||
*
|
||||
* @return DataSeriesValues
|
||||
*/
|
||||
|
|
@ -270,11 +273,15 @@ class DataSeriesValues
|
|||
{
|
||||
if (is_array($color)) {
|
||||
foreach ($color as $colorValue) {
|
||||
if (substr($colorValue, 0, 1) !== '*' && substr($colorValue, 0, 1) !== '/') {
|
||||
$this->validateColor($colorValue);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (substr($color, 0, 1) !== '*' && substr($color, 0, 1) !== '/') {
|
||||
$this->validateColor("$color");
|
||||
}
|
||||
}
|
||||
$this->fillColor = $color;
|
||||
|
||||
return $this;
|
||||
|
|
@ -470,4 +477,16 @@ class DataSeriesValues
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPrstClr(): string
|
||||
{
|
||||
return $this->prstClr;
|
||||
}
|
||||
|
||||
public function setPrstClr(string $prstClr): self
|
||||
{
|
||||
$this->prstClr = $prstClr;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,11 @@ abstract class Properties
|
|||
EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
|
||||
const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
|
||||
const EXCEL_COLOR_TYPE_ARGB = 'srgbClr';
|
||||
const EXCEL_COLOR_TYPES = [
|
||||
self::EXCEL_COLOR_TYPE_ARGB,
|
||||
self::EXCEL_COLOR_TYPE_SCHEME,
|
||||
self::EXCEL_COLOR_TYPE_STANDARD,
|
||||
];
|
||||
|
||||
const
|
||||
AXIS_LABELS_LOW = 'low';
|
||||
|
|
|
|||
|
|
@ -359,7 +359,9 @@ class Chart
|
|||
$pointSize = null;
|
||||
$noFill = false;
|
||||
$schemeClr = '';
|
||||
$prstClr = '';
|
||||
$bubble3D = false;
|
||||
$dPtColors = [];
|
||||
foreach ($seriesDetails as $seriesKey => $seriesDetail) {
|
||||
switch ($seriesKey) {
|
||||
case 'idx':
|
||||
|
|
@ -383,7 +385,24 @@ class Chart
|
|||
$noFill = true;
|
||||
}
|
||||
if (isset($children->solidFill)) {
|
||||
$this->readColor($children->solidFill, $srgbClr, $schemeClr);
|
||||
$this->readColor($children->solidFill, $srgbClr, $schemeClr, $prstClr);
|
||||
}
|
||||
|
||||
break;
|
||||
case 'dPt':
|
||||
$dptIdx = (int) self::getAttribute($seriesDetail->idx, 'val', 'string');
|
||||
if (isset($seriesDetail->spPr)) {
|
||||
$children = $seriesDetail->spPr->children($this->aNamespace);
|
||||
if (isset($children->solidFill)) {
|
||||
$arrayColors = $this->readColor($children->solidFill);
|
||||
if ($arrayColors['type'] === 'srgbClr') {
|
||||
$dptColors[$dptIdx] = $arrayColors['value'];
|
||||
} elseif ($arrayColors['type'] === 'prstClr') {
|
||||
$dptColors[$dptIdx] = '/' . $arrayColors['value'];
|
||||
} else {
|
||||
$dptColors[$dptIdx] = '*' . $arrayColors['value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
|
@ -394,7 +413,7 @@ class Chart
|
|||
if (count($seriesDetail->spPr) === 1) {
|
||||
$ln = $seriesDetail->spPr->children($this->aNamespace);
|
||||
if (isset($ln->solidFill)) {
|
||||
$this->readColor($ln->solidFill, $srgbClr, $schemeClr);
|
||||
$this->readColor($ln->solidFill, $srgbClr, $schemeClr, $prstClr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -461,6 +480,16 @@ class Chart
|
|||
if (isset($seriesValues[$seriesIndex])) {
|
||||
$seriesValues[$seriesIndex]->setSchemeClr($schemeClr);
|
||||
}
|
||||
} elseif ($prstClr) {
|
||||
if (isset($seriesLabel[$seriesIndex])) {
|
||||
$seriesLabel[$seriesIndex]->setPrstClr($prstClr);
|
||||
}
|
||||
if (isset($seriesCategory[$seriesIndex])) {
|
||||
$seriesCategory[$seriesIndex]->setPrstClr($prstClr);
|
||||
}
|
||||
if (isset($seriesValues[$seriesIndex])) {
|
||||
$seriesValues[$seriesIndex]->setPrstClr($prstClr);
|
||||
}
|
||||
}
|
||||
if ($bubble3D) {
|
||||
if (isset($seriesLabel[$seriesIndex])) {
|
||||
|
|
@ -473,6 +502,17 @@ class Chart
|
|||
$seriesValues[$seriesIndex]->setBubble3D($bubble3D);
|
||||
}
|
||||
}
|
||||
if (!empty($dptColors)) {
|
||||
if (isset($seriesLabel[$seriesIndex])) {
|
||||
$seriesLabel[$seriesIndex]->setFillColor($dptColors);
|
||||
}
|
||||
if (isset($seriesCategory[$seriesIndex])) {
|
||||
$seriesCategory[$seriesIndex]->setFillColor($dptColors);
|
||||
}
|
||||
if (isset($seriesValues[$seriesIndex])) {
|
||||
$seriesValues[$seriesIndex]->setFillColor($dptColors);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/** @phpstan-ignore-next-line */
|
||||
|
|
@ -1001,39 +1041,31 @@ class Chart
|
|||
'innerShdw',
|
||||
];
|
||||
|
||||
private function readColor(SimpleXMLElement $colorXml, ?string &$srgbClr = null, ?string &$schemeClr = null): array
|
||||
private function readColor(SimpleXMLElement $colorXml, ?string &$srgbClr = null, ?string &$schemeClr = null, ?string &$prstClr = null): array
|
||||
{
|
||||
$result = [
|
||||
'type' => null,
|
||||
'value' => null,
|
||||
'alpha' => null,
|
||||
];
|
||||
if (isset($colorXml->srgbClr)) {
|
||||
$result['type'] = Properties::EXCEL_COLOR_TYPE_ARGB;
|
||||
$result['value'] = $srgbClr = self::getAttribute($colorXml->srgbClr, 'val', 'string');
|
||||
if (isset($colorXml->srgbClr->alpha)) {
|
||||
/** @var string */
|
||||
$alpha = self::getAttribute($colorXml->srgbClr->alpha, 'val', 'string');
|
||||
$alpha = Properties::alphaFromXml($alpha);
|
||||
foreach (Properties::EXCEL_COLOR_TYPES as $type) {
|
||||
if (isset($colorXml->$type)) {
|
||||
$result['type'] = $type;
|
||||
$result['value'] = self::getAttribute($colorXml->$type, 'val', 'string');
|
||||
if ($type === Properties::EXCEL_COLOR_TYPE_ARGB) {
|
||||
$srgbClr = $result['value'];
|
||||
} elseif ($type === Properties::EXCEL_COLOR_TYPE_SCHEME) {
|
||||
$schemeClr = $result['value'];
|
||||
} elseif ($type === Properties::EXCEL_COLOR_TYPE_STANDARD) {
|
||||
$prstClr = $result['value'];
|
||||
}
|
||||
if (isset($colorXml->$type->alpha)) {
|
||||
$alpha = (int) self::getAttribute($colorXml->$type->alpha, 'val', 'string');
|
||||
$alpha = 100 - (int) ($alpha / 1000);
|
||||
$result['alpha'] = $alpha;
|
||||
}
|
||||
} elseif (isset($colorXml->schemeClr)) {
|
||||
$result['type'] = Properties::EXCEL_COLOR_TYPE_SCHEME;
|
||||
$result['value'] = $schemeClr = self::getAttribute($colorXml->schemeClr, 'val', 'string');
|
||||
if (isset($colorXml->schemeClr->alpha)) {
|
||||
/** @var string */
|
||||
$alpha = self::getAttribute($colorXml->schemeClr->alpha, 'val', 'string');
|
||||
$alpha = Properties::alphaFromXml($alpha);
|
||||
$result['alpha'] = $alpha;
|
||||
}
|
||||
} elseif (isset($colorXml->prstClr)) {
|
||||
$result['type'] = Properties::EXCEL_COLOR_TYPE_STANDARD;
|
||||
$result['value'] = self::getAttribute($colorXml->prstClr, 'val', 'string');
|
||||
if (isset($colorXml->prstClr->alpha)) {
|
||||
/** @var string */
|
||||
$alpha = self::getAttribute($colorXml->prstClr->alpha, 'val', 'string');
|
||||
$alpha = Properties::alphaFromXml($alpha);
|
||||
$result['alpha'] = $alpha;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -942,6 +942,9 @@ class Chart extends WriterPart
|
|||
*/
|
||||
private function writePlotSeriesValuesElement(XMLWriter $objWriter, $val = 3, $fillColor = 'FF9900'): void
|
||||
{
|
||||
if ($fillColor === '') {
|
||||
return;
|
||||
}
|
||||
$objWriter->startElement('c:dPt');
|
||||
$objWriter->startElement('c:idx');
|
||||
$objWriter->writeAttribute('val', $val);
|
||||
|
|
@ -953,8 +956,16 @@ class Chart extends WriterPart
|
|||
|
||||
$objWriter->startElement('c:spPr');
|
||||
$objWriter->startElement('a:solidFill');
|
||||
if (substr($fillColor, 0, 1) === '*') {
|
||||
$objWriter->startElement('a:schemeClr');
|
||||
$objWriter->writeAttribute('val', substr($fillColor, 1));
|
||||
} elseif (substr($fillColor, 0, 1) === '/') {
|
||||
$objWriter->startElement('a:prstClr');
|
||||
$objWriter->writeAttribute('val', substr($fillColor, 1));
|
||||
} else {
|
||||
$objWriter->startElement('a:srgbClr');
|
||||
$objWriter->writeAttribute('val', $fillColor);
|
||||
}
|
||||
$objWriter->endElement();
|
||||
$objWriter->endElement();
|
||||
$objWriter->endElement();
|
||||
|
|
@ -1039,7 +1050,7 @@ class Chart extends WriterPart
|
|||
$fillColorValues = $plotSeriesValues->getFillColor();
|
||||
if ($fillColorValues !== null && is_array($fillColorValues)) {
|
||||
foreach ($plotSeriesValues->getDataValues() as $dataKey => $dataValue) {
|
||||
$this->writePlotSeriesValuesElement($objWriter, $dataKey, ($fillColorValues[$dataKey] ?? 'FF9900'));
|
||||
$this->writePlotSeriesValuesElement($objWriter, $dataKey, $fillColorValues[$dataKey] ?? '');
|
||||
}
|
||||
} else {
|
||||
$this->writePlotSeriesValuesElement($objWriter);
|
||||
|
|
@ -1061,7 +1072,7 @@ class Chart extends WriterPart
|
|||
$groupType == DataSeries::TYPE_LINECHART
|
||||
|| $groupType == DataSeries::TYPE_STOCKCHART
|
||||
|| ($groupType === DataSeries::TYPE_SCATTERCHART && $plotSeriesValues !== false && !$plotSeriesValues->getScatterLines())
|
||||
|| ($plotSeriesValues !== false && $plotSeriesValues->getSchemeClr())
|
||||
|| ($plotSeriesValues !== false && ($plotSeriesValues->getSchemeClr() || $plotSeriesValues->getPrstClr()))
|
||||
) {
|
||||
$plotLineWidth = 12700;
|
||||
if ($plotSeriesValues) {
|
||||
|
|
@ -1069,10 +1080,21 @@ class Chart extends WriterPart
|
|||
}
|
||||
|
||||
$objWriter->startElement('c:spPr');
|
||||
$schemeClr = $plotLabel ? $plotLabel->getSchemeClr() : null;
|
||||
$schemeClr = $typeClr = '';
|
||||
if ($plotLabel) {
|
||||
$schemeClr = $plotLabel->getSchemeClr();
|
||||
if ($schemeClr) {
|
||||
$typeClr = 'schemeClr';
|
||||
} else {
|
||||
$schemeClr = $plotLabel->getPrstClr();
|
||||
if ($schemeClr) {
|
||||
$typeClr = 'prstClr';
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($schemeClr) {
|
||||
$objWriter->startElement('a:solidFill');
|
||||
$objWriter->startElement('a:schemeClr');
|
||||
$objWriter->startElement("a:$typeClr");
|
||||
$objWriter->writeAttribute('val', $schemeClr);
|
||||
$objWriter->endElement();
|
||||
$objWriter->endElement();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ namespace PhpOffice\PhpSpreadsheetTests\Chart;
|
|||
|
||||
use PhpOffice\PhpSpreadsheet\Chart\Properties;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\File;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
|
@ -13,17 +12,6 @@ class Charts32XmlTest extends TestCase
|
|||
// These tests can only be performed by examining xml.
|
||||
private const DIRECTORY = 'samples' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
|
||||
|
||||
/** @var string */
|
||||
private $outputFileName = '';
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->outputFileName !== '') {
|
||||
unlink($this->outputFileName);
|
||||
$this->outputFileName = '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerScatterCharts
|
||||
*/
|
||||
|
|
@ -33,26 +21,22 @@ class Charts32XmlTest extends TestCase
|
|||
$reader = new XlsxReader();
|
||||
$reader->setIncludeCharts(true);
|
||||
$spreadsheet = $reader->load($file);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$charts = $sheet->getChartCollection();
|
||||
self::assertCount(1, $charts);
|
||||
$chart = $charts[0];
|
||||
self::assertNotNull($chart);
|
||||
|
||||
$writer = new XlsxWriter($spreadsheet);
|
||||
$writer->setIncludeCharts(true);
|
||||
$this->outputFileName = File::temporaryFilename();
|
||||
$writer->save($this->outputFileName);
|
||||
$writerChart = new XlsxWriter\Chart($writer);
|
||||
$data = $writerChart->writeChart($chart);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
$file = 'zip://';
|
||||
$file .= $this->outputFileName;
|
||||
$file .= '#xl/charts/chart2.xml';
|
||||
$data = file_get_contents($file);
|
||||
// confirm that file contains expected tags
|
||||
if ($data === false) {
|
||||
self::fail('Unable to read file');
|
||||
} else {
|
||||
self::assertSame(1, substr_count($data, '<c:scatterStyle val='));
|
||||
self::assertSame($expectedCount ? 1 : 0, substr_count($data, '<c:scatterStyle val="smoothMarker"/>'));
|
||||
self::assertSame($expectedCount, substr_count($data, '<c:smooth val="1"/>'));
|
||||
}
|
||||
}
|
||||
|
||||
public function providerScatterCharts(): array
|
||||
{
|
||||
|
|
@ -69,24 +53,21 @@ class Charts32XmlTest extends TestCase
|
|||
$reader = new XlsxReader();
|
||||
$reader->setIncludeCharts(true);
|
||||
$spreadsheet = $reader->load($file);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$charts = $sheet->getChartCollection();
|
||||
self::assertCount(1, $charts);
|
||||
$chart = $charts[0];
|
||||
self::assertNotNull($chart);
|
||||
|
||||
$writer = new XlsxWriter($spreadsheet);
|
||||
$writer->setIncludeCharts(true);
|
||||
$this->outputFileName = File::temporaryFilename();
|
||||
$writer->save($this->outputFileName);
|
||||
$writerChart = new XlsxWriter\Chart($writer);
|
||||
$data = $writerChart->writeChart($chart);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
$file = 'zip://';
|
||||
$file .= $this->outputFileName;
|
||||
$file .= '#xl/charts/chart1.xml';
|
||||
$data = file_get_contents($file);
|
||||
// confirm that file contains expected tags
|
||||
if ($data === false) {
|
||||
self::fail('Unable to read file');
|
||||
} else {
|
||||
self::assertSame(0, substr_count($data, '<c:cat>'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerCatAxValAx
|
||||
|
|
@ -116,18 +97,11 @@ class Charts32XmlTest extends TestCase
|
|||
|
||||
$writer = new XlsxWriter($spreadsheet);
|
||||
$writer->setIncludeCharts(true);
|
||||
$this->outputFileName = File::temporaryFilename();
|
||||
$writer->save($this->outputFileName);
|
||||
$writerChart = new XlsxWriter\Chart($writer);
|
||||
$data = $writerChart->writeChart($chart);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
$file = 'zip://';
|
||||
$file .= $this->outputFileName;
|
||||
$file .= '#xl/charts/chart2.xml';
|
||||
$data = file_get_contents($file);
|
||||
// confirm that file contains expected tags
|
||||
if ($data === false) {
|
||||
self::fail('Unable to read file');
|
||||
} elseif ($numeric === true) {
|
||||
if ($numeric === true) {
|
||||
self::assertSame(0, substr_count($data, '<c:catAx>'));
|
||||
self::assertSame(2, substr_count($data, '<c:valAx>'));
|
||||
} else {
|
||||
|
|
@ -144,4 +118,31 @@ class Charts32XmlTest extends TestCase
|
|||
[null],
|
||||
];
|
||||
}
|
||||
|
||||
public function testAreaPrstClr(): void
|
||||
{
|
||||
$file = self::DIRECTORY . '32readwriteAreaChart4.xlsx';
|
||||
$reader = new XlsxReader();
|
||||
$reader->setIncludeCharts(true);
|
||||
$spreadsheet = $reader->load($file);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$charts = $sheet->getChartCollection();
|
||||
self::assertCount(1, $charts);
|
||||
$chart = $charts[0];
|
||||
self::assertNotNull($chart);
|
||||
|
||||
$writer = new XlsxWriter($spreadsheet);
|
||||
$writer->setIncludeCharts(true);
|
||||
$writerChart = new XlsxWriter\Chart($writer);
|
||||
$data = $writerChart->writeChart($chart);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
self::assertSame(
|
||||
1,
|
||||
substr_count(
|
||||
$data,
|
||||
'</c:tx><c:spPr><a:solidFill><a:prstClr val="red"/>'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,160 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Chart\Chart;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\Layout;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
|
||||
use PhpOffice\PhpSpreadsheet\Chart\Title;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
|
||||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
||||
|
||||
class PieFillTest extends AbstractFunctional
|
||||
{
|
||||
public function readCharts(XlsxReader $reader): void
|
||||
{
|
||||
$reader->setIncludeCharts(true);
|
||||
}
|
||||
|
||||
public function writeCharts(XlsxWriter $writer): void
|
||||
{
|
||||
$writer->setIncludeCharts(true);
|
||||
}
|
||||
|
||||
public function testPieFill(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
$worksheet->fromArray(
|
||||
[
|
||||
['', 2010, 2011, 2012],
|
||||
['Q1', 12, 15, 21],
|
||||
['Q2', 56, 73, 86],
|
||||
['Q3', 52, 61, 69],
|
||||
['Q4', 30, 32, 0],
|
||||
]
|
||||
);
|
||||
// Custom colors for dataSeries (gray, blue, red, orange)
|
||||
$colors = [
|
||||
'cccccc',
|
||||
'*accent1', // use schemeClr, was '00abb8',
|
||||
'/green', // use prstClr, was 'b8292f',
|
||||
'eb8500',
|
||||
];
|
||||
|
||||
// Set the Labels for each data series we want to plot
|
||||
// Datatype
|
||||
// Cell reference for data
|
||||
// Format Code
|
||||
// Number of datapoints in series
|
||||
// Data values
|
||||
// Data Marker
|
||||
$dataSeriesLabels1 = [
|
||||
new DataSeriesValues(
|
||||
DataSeriesValues::DATASERIES_TYPE_STRING,
|
||||
'Worksheet!$C$1',
|
||||
null,
|
||||
1
|
||||
), // 2011
|
||||
];
|
||||
// Set the X-Axis Labels
|
||||
// Datatype
|
||||
// Cell reference for data
|
||||
// Format Code
|
||||
// Number of datapoints in series
|
||||
// Data values
|
||||
// Data Marker
|
||||
$xAxisTickValues1 = [
|
||||
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
|
||||
];
|
||||
// Set the Data values for each data series we want to plot
|
||||
// Datatype
|
||||
// Cell reference for data
|
||||
// Format Code
|
||||
// Number of datapoints in series
|
||||
// Data values
|
||||
// Data Marker
|
||||
// Custom Colors
|
||||
$dataSeriesValues1Element = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4);
|
||||
$dataSeriesValues1Element->setFillColor($colors);
|
||||
$dataSeriesValues1 = [$dataSeriesValues1Element];
|
||||
|
||||
// Build the dataseries
|
||||
$series1 = new DataSeries(
|
||||
DataSeries::TYPE_PIECHART, // plotType
|
||||
null, // plotGrouping (Pie charts don't have any grouping)
|
||||
range(0, count($dataSeriesValues1) - 1), // plotOrder
|
||||
$dataSeriesLabels1, // plotLabel
|
||||
$xAxisTickValues1, // plotCategory
|
||||
$dataSeriesValues1 // plotValues
|
||||
);
|
||||
|
||||
// Set up a layout object for the Pie chart
|
||||
$layout1 = new Layout();
|
||||
$layout1->setShowVal(true);
|
||||
$layout1->setShowPercent(true);
|
||||
|
||||
// Set the series in the plot area
|
||||
$plotArea1 = new PlotArea($layout1, [$series1]);
|
||||
// Set the chart legend
|
||||
$legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
|
||||
|
||||
$title1 = new Title('Test Pie Chart');
|
||||
|
||||
// Create the chart
|
||||
$chart1 = new Chart(
|
||||
'chart1', // name
|
||||
$title1, // title
|
||||
$legend1, // legend
|
||||
$plotArea1, // plotArea
|
||||
true, // plotVisibleOnly
|
||||
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
|
||||
null, // xAxisLabel
|
||||
null // no Y-Axis for Pie Chart
|
||||
);
|
||||
|
||||
// Set the position where the chart should appear in the worksheet
|
||||
$chart1->setTopLeftPosition('A7');
|
||||
$chart1->setBottomRightPosition('H20');
|
||||
|
||||
// Add the chart to the worksheet
|
||||
$worksheet->addChart($chart1);
|
||||
|
||||
/** @var callable */
|
||||
$callableReader = [$this, 'readCharts'];
|
||||
/** @var callable */
|
||||
$callableWriter = [$this, 'writeCharts'];
|
||||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
$sheet = $reloadedSpreadsheet->getActiveSheet();
|
||||
$charts2 = $sheet->getChartCollection();
|
||||
self::assertCount(1, $charts2);
|
||||
$chart2 = $charts2[0];
|
||||
self::assertNotNull($chart2);
|
||||
$plotArea2 = $chart2->getPlotArea();
|
||||
$dataSeries2 = $plotArea2->getPlotGroup();
|
||||
self::assertCount(1, $dataSeries2);
|
||||
$plotValues = $dataSeries2[0]->getPlotValues();
|
||||
self::assertCount(1, $plotValues);
|
||||
$fillColors = $plotValues[0]->getFillColor();
|
||||
self::assertSame($colors, $fillColors);
|
||||
|
||||
$writer = new XlsxWriter($reloadedSpreadsheet);
|
||||
$writer->setIncludeCharts(true);
|
||||
$writerChart = new XlsxWriter\Chart($writer);
|
||||
$data = $writerChart->writeChart($chart2);
|
||||
self::assertSame(1, substr_count($data, '<a:srgbClr val="cccccc"/>'));
|
||||
self::assertSame(1, substr_count($data, '<a:schemeClr val="accent1"/>'));
|
||||
self::assertSame(1, substr_count($data, '<a:prstClr val="green"/>'));
|
||||
self::assertSame(1, substr_count($data, '<a:srgbClr val="eb8500"/>'));
|
||||
self::assertSame(4, substr_count($data, '<c:dPt>'));
|
||||
|
||||
$reloadedSpreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue