Skip to content

Commit 051598e

Browse files
authored
Add Chart Axis Option textRotation (#2940)
Fix #2705. Add to Axis class, Reader Xlsx Chart, and Writer Xlsx Chart. Add feature to an existing 32* sample, to an existing 33* sample, and a formal unit test.
1 parent 4bf4278 commit 051598e

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

samples/Chart/33_Chart_create_scatter2.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
$xAxis = new Axis();
125125
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
126126
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
127+
$xAxis->setAxisOption('textRotation', '45');
127128

128129
$yAxis = new Axis();
129130
$yAxis->setLineStyleProperties(
29 Bytes
Binary file not shown.

src/PhpSpreadsheet/Chart/Axis.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function __construct()
6060
'axis_labels' => self::AXIS_LABELS_NEXT_TO,
6161
'horizontal_crosses' => self::HORIZONTAL_CROSSES_AUTOZERO,
6262
'horizontal_crosses_value' => null,
63+
'textRotation' => null,
6364
];
6465

6566
/**
@@ -136,7 +137,8 @@ public function setAxisOptionsProperties(
136137
?string $minimum = null,
137138
?string $maximum = null,
138139
?string $majorUnit = null,
139-
?string $minorUnit = null
140+
?string $minorUnit = null,
141+
?string $textRotation = null
140142
): void {
141143
$this->axisOptions['axis_labels'] = $axisLabels;
142144
$this->setAxisOption('horizontal_crosses_value', $horizontalCrossesValue);
@@ -148,6 +150,7 @@ public function setAxisOptionsProperties(
148150
$this->setAxisOption('maximum', $maximum);
149151
$this->setAxisOption('major_unit', $majorUnit);
150152
$this->setAxisOption('minor_unit', $minorUnit);
153+
$this->setAxisOption('textRotation', $textRotation);
151154
}
152155

153156
/**

src/PhpSpreadsheet/Reader/Xlsx/Chart.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,5 +1279,15 @@ private function setAxisProperties(SimpleXMLElement $chartDetail, ?Axis $whichAx
12791279
if (isset($chartDetail->minorUnit)) {
12801280
$whichAxis->setAxisOption('minor_unit', (string) self::getAttribute($chartDetail->minorUnit, 'val', 'string'));
12811281
}
1282+
if (isset($chartDetail->txPr)) {
1283+
$children = $chartDetail->txPr->children($this->aNamespace);
1284+
if (isset($children->bodyPr)) {
1285+
/** @var string */
1286+
$textRotation = self::getAttribute($children->bodyPr, 'rot', 'string');
1287+
if (is_numeric($textRotation)) {
1288+
$whichAxis->setAxisOption('textRotation', (string) Properties::xmlToAngle($textRotation));
1289+
}
1290+
}
1291+
}
12821292
}
12831293
}

src/PhpSpreadsheet/Writer/Xlsx/Chart.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,23 @@ private function writeCategoryAxis(XMLWriter $objWriter, ?Title $xAxisLabel, $id
573573
$objWriter->endElement();
574574
}
575575

576+
$textRotation = $yAxis->getAxisOptionsProperty('textRotation');
577+
if (is_numeric($textRotation)) {
578+
$objWriter->startElement('c:txPr');
579+
$objWriter->startElement('a:bodyPr');
580+
$objWriter->writeAttribute('rot', Properties::angleToXml((float) $textRotation));
581+
$objWriter->endElement(); // a:bodyPr
582+
$objWriter->startElement('a:lstStyle');
583+
$objWriter->endElement(); // a:lstStyle
584+
$objWriter->startElement('a:p');
585+
$objWriter->startElement('a:pPr');
586+
$objWriter->startElement('a:defRPr');
587+
$objWriter->endElement(); // a:defRPr
588+
$objWriter->endElement(); // a:pPr
589+
$objWriter->endElement(); // a:p
590+
$objWriter->endElement(); // c:txPr
591+
}
592+
576593
$objWriter->startElement('c:spPr');
577594
$this->writeColor($objWriter, $yAxis->getFillColorObject());
578595
$this->writeEffects($objWriter, $yAxis);
@@ -748,6 +765,23 @@ private function writeValueAxis(XMLWriter $objWriter, ?Title $yAxisLabel, $group
748765
$objWriter->endElement();
749766
}
750767

768+
$textRotation = $xAxis->getAxisOptionsProperty('textRotation');
769+
if (is_numeric($textRotation)) {
770+
$objWriter->startElement('c:txPr');
771+
$objWriter->startElement('a:bodyPr');
772+
$objWriter->writeAttribute('rot', Properties::angleToXml((float) $textRotation));
773+
$objWriter->endElement(); // a:bodyPr
774+
$objWriter->startElement('a:lstStyle');
775+
$objWriter->endElement(); // a:lstStyle
776+
$objWriter->startElement('a:p');
777+
$objWriter->startElement('a:pPr');
778+
$objWriter->startElement('a:defRPr');
779+
$objWriter->endElement(); // a:defRPr
780+
$objWriter->endElement(); // a:pPr
781+
$objWriter->endElement(); // a:p
782+
$objWriter->endElement(); // c:txPr
783+
}
784+
751785
$objWriter->startElement('c:spPr');
752786
$this->writeColor($objWriter, $xAxis->getFillColorObject());
753787
$this->writeLineStyles($objWriter, $xAxis);

tests/PhpSpreadsheetTests/Chart/Charts32ScatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ public function testScatter8(): void
391391
$chart = $charts[0];
392392
self::assertNotNull($chart);
393393

394+
$xAxis = $chart->getChartAxisX();
395+
self::assertEquals(45, $xAxis->getAxisOptionsProperty('textRotation'));
396+
394397
$plotArea = $chart->getPlotArea();
395398
self::assertNotNull($plotArea);
396399
$plotSeries = $plotArea->getPlotGroup();

0 commit comments

Comments
 (0)