Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Chart/Charts32XmlTest.php
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

181 lines
6.5 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PHPUnit\Framework\TestCase;
class Charts32XmlTest extends TestCase
{
// These tests can only be performed by examining xml.
private const DIRECTORY = 'samples' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
/**
* @dataProvider providerScatterCharts
*/
public function testBezierCount(int $expectedCount, string $inputFile): void
{
$file = self::DIRECTORY . $inputFile;
$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: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
{
return [
'no line' => [0, '32readwriteScatterChart1.xlsx'],
'smooth line (Bezier)' => [3, '32readwriteScatterChart2.xlsx'],
'straight line' => [0, '32readwriteScatterChart3.xlsx'],
];
}
public function testAreaPercentageNoCat(): void
{
$file = self::DIRECTORY . '32readwriteAreaPercentageChart1.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();
// confirm that file contains expected tags
self::assertSame(0, substr_count($data, '<c:cat>'));
}
/**
* @dataProvider providerCatAxValAx
*/
public function testCatAxValAx(?bool $numeric): void
{
$file = self::DIRECTORY . '32readwriteScatterChart1.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);
$xAxis = $chart->getChartAxisX();
$yAxis = $chart->getChartAxisY();
self::assertSame(Properties::FORMAT_CODE_GENERAL, $xAxis->getAxisNumberFormat());
if (is_bool($numeric)) {
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_GENERAL, true);
}
self::assertSame('valAx', $yAxis->getAxisType());
self::assertSame('valAx', $xAxis->getAxisType());
self::assertSame(Properties::FORMAT_CODE_GENERAL, $yAxis->getAxisNumberFormat());
$xAxis->setAxisType('');
$yAxis->setAxisType('');
if (is_bool($numeric)) {
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_GENERAL, $numeric);
$yAxis->setAxisNumberProperties(Properties::FORMAT_CODE_GENERAL, $numeric);
}
$writer = new XlsxWriter($spreadsheet);
$writer->setIncludeCharts(true);
$writerChart = new XlsxWriter\Chart($writer);
$data = $writerChart->writeChart($chart);
$spreadsheet->disconnectWorksheets();
if ($numeric === true) {
self::assertSame(0, substr_count($data, '<c:catAx>'));
self::assertSame(2, substr_count($data, '<c:valAx>'));
} else {
self::assertSame(1, substr_count($data, '<c:catAx>'));
self::assertSame(1, substr_count($data, '<c:valAx>'));
}
}
public function providerCatAxValAx(): array
{
return [
[true],
[false],
[null],
];
}
public function testCatAxValAxFromRead(): void
{
$file = self::DIRECTORY . '32readwriteScatterChart1.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);
$xAxis = $chart->getChartAxisX();
$yAxis = $chart->getChartAxisY();
self::assertSame(Properties::FORMAT_CODE_GENERAL, $xAxis->getAxisNumberFormat());
self::assertSame('valAx', $yAxis->getAxisType());
self::assertSame('valAx', $xAxis->getAxisType());
self::assertSame(Properties::FORMAT_CODE_GENERAL, $yAxis->getAxisNumberFormat());
$writer = new XlsxWriter($spreadsheet);
$writer->setIncludeCharts(true);
$writerChart = new XlsxWriter\Chart($writer);
$data = $writerChart->writeChart($chart);
$spreadsheet->disconnectWorksheets();
self::assertSame(0, substr_count($data, '<c:catAx>'));
self::assertSame(2, substr_count($data, '<c:valAx>'));
}
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"/>'
)
);
}
}