Skip to content

Commit

Permalink
Fix/chart axis titles (#1760)
Browse files Browse the repository at this point in the history
* use axPos value to determine whether an axis title is mapped to the XaxisLabel or YaxisLabel

* update changelog

* Fix php-cs-fixer violations

Co-authored-by: Darren Maczka <dkm@utk.edu>
Co-authored-by: Mark Baker <mark@lange.demon.co.uk>
  • Loading branch information
3 people committed Jan 31, 2021
1 parent 44248cd commit c82ff25
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
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.

0 comments on commit c82ff25

Please sign in to comment.