Skip to content

Address Some Chart Problems #3771

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

Merged
merged 3 commits into from
Nov 9, 2023
Merged
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
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ public function save($filename, int $flags = 0): void
}
}
}
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']) && !isset($zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'])) {
$zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = '<xml></xml>';
}

// Add comment relationship parts
$legacy = $unparsedLoadedData['sheets'][$this->spreadSheet->getSheet($i)->getCodeName()]['legacyDrawing'] ?? null;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Writer/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ private function writeTitle(XMLWriter $objWriter, ?Title $title = null): void
$objWriter->endElement();

$caption = $title->getCaption();
if ((is_array($caption)) && (count($caption) > 0)) {
$caption = $caption[0];
if (is_array($caption)) {
$caption = $caption[0] ?? '';
}
$this->getParentWriter()->getWriterPartstringtable()->writeRichTextForCharts($objWriter, $caption, 'a');

Expand Down
88 changes: 88 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3767Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue3767Test extends AbstractFunctional
{
// some weirdness with this file, including the fact that it has a
// title ('Chart Title') which I cannot find anywhere in the xml.
private static string $testbook = 'tests/data/Reader/XLSX/issue.3767.xlsx';

private string $tempfile = '';

protected function tearDown(): void
{
if ($this->tempfile !== '') {
unlink($this->tempfile);
$this->tempfile = '';
}
}

public function readCharts(XlsxReader $reader): void
{
$reader->setIncludeCharts(true);
}

public function writeCharts(XlsxWriter $writer): void
{
$writer->setIncludeCharts(true);
}

public function testReadWithoutCharts(): void
{
$reader = new XlsxReader();
//$this->readCharts($reader); // Commented out - don't want to read charts.
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
$charts = $sheet->getChartCollection();
self::assertCount(0, $charts);
$this->tempfile = File::temporaryFileName();
$writer = new XlsxWriter($spreadsheet);
$this->writeCharts($writer);
$writer->save($this->tempfile);
$spreadsheet->disconnectWorksheets();
$file = 'zip://';
$file .= $this->tempfile;
$file .= '#xl/worksheets/_rels/sheet1.xml.rels';
$data = (string) file_get_contents($file);
// PhpSpreadsheet still generates this target even though charts aren't included
self::assertStringContainsString('Target="../drawings/drawing1.xml"', $data);
$file = 'zip://';
$file .= $this->tempfile;
$file .= '#xl/drawings/drawing1.xml';
$data = file_get_contents($file);
self::assertSame('<xml></xml>', $data); // fake file because rels needs it
}

public function testReadWithCharts(): void
{
$reader = new XlsxReader();
$this->readCharts($reader);
$spreadsheet = $reader->load(self::$testbook);
$xsheet = $spreadsheet->getActiveSheet();
$xcharts = $xsheet->getChartCollection();
self::assertCount(1, $xcharts);
/** @var callable */
$callableReader = [$this, 'readCharts'];
/** @var callable */
$callableWriter = [$this, 'writeCharts'];
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
$spreadsheet->disconnectWorksheets();
$sheet = $reloadedSpreadsheet->getActiveSheet();
$charts = $xsheet->getChartCollection();
self::assertCount(1, $charts);
// In Excel, a default title ('Chart Title') is shown.
// I can't find that anywhere in the Xml.
self::assertSame('', $charts[0]?->getTitle()?->getCaptionText());
// Just test anything on the chart.
self::assertSame($sheet->getCell('B2')->getValue(), $charts[0]->getPlotArea()?->getPlotGroup()[0]->getPlotValues()[0]->getDataValues()[0]);
$reloadedSpreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3767.xlsx
Binary file not shown.