mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-27 18:48:36 +00:00
7d0f2ad706
Writer/Xlsx/Chart hard-codes a lot of output values. While nobody has reported a problem involving these, it makes sense to me to copy those values over from what Xlsx/Reader/Chart read, rather than hard-coding them. This involves adding some new properties to Chart and Layout, all, naturally, initialized to the values that we have been hard-coding. - Chart\date1904 - Chart\lang - Chart\pageMargins - Chart\pageSetup - Layout\bodyPr With one exception, I have not investigated any of the new properties in depth. That may come in time. The one I did look at is `date1904`. Chances are that it should match the equivalent spreadsheet setting, but it won't matter if there are no dates on your chart, and it often won't matter even if you do. It may well matter if you are using a `date axis`. While looking into that, it became apparent that some Shared Date conversions need to be a bit more flexible, specifying an optional `calendar` parameter rather than relying on the Spreadsheet `calendar` (which is not accessible while processing the chart) or the Shared\Date `calendar` (which is accessible).
114 lines
4.2 KiB
PHP
114 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Chart;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Chart\Chart;
|
|
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
|
|
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
|
|
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
|
|
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
|
|
use PhpOffice\PhpSpreadsheet\Chart\Title;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class Issue2931Test extends TestCase
|
|
{
|
|
public function testSurface(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
$dataSeriesLabels = [
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['5-6']),
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['6-7']),
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['7-8']),
|
|
];
|
|
|
|
$xAxisTickValues = [
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
|
];
|
|
|
|
$dataSeriesValues = [
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 6, 6, 6, 5.9, 6, 6]),
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 6.5, 7, 7, 7, 7, 7]),
|
|
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 7, 8, 8, 8, 8, 7.9]),
|
|
];
|
|
|
|
$series = new DataSeries(
|
|
DataSeries::TYPE_SURFACECHART,
|
|
DataSeries::GROUPING_STANDARD, // grouping should not be written for surface chart
|
|
range(0, count($dataSeriesValues) - 1),
|
|
$dataSeriesLabels,
|
|
$xAxisTickValues,
|
|
$dataSeriesValues,
|
|
null, // plotDirection
|
|
false, // smooth line
|
|
DataSeries::STYLE_LINEMARKER // plotStyle
|
|
);
|
|
|
|
$plotArea = new PlotArea(null, [$series]);
|
|
$legend = new ChartLegend(ChartLegend::POSITION_BOTTOM, null, false);
|
|
|
|
$title = new Title('График распредления температур в пределах кр');
|
|
|
|
$chart = new Chart(
|
|
'chart2',
|
|
$title,
|
|
$legend,
|
|
$plotArea,
|
|
true,
|
|
DataSeries::EMPTY_AS_GAP,
|
|
);
|
|
|
|
$chart->setTopLeftPosition('$A$1');
|
|
$chart->setBottomRightPosition('$P$20');
|
|
|
|
$sheet->addChart($chart);
|
|
|
|
$writer = new XlsxWriter($spreadsheet);
|
|
$writer->setIncludeCharts(true);
|
|
$writer = new XlsxWriter($spreadsheet);
|
|
$writer->setIncludeCharts(true);
|
|
$writerChart = new XlsxWriter\Chart($writer);
|
|
$data = $writerChart->writeChart($chart);
|
|
|
|
// rotX etc. should be generated for surfaceChart 2D
|
|
// even when unspecified.
|
|
$expectedXml2D = [
|
|
'<c:view3D><c:rotX val="90"/><c:rotY val="0"/><c:rAngAx val="0"/><c:perspective val="0"/></c:view3D>',
|
|
];
|
|
$expectedXml3D = [
|
|
'c:view3D', // empty view3d no longer generated
|
|
];
|
|
$expectedXmlNoX = [
|
|
'c:grouping',
|
|
];
|
|
|
|
// confirm that file contains expected tags
|
|
foreach ($expectedXml2D as $expected) {
|
|
self::assertSame(1, substr_count($data, $expected), $expected);
|
|
}
|
|
foreach ($expectedXmlNoX as $expected) {
|
|
self::assertSame(0, substr_count($data, $expected), $expected);
|
|
}
|
|
|
|
$series->setPlotType(DataSeries::TYPE_SURFACECHART_3D);
|
|
$plotArea = new PlotArea(null, [$series]);
|
|
$chart->setPlotArea($plotArea);
|
|
$writerChart = new XlsxWriter\Chart($writer);
|
|
$data = $writerChart->writeChart($chart);
|
|
// confirm that file contains expected tags
|
|
foreach ($expectedXml3D as $expected) {
|
|
self::assertSame(0, substr_count($data, $expected), $expected);
|
|
}
|
|
foreach ($expectedXmlNoX as $expected) {
|
|
self::assertSame(0, substr_count($data, $expected), $expected);
|
|
}
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|