Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Chart/Issue589Test.php
T
oleibman 5d5e550342 Additional Support for Chart DataSeriesValues (#2906)
* Additional Support for Chart DataSeriesValues

Fix #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).

* Update Change Log

I had updated previously but forgot to stage the member.

* Adopt Some Suggestions

Incorporate some changes suggested by @bridgeplayr.

* Use ChartColor for DSV Fill And Font Text

DataSeriesValues Fill could be a scalar or an array, so I saved it till last.

* Some Final Cleanup

No code changes.

Illustrate even more of the new features in existing sample files.

Deprecate *_ARGB in Properties/ChartColors in favor of *_RGB, because it uses only 6 hex digits. The alpha value is stored separately.
2022-06-29 17:52:09 -07:00

163 lines
5.6 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use DOMDocument;
use DOMNode;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
use PHPUnit\Framework\TestCase;
use ZipArchive;
class Issue589Test extends TestCase
{
/**
* Build a testable chart in a spreadsheet and set fill color for series.
*
* @param string|string[] $color HEX color or array with HEX colors
*/
private function buildChartSpreadsheet($color): Spreadsheet
{
// Problem occurs when setting plot line color
// The output chart xml file is missing the a:ln tag
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray(
[
[2010],
[12],
[56],
]
);
$dataSeriesLabels = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$1', null, 1),
];
$dataSeriesLabels[0]->setFillColor($color);
$dataSeriesValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$3', null, 2),
];
// Build the dataseries
$series = new DataSeries(
DataSeries::TYPE_LINECHART,
DataSeries::GROUPING_STACKED,
range(0, count($dataSeriesValues) - 1),
$dataSeriesLabels,
[],
$dataSeriesValues
);
// Set the series in the plot area
$plotArea = new PlotArea(null, [$series]);
// Create the chart
$chart = new Chart(
'chart1',
null,
null,
$plotArea
);
// Add the chart to the worksheet
$worksheet->addChart($chart);
return $spreadsheet;
}
public function testLineChartFill(): void
{
$outputFilename = File::temporaryFilename();
$spreadsheet = $this->buildChartSpreadsheet('98B954');
$writer = new Writer($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($outputFilename);
$zip = new ZipArchive();
$zip->open($outputFilename);
$resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
$zip->close();
unlink($outputFilename);
$dom = new DOMDocument();
if ($resultChart1Raw === false) {
self::fail('Unable to open the chart file');
} else {
$loaded = $dom->loadXML($resultChart1Raw);
if (!$loaded) {
self::fail('Unable to load the chart xml');
} else {
$series = $dom->getElementsByTagName('ser');
$firstSeries = $series->item(0);
if ($firstSeries === null) {
self::fail('The chart XML does not contain a \'ser\' tag!');
} else {
$spPrList = $firstSeries->getElementsByTagName('spPr');
// expect to see only one element with name 'c:spPr'
self::assertCount(1, $spPrList);
/** @var DOMNode $node */
$node = $spPrList->item(0); // Get the spPr element
$actualXml = $dom->saveXML($node);
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
}
}
}
}
}
public function testLineChartFillIgnoresColorArray(): void
{
$outputFilename = File::temporaryFilename();
$spreadsheet = $this->buildChartSpreadsheet(['98B954']);
$writer = new Writer($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($outputFilename);
$zip = new ZipArchive();
$zip->open($outputFilename);
$resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
$zip->close();
unlink($outputFilename);
$dom = new DOMDocument();
if ($resultChart1Raw === false) {
self::fail('Unable to open the chart file');
} else {
$loaded = $dom->loadXML($resultChart1Raw);
if (!$loaded) {
self::fail('Unable to load the chart xml');
} else {
$series = $dom->getElementsByTagName('ser');
$firstSeries = $series->item(0);
if ($firstSeries === null) {
self::fail('The chart XML does not contain a \'ser\' tag!');
} else {
$spPrList = $firstSeries->getElementsByTagName('spPr');
// expect to see only one element with name 'c:spPr'
self::assertCount(1, $spPrList);
/** @var DOMNode $node */
$node = $spPrList->item(0); // Get the spPr element
$actualXml = $dom->saveXML($node);
if ($actualXml === false) {
self::fail('Failure saving the spPr element as xml string!');
} else {
self::assertXmlStringEqualsXmlString('<c:spPr><a:ln/></c:spPr>', $actualXml);
}
}
}
}
}
}