Skip to content

Commit

Permalink
Add Chart Axis Option textRotation (#2940)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman committed Jul 17, 2022
1 parent 4bf4278 commit 051598e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/Chart/33_Chart_create_scatter2.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
$xAxis = new Axis();
//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
$xAxis->setAxisOption('textRotation', '45');

$yAxis = new Axis();
$yAxis->setLineStyleProperties(
Expand Down
Binary file modified samples/templates/32readwriteScatterChart8.xlsx
Binary file not shown.
5 changes: 4 additions & 1 deletion src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct()
'axis_labels' => self::AXIS_LABELS_NEXT_TO,
'horizontal_crosses' => self::HORIZONTAL_CROSSES_AUTOZERO,
'horizontal_crosses_value' => null,
'textRotation' => null,
];

/**
Expand Down Expand Up @@ -136,7 +137,8 @@ public function setAxisOptionsProperties(
?string $minimum = null,
?string $maximum = null,
?string $majorUnit = null,
?string $minorUnit = null
?string $minorUnit = null,
?string $textRotation = null
): void {
$this->axisOptions['axis_labels'] = $axisLabels;
$this->setAxisOption('horizontal_crosses_value', $horizontalCrossesValue);
Expand All @@ -148,6 +150,7 @@ public function setAxisOptionsProperties(
$this->setAxisOption('maximum', $maximum);
$this->setAxisOption('major_unit', $majorUnit);
$this->setAxisOption('minor_unit', $minorUnit);
$this->setAxisOption('textRotation', $textRotation);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,5 +1279,15 @@ private function setAxisProperties(SimpleXMLElement $chartDetail, ?Axis $whichAx
if (isset($chartDetail->minorUnit)) {
$whichAxis->setAxisOption('minor_unit', (string) self::getAttribute($chartDetail->minorUnit, 'val', 'string'));
}
if (isset($chartDetail->txPr)) {
$children = $chartDetail->txPr->children($this->aNamespace);
if (isset($children->bodyPr)) {
/** @var string */
$textRotation = self::getAttribute($children->bodyPr, 'rot', 'string');
if (is_numeric($textRotation)) {
$whichAxis->setAxisOption('textRotation', (string) Properties::xmlToAngle($textRotation));
}
}
}
}
}
34 changes: 34 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,23 @@ private function writeCategoryAxis(XMLWriter $objWriter, ?Title $xAxisLabel, $id
$objWriter->endElement();
}

$textRotation = $yAxis->getAxisOptionsProperty('textRotation');
if (is_numeric($textRotation)) {
$objWriter->startElement('c:txPr');
$objWriter->startElement('a:bodyPr');
$objWriter->writeAttribute('rot', Properties::angleToXml((float) $textRotation));
$objWriter->endElement(); // a:bodyPr
$objWriter->startElement('a:lstStyle');
$objWriter->endElement(); // a:lstStyle
$objWriter->startElement('a:p');
$objWriter->startElement('a:pPr');
$objWriter->startElement('a:defRPr');
$objWriter->endElement(); // a:defRPr
$objWriter->endElement(); // a:pPr
$objWriter->endElement(); // a:p
$objWriter->endElement(); // c:txPr
}

$objWriter->startElement('c:spPr');
$this->writeColor($objWriter, $yAxis->getFillColorObject());
$this->writeEffects($objWriter, $yAxis);
Expand Down Expand Up @@ -748,6 +765,23 @@ private function writeValueAxis(XMLWriter $objWriter, ?Title $yAxisLabel, $group
$objWriter->endElement();
}

$textRotation = $xAxis->getAxisOptionsProperty('textRotation');
if (is_numeric($textRotation)) {
$objWriter->startElement('c:txPr');
$objWriter->startElement('a:bodyPr');
$objWriter->writeAttribute('rot', Properties::angleToXml((float) $textRotation));
$objWriter->endElement(); // a:bodyPr
$objWriter->startElement('a:lstStyle');
$objWriter->endElement(); // a:lstStyle
$objWriter->startElement('a:p');
$objWriter->startElement('a:pPr');
$objWriter->startElement('a:defRPr');
$objWriter->endElement(); // a:defRPr
$objWriter->endElement(); // a:pPr
$objWriter->endElement(); // a:p
$objWriter->endElement(); // c:txPr
}

$objWriter->startElement('c:spPr');
$this->writeColor($objWriter, $xAxis->getFillColorObject());
$this->writeLineStyles($objWriter, $xAxis);
Expand Down
3 changes: 3 additions & 0 deletions tests/PhpSpreadsheetTests/Chart/Charts32ScatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ public function testScatter8(): void
$chart = $charts[0];
self::assertNotNull($chart);

$xAxis = $chart->getChartAxisX();
self::assertEquals(45, $xAxis->getAxisOptionsProperty('textRotation'));

$plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup();
Expand Down

0 comments on commit 051598e

Please sign in to comment.