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

Chart Axis Display Units and Logarithmic Scale #3836

Merged
merged 2 commits into from
Dec 22, 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
2 changes: 1 addition & 1 deletion samples/Chart/33_Chart_create_line_dateaxis.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@
$spreadsheet->setActiveSheetIndex(1);

// Save Excel 2007 file
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true);
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true, resetActiveSheet: false);
$spreadsheet->disconnectWorksheets();

function dateRange(int $nrows, Spreadsheet $wrkbk): array
Expand Down
2 changes: 1 addition & 1 deletion samples/Chart/33_Chart_create_scatter5_trendlines.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,4 @@
$spreadsheet->setActiveSheetIndex(1);

// Save Excel 2007 file
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true);
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true, resetActiveSheet: false);
2 changes: 1 addition & 1 deletion samples/Chart/33_Chart_create_scatter6_value_xaxis.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@
$spreadsheet->setActiveSheetIndex(1);

// Save Excel 2007 file
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true);
$helper->write($spreadsheet, __FILE__, ['Xlsx'], true, resetActiveSheet: false);
43 changes: 42 additions & 1 deletion src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function __construct()

private ?AxisText $axisText = null;

private ?Title $dispUnitsTitle = null;

/**
* Axis Options.
*
Expand All @@ -70,6 +72,28 @@ public function __construct()
'majorTimeUnit' => self::TIME_UNIT_YEARS,
'minorTimeUnit' => self::TIME_UNIT_MONTHS,
'baseTimeUnit' => self::TIME_UNIT_DAYS,
'logBase' => null,
'dispUnitsBuiltIn' => null,
];
public const DISP_UNITS_HUNDREDS = 'hundreds';
public const DISP_UNITS_THOUSANDS = 'thousands';
public const DISP_UNITS_TEN_THOUSANDS = 'tenThousands';
public const DISP_UNITS_HUNDRED_THOUSANDS = 'hundredThousands';
public const DISP_UNITS_MILLIONS = 'millions';
public const DISP_UNITS_TEN_MILLIONS = 'tenMillions';
public const DISP_UNITS_HUNDRED_MILLIONS = 'hundredMillions';
public const DISP_UNITS_BILLIONS = 'billions';
public const DISP_UNITS_TRILLIONS = 'trillions';
public const DISP_UNITS_BUILTIN_INT = [
100 => self::DISP_UNITS_HUNDREDS,
1000 => self::DISP_UNITS_THOUSANDS,
10000 => self::DISP_UNITS_TEN_THOUSANDS,
100000 => self::DISP_UNITS_HUNDRED_THOUSANDS,
1000000 => self::DISP_UNITS_MILLIONS,
10000000 => self::DISP_UNITS_TEN_MILLIONS,
100000000 => self::DISP_UNITS_HUNDRED_MILLIONS,
1000000000 => self::DISP_UNITS_BILLIONS,
1000000000000 => self::DISP_UNITS_TRILLIONS,
];

/**
Expand Down Expand Up @@ -149,7 +173,9 @@ public function setAxisOptionsProperties(
?string $hidden = null,
?string $baseTimeUnit = null,
?string $majorTimeUnit = null,
?string $minorTimeUnit = null
?string $minorTimeUnit = null,
null|float|int|string $logBase = null,
?string $dispUnitsBuiltIn = null
): void {
$this->axisOptions['axis_labels'] = $axisLabels;
$this->setAxisOption('horizontal_crosses_value', $horizontalCrossesValue);
Expand All @@ -166,6 +192,8 @@ public function setAxisOptionsProperties(
$this->setAxisOption('baseTimeUnit', $baseTimeUnit);
$this->setAxisOption('majorTimeUnit', $majorTimeUnit);
$this->setAxisOption('minorTimeUnit', $minorTimeUnit);
$this->setAxisOption('logBase', $logBase);
$this->setAxisOption('dispUnitsBuiltIn', $dispUnitsBuiltIn);
}

/**
Expand Down Expand Up @@ -317,6 +345,18 @@ public function getNoFill(): bool
return $this->noFill;
}

public function setDispUnitsTitle(?Title $dispUnitsTitle): self
{
$this->dispUnitsTitle = $dispUnitsTitle;

return $this;
}

public function getDispUnitsTitle(): ?Title
{
return $this->dispUnitsTitle;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
Expand All @@ -326,6 +366,7 @@ public function __clone()
$this->majorGridlines = ($this->majorGridlines === null) ? null : clone $this->majorGridlines;
$this->majorGridlines = ($this->minorGridlines === null) ? null : clone $this->minorGridlines;
$this->axisText = ($this->axisText === null) ? null : clone $this->axisText;
$this->dispUnitsTitle = ($this->dispUnitsTitle === null) ? null : clone $this->dispUnitsTitle;
$this->fillColor = clone $this->fillColor;
}
}
11 changes: 7 additions & 4 deletions src/PhpSpreadsheet/Chart/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Title
/**
* Title Caption.
*
* @var array|RichText|string
* @var null|array|RichText|string may be null
*/
private $caption = '';

Expand All @@ -39,7 +39,7 @@ class Title
/**
* Create a new Title.
*
* @param array|RichText|string $caption
* @param null|array|RichText|string $caption may be null
* @param bool $overlay
*/
public function __construct($caption = '', ?Layout $layout = null, $overlay = false)
Expand All @@ -52,7 +52,7 @@ public function __construct($caption = '', ?Layout $layout = null, $overlay = fa
/**
* Get caption.
*
* @return array|RichText|string
* @return null|array|RichText|string may be null
*/
public function getCaption()
{
Expand All @@ -68,6 +68,9 @@ public function getCaptionText(?Spreadsheet $spreadsheet = null): string
}
}
$caption = $this->caption;
if ($caption === null) {
return '';
}
if (is_string($caption)) {
return $caption;
}
Expand All @@ -91,7 +94,7 @@ public function getCaptionText(?Spreadsheet $spreadsheet = null): string
/**
* Set caption.
*
* @param array|RichText|string $caption
* @param null|array|RichText|string $caption may be null
*
* @return $this
*/
Expand Down
7 changes: 4 additions & 3 deletions src/PhpSpreadsheet/Helper/Sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public function getSamples(): array
/**
* Write documents.
*
* @param string $filename
* @param string[] $writers
*/
public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls'], bool $withCharts = false, ?callable $writerCallback = null): void
public function write(Spreadsheet $spreadsheet, string $filename, array $writers = ['Xlsx', 'Xls'], bool $withCharts = false, ?callable $writerCallback = null, bool $resetActiveSheet = true): void
{
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
if ($resetActiveSheet) {
$spreadsheet->setActiveSheetIndex(0);
}

// Write documents
foreach ($writers as $writerType) {
Expand Down
13 changes: 12 additions & 1 deletion src/PhpSpreadsheet/Reader/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public function readChart(SimpleXMLElement $chartElements, $chartName): \PhpOffi

private function chartTitle(SimpleXMLElement $titleDetails): Title
{
$caption = [];
$caption = null;
$titleLayout = null;
$titleOverlay = false;
$titleFormula = null;
Expand All @@ -503,6 +503,7 @@ private function chartTitle(SimpleXMLElement $titleDetails): Title
$chartDetail = Xlsx::testSimpleXml($chartDetail);
switch ($titleDetailKey) {
case 'tx':
$caption = [];
if (isset($chartDetail->rich)) {
$titleDetails = $chartDetail->rich->children($this->aNamespace);
foreach ($titleDetails as $titleKey => $titleDetail) {
Expand Down Expand Up @@ -1518,6 +1519,13 @@ private function setAxisProperties(SimpleXMLElement $chartDetail, ?Axis $whichAx
if (isset($chartDetail->crossBetween)) {
$whichAxis->setCrossBetween((string) self::getAttributeString($chartDetail->crossBetween, 'val'));
}
if (isset($chartDetail->dispUnits, $chartDetail->dispUnits->builtInUnit)) {
$whichAxis->setAxisOption('dispUnitsBuiltIn', (string) self::getAttributeString($chartDetail->dispUnits->builtInUnit, 'val'));
if (isset($chartDetail->dispUnits->dispUnitsLbl)) {
$whichAxis->setDispUnitsTitle(new Title());
// TODO parse title elements
}
}
if (isset($chartDetail->majorTickMark)) {
$whichAxis->setAxisOption('major_tick_mark', (string) self::getAttributeString($chartDetail->majorTickMark, 'val'));
}
Expand All @@ -1533,6 +1541,9 @@ private function setAxisProperties(SimpleXMLElement $chartDetail, ?Axis $whichAx
if (isset($chartDetail->crossesAt)) {
$whichAxis->setAxisOption('horizontal_crosses_value', (string) self::getAttributeString($chartDetail->crossesAt, 'val'));
}
if (isset($chartDetail->scaling->logBase)) {
$whichAxis->setAxisOption('logBase', (string) self::getAttributeString($chartDetail->scaling->logBase, 'val'));
}
if (isset($chartDetail->scaling->orientation)) {
$whichAxis->setAxisOption('orientation', (string) self::getAttributeString($chartDetail->scaling->orientation, 'val'));
}
Expand Down
Loading