Skip to content
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

Fix/chart axis titles #1760

Merged
merged 4 commits into from Jan 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Fix for Xlsx Chart axis titles mapping to correct X or Y axis label when only one is present.
- Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx.
- Fix For Null Exception on ODS Read of Page Settings. [#1772](https://github.com/PHPOffice/PhpSpreadsheet/issues/1772)
- Fix Xlsx reader overriding manually set number format with builtin number format. [PR #1805](https://github.com/PHPOffice/PhpSpreadsheet/pull/1805)
- Fix Xlsx reader cell alignment. [PR #1710](https://github.com/PHPOffice/PhpSpreadsheet/pull/1710)
Expand All @@ -58,7 +60,6 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx.
- Fix for Xls Reader when SST has a bad length [#1592](https://github.com/PHPOffice/PhpSpreadsheet/issues/1592)
- Resolve Xlsx loader issue whe hyperlinks don't have a destination
- Resolve issues when printer settings resources IDs clash with drawing IDs
Expand Down
16 changes: 15 additions & 1 deletion src/PhpSpreadsheet/Reader/Xlsx/Chart.php
Expand Up @@ -91,7 +91,21 @@ public static function readChart(SimpleXMLElement $chartElements, $chartName)
break;
case 'valAx':
if (isset($chartDetail->title)) {
$YaxisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
$axisLabel = self::chartTitle($chartDetail->title->children($namespacesChartMeta['c']), $namespacesChartMeta);
$axPos = self::getAttribute($chartDetail->axPos, 'val', 'string');

switch ($axPos) {
case 't':
case 'b':
$XaxisLabel = $axisLabel;

break;
case 'r':
case 'l':
$YaxisLabel = $axisLabel;

break;
}
}

break;
Expand Down
64 changes: 64 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/ChartsTitleTest.php
@@ -0,0 +1,64 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;

function getTitleText($title)
{
if (null === $title || null === $title->getCaption()) {
return null;
}

return implode("\n", array_map(function ($rt) {
return $rt->getPlainText();
}, $title->getCaption()));
}

class ChartsTitleTest extends TestCase
{
public function testChartTitles(): void
{
$filename = 'tests/data/Reader/XLSX/excelChartsTest.xlsx';
$reader = IOFactory::createReader('Xlsx')->setIncludeCharts(true);
$spreadsheet = $reader->load($filename);
$worksheet = $spreadsheet->getActiveSheet();

$charts = $worksheet->getChartCollection();
self::assertEquals(5, $worksheet->getChartCount());
self::assertCount(5, $charts);

// No title or axis labels
$chart1 = $charts[0];
$title = getTitleText($chart1->getTitle());
self::assertEmpty($title);
self::assertEmpty(getTitleText($chart1->getXAxisLabel()));
self::assertEmpty(getTitleText($chart1->getYAxisLabel()));

// Title, no axis labels
$chart2 = $charts[1];

self::assertEquals('Chart with Title and no Axis Labels', getTitleText($chart2->getTitle()));
self::assertEmpty(getTitleText($chart2->getXAxisLabel()));
self::assertEmpty(getTitleText($chart2->getYAxisLabel()));

// No title, only horizontal axis label
$chart3 = $charts[2];
self::assertEmpty(getTitleText($chart3->getTitle()));
self::assertEquals('Horizontal Axis Title Only', getTitleText($chart3->getXAxisLabel()));
self::assertEmpty(getTitleText($chart3->getYAxisLabel()));

// No title, only vertical axis label
$chart4 = $charts[3];
self::assertEmpty(getTitleText($chart4->getTitle()));
self::assertEquals('Vertical Axis Title Only', getTitleText($chart4->getYAxisLabel()));
self::assertEmpty(getTitleText($chart4->getXAxisLabel()));

// Title and both axis labels
$chart5 = $charts[4];
self::assertEquals('Complete Annotations', getTitleText($chart5->getTitle()));
self::assertEquals('Horizontal Axis Title', getTitleText($chart5->getXAxisLabel()));
self::assertEquals('Vertical Axis Title', getTitleText($chart5->getYAxisLabel()));
}
}
Binary file added tests/data/Reader/XLSX/excelChartsTest.xlsx
Binary file not shown.