<?php
namespace PhpOffice\PhpSpreadsheet\Chart;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Chart
{
/**
* Chart Name.
*
* @var string
*/
private $name = '';
/**
* Worksheet.
*
< * @var Worksheet
> * @var ?Worksheet
*/
private $worksheet;
/**
* Chart Title.
*
< * @var Title
> * @var ?Title
*/
private $title;
/**
* Chart Legend.
*
< * @var Legend
> * @var ?Legend
*/
private $legend;
/**
* X-Axis Label.
*
< * @var Title
> * @var ?Title
*/
private $xAxisLabel;
/**
* Y-Axis Label.
*
< * @var Title
> * @var ?Title
*/
private $yAxisLabel;
/**
* Chart Plot Area.
*
< * @var PlotArea
> * @var ?PlotArea
*/
private $plotArea;
/**
* Plot Visible Only.
*
* @var bool
*/
private $plotVisibleOnly = true;
/**
* Display Blanks as.
*
* @var string
*/
< private $displayBlanksAs = '0';
> private $displayBlanksAs = DataSeries::EMPTY_AS_GAP;
/**
* Chart Asix Y as.
*
* @var Axis
*/
private $yAxis;
/**
* Chart Asix X as.
*
* @var Axis
*/
private $xAxis;
/**
< * Chart Major Gridlines as.
< *
< * @var GridLines
< */
< private $majorGridlines;
<
< /**
< * Chart Minor Gridlines as.
< *
< * @var GridLines
< */
< private $minorGridlines;
<
< /**
* Top-Left Cell Position.
*
* @var string
*/
private $topLeftCellRef = 'A1';
/**
* Top-Left X-Offset.
*
* @var int
*/
private $topLeftXOffset = 0;
/**
* Top-Left Y-Offset.
*
* @var int
*/
private $topLeftYOffset = 0;
/**
* Bottom-Right Cell Position.
*
* @var string
*/
< private $bottomRightCellRef = 'A1';
> private $bottomRightCellRef = '';
/**
* Bottom-Right X-Offset.
*
* @var int
*/
private $bottomRightXOffset = 10;
/**
* Bottom-Right Y-Offset.
*
* @var int
*/
private $bottomRightYOffset = 10;
> /** @var ?int */
/**
> private $rotX;
* Create a new Chart.
>
*
> /** @var ?int */
* @param mixed $name
> private $rotY;
* @param null|Title $title
>
* @param null|Legend $legend
> /** @var ?int */
* @param null|PlotArea $plotArea
> private $rAngAx;
* @param mixed $plotVisibleOnly
>
* @param mixed $displayBlanksAs
> /** @var ?int */
* @param null|Title $xAxisLabel
> private $perspective;
* @param null|Title $yAxisLabel
>
* @param null|Axis $xAxis
> /** @var bool */
* @param null|Axis $yAxis
> private $oneCellAnchor = false;
* @param null|GridLines $majorGridlines
>
* @param null|GridLines $minorGridlines
> /** @var bool */
*/
> private $autoTitleDeleted = false;
public function __construct($name, Title $title = null, Legend $legend = null, PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = 'gap', Title $xAxisLabel = null, Title $yAxisLabel = null, Axis $xAxis = null, Axis $yAxis = null, GridLines $majorGridlines = null, GridLines $minorGridlines = null)
>
{
> /** @var bool */
$this->name = $name;
> private $noFill = false;
$this->title = $title;
>
$this->legend = $legend;
> /** @var bool */
$this->xAxisLabel = $xAxisLabel;
> private $roundedCorners = false;
$this->yAxisLabel = $yAxisLabel;
>
$this->plotArea = $plotArea;
> /** @var GridLines */
$this->plotVisibleOnly = $plotVisibleOnly;
> private $borderLines;
$this->displayBlanksAs = $displayBlanksAs;
>
$this->xAxis = $xAxis;
> /** @var ChartColor */
$this->yAxis = $yAxis;
> private $fillColor;
$this->majorGridlines = $majorGridlines;
>
$this->minorGridlines = $minorGridlines;
> * majorGridlines and minorGridlines are deprecated, moved to Axis.
< * @param null|Title $title
< * @param null|Legend $legend
< * @param null|PlotArea $plotArea
< * @param mixed $displayBlanksAs
< * @param null|Title $xAxisLabel
< * @param null|Title $yAxisLabel
< * @param null|Axis $xAxis
< * @param null|Axis $yAxis
< * @param null|GridLines $majorGridlines
< * @param null|GridLines $minorGridlines
> * @param string $displayBlanksAs
< public function __construct($name, Title $title = null, Legend $legend = null, PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = 'gap', Title $xAxisLabel = null, Title $yAxisLabel = null, Axis $xAxis = null, Axis $yAxis = null, GridLines $majorGridlines = null, GridLines $minorGridlines = null)
> public function __construct($name, ?Title $title = null, ?Legend $legend = null, ?PlotArea $plotArea = null, $plotVisibleOnly = true, $displayBlanksAs = DataSeries::EMPTY_AS_GAP, ?Title $xAxisLabel = null, ?Title $yAxisLabel = null, ?Axis $xAxis = null, ?Axis $yAxis = null, ?GridLines $majorGridlines = null, ?GridLines $minorGridlines = null)
< $this->xAxis = $xAxis;
< $this->yAxis = $yAxis;
< $this->majorGridlines = $majorGridlines;
< $this->minorGridlines = $minorGridlines;
> $this->xAxis = $xAxis ?? new Axis();
> $this->yAxis = $yAxis ?? new Axis();
> if ($majorGridlines !== null) {
> $this->yAxis->setMajorGridlines($majorGridlines);
> }
> if ($minorGridlines !== null) {
> $this->yAxis->setMinorGridlines($minorGridlines);
> }
> $this->fillColor = new ChartColor();
> $this->borderLines = new GridLines();
* @return Worksheet
> public function setName(string $name): self
*/
> {
public function getWorksheet()
> $this->name = $name;
{
>
return $this->worksheet;
> return $this;
}
> }
>
< *
< * @return Worksheet
< public function getWorksheet()
> public function getWorksheet(): ?Worksheet
< * @param Worksheet $pValue
< *
< * @return Chart
> * @return $this
*/
< public function setWorksheet(Worksheet $pValue = null)
> public function setWorksheet(?Worksheet $worksheet = null)
{
< $this->worksheet = $pValue;
> $this->worksheet = $worksheet;
return $this;
}
< /**
< * Get Title.
< *
< * @return Title
< */
< public function getTitle()
> public function getTitle(): ?Title
{
return $this->title;
}
/**
* Set Title.
*
< * @param Title $title
< *
< * @return Chart
> * @return $this
*/
public function setTitle(Title $title)
{
$this->title = $title;
return $this;
}
< /**
< * Get Legend.
< *
< * @return Legend
< */
< public function getLegend()
> public function getLegend(): ?Legend
{
return $this->legend;
}
/**
* Set Legend.
*
< * @param Legend $legend
< *
< * @return Chart
> * @return $this
*/
public function setLegend(Legend $legend)
{
$this->legend = $legend;
return $this;
}
< /**
< * Get X-Axis Label.
< *
< * @return Title
< */
< public function getXAxisLabel()
> public function getXAxisLabel(): ?Title
{
return $this->xAxisLabel;
}
/**
* Set X-Axis Label.
*
< * @param Title $label
< *
< * @return Chart
> * @return $this
*/
public function setXAxisLabel(Title $label)
{
$this->xAxisLabel = $label;
return $this;
}
< /**
< * Get Y-Axis Label.
< *
< * @return Title
< */
< public function getYAxisLabel()
> public function getYAxisLabel(): ?Title
{
return $this->yAxisLabel;
}
/**
* Set Y-Axis Label.
*
< * @param Title $label
< *
< * @return Chart
> * @return $this
*/
public function setYAxisLabel(Title $label)
{
$this->yAxisLabel = $label;
return $this;
}
> public function getPlotArea(): ?PlotArea
/**
> {
* Get Plot Area.
> return $this->plotArea;
*
> }
* @return PlotArea
>
< * Get Plot Area.
< *
< * @return PlotArea
> * Set Plot Area.
< public function getPlotArea()
> public function setPlotArea(PlotArea $plotArea): self
< return $this->plotArea;
> $this->plotArea = $plotArea;
>
> return $this;
/**
* Get Plot Visible Only.
*
* @return bool
*/
public function getPlotVisibleOnly()
{
return $this->plotVisibleOnly;
}
/**
* Set Plot Visible Only.
*
* @param bool $plotVisibleOnly
*
< * @return Chart
> * @return $this
*/
public function setPlotVisibleOnly($plotVisibleOnly)
{
$this->plotVisibleOnly = $plotVisibleOnly;
return $this;
}
/**
* Get Display Blanks as.
*
* @return string
*/
public function getDisplayBlanksAs()
{
return $this->displayBlanksAs;
}
/**
* Set Display Blanks as.
*
* @param string $displayBlanksAs
*
< * @return Chart
> * @return $this
*/
public function setDisplayBlanksAs($displayBlanksAs)
{
$this->displayBlanksAs = $displayBlanksAs;
return $this;
}
> public function getChartAxisY(): Axis
/**
> {
* Get yAxis.
> return $this->yAxis;
*
> }
* @return Axis
>
< * Get yAxis.
< *
< * @return Axis
> * Set yAxis.
< public function getChartAxisY()
> public function setChartAxisY(?Axis $axis): self
< if ($this->yAxis !== null) {
< return $this->yAxis;
> $this->yAxis = $axis ?? new Axis();
>
> return $this;
< return new Axis();
> public function getChartAxisX(): Axis
> {
> return $this->xAxis;
}
/**
< * Get xAxis.
< *
< * @return Axis
> * Set xAxis.
*/
< public function getChartAxisX()
> public function setChartAxisX(?Axis $axis): self
{
< if ($this->xAxis !== null) {
< return $this->xAxis;
< }
> $this->xAxis = $axis ?? new Axis();
< return new Axis();
> return $this;
}
/**
* Get Major Gridlines.
*
< * @return GridLines
> * @deprecated 1.24.0 Use Axis->getMajorGridlines()
> * @see Axis::getMajorGridlines()
> *
> * @codeCoverageIgnore
*/
< public function getMajorGridlines()
> public function getMajorGridlines(): ?GridLines
{
< if ($this->majorGridlines !== null) {
< return $this->majorGridlines;
< }
<
< return new GridLines();
> return $this->yAxis->getMajorGridLines();
}
/**
* Get Minor Gridlines.
*
< * @return GridLines
> * @deprecated 1.24.0 Use Axis->getMinorGridlines()
> * @see Axis::getMinorGridlines()
> *
> * @codeCoverageIgnore
*/
< public function getMinorGridlines()
> public function getMinorGridlines(): ?GridLines
{
< if ($this->minorGridlines !== null) {
< return $this->minorGridlines;
< }
<
< return new GridLines();
> return $this->yAxis->getMinorGridLines();
}
/**
* Set the Top Left position for the chart.
*
< * @param string $cell
> * @param string $cellAddress
* @param int $xOffset
* @param int $yOffset
*
< * @return Chart
> * @return $this
*/
< public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null)
> public function setTopLeftPosition($cellAddress, $xOffset = null, $yOffset = null)
{
< $this->topLeftCellRef = $cell;
> $this->topLeftCellRef = $cellAddress;
if ($xOffset !== null) {
$this->setTopLeftXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setTopLeftYOffset($yOffset);
}
return $this;
}
/**
* Get the top left position of the chart.
*
< * @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
> * Returns ['cell' => string cell address, 'xOffset' => int, 'yOffset' => int].
> *
> * @return array{cell: string, xOffset: int, yOffset: int} an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
*/
< public function getTopLeftPosition()
> public function getTopLeftPosition(): array
{
return [
'cell' => $this->topLeftCellRef,
'xOffset' => $this->topLeftXOffset,
'yOffset' => $this->topLeftYOffset,
];
}
/**
* Get the cell address where the top left of the chart is fixed.
*
* @return string
*/
public function getTopLeftCell()
{
return $this->topLeftCellRef;
}
/**
* Set the Top Left cell position for the chart.
*
< * @param string $cell
> * @param string $cellAddress
*
< * @return Chart
> * @return $this
*/
< public function setTopLeftCell($cell)
> public function setTopLeftCell($cellAddress)
{
< $this->topLeftCellRef = $cell;
> $this->topLeftCellRef = $cellAddress;
return $this;
}
/**
* Set the offset position within the Top Left cell for the chart.
*
< * @param int $xOffset
< * @param int $yOffset
> * @param ?int $xOffset
> * @param ?int $yOffset
*
< * @return Chart
> * @return $this
*/
public function setTopLeftOffset($xOffset, $yOffset)
{
if ($xOffset !== null) {
$this->setTopLeftXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setTopLeftYOffset($yOffset);
}
return $this;
}
/**
* Get the offset position within the Top Left cell for the chart.
*
* @return int[]
*/
public function getTopLeftOffset()
{
return [
'X' => $this->topLeftXOffset,
'Y' => $this->topLeftYOffset,
];
}
> /**
public function setTopLeftXOffset($xOffset)
> * @param int $xOffset
{
> *
$this->topLeftXOffset = $xOffset;
> * @return $this
> */
return $this;
}
< public function getTopLeftXOffset()
> public function getTopLeftXOffset(): int
{
return $this->topLeftXOffset;
}
> /**
public function setTopLeftYOffset($yOffset)
> * @param int $yOffset
{
> *
$this->topLeftYOffset = $yOffset;
> * @return $this
> */
return $this;
}
< public function getTopLeftYOffset()
> public function getTopLeftYOffset(): int
{
return $this->topLeftYOffset;
}
/**
* Set the Bottom Right position of the chart.
*
< * @param string $cell
> * @param string $cellAddress
* @param int $xOffset
* @param int $yOffset
*
< * @return Chart
> * @return $this
*/
< public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
> public function setBottomRightPosition($cellAddress = '', $xOffset = null, $yOffset = null)
{
< $this->bottomRightCellRef = $cell;
> $this->bottomRightCellRef = $cellAddress;
if ($xOffset !== null) {
$this->setBottomRightXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setBottomRightYOffset($yOffset);
}
return $this;
}
/**
* Get the bottom right position of the chart.
*
* @return array an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
*/
public function getBottomRightPosition()
{
return [
'cell' => $this->bottomRightCellRef,
'xOffset' => $this->bottomRightXOffset,
'yOffset' => $this->bottomRightYOffset,
];
}
< public function setBottomRightCell($cell)
> /**
> * Set the Bottom Right cell for the chart.
> *
> * @return $this
> */
> public function setBottomRightCell(string $cellAddress = '')
{
< $this->bottomRightCellRef = $cell;
> $this->bottomRightCellRef = $cellAddress;
return $this;
}
/**
* Get the cell address where the bottom right of the chart is fixed.
< *
< * @return string
*/
< public function getBottomRightCell()
> public function getBottomRightCell(): string
{
return $this->bottomRightCellRef;
}
/**
* Set the offset position within the Bottom Right cell for the chart.
*
< * @param int $xOffset
< * @param int $yOffset
> * @param ?int $xOffset
> * @param ?int $yOffset
*
< * @return Chart
> * @return $this
*/
public function setBottomRightOffset($xOffset, $yOffset)
{
if ($xOffset !== null) {
$this->setBottomRightXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setBottomRightYOffset($yOffset);
}
return $this;
}
/**
* Get the offset position within the Bottom Right cell for the chart.
*
* @return int[]
*/
public function getBottomRightOffset()
{
return [
'X' => $this->bottomRightXOffset,
'Y' => $this->bottomRightYOffset,
];
}
> /**
public function setBottomRightXOffset($xOffset)
> * @param int $xOffset
{
> *
$this->bottomRightXOffset = $xOffset;
> * @return $this
> */
return $this;
}
< public function getBottomRightXOffset()
> public function getBottomRightXOffset(): int
{
return $this->bottomRightXOffset;
}
> /**
public function setBottomRightYOffset($yOffset)
> * @param int $yOffset
{
> *
$this->bottomRightYOffset = $yOffset;
> * @return $this
> */
return $this;
}
< public function getBottomRightYOffset()
> public function getBottomRightYOffset(): int
{
return $this->bottomRightYOffset;
}
< public function refresh()
> public function refresh(): void
{
< if ($this->worksheet !== null) {
> if ($this->worksheet !== null && $this->plotArea !== null) {
$this->plotArea->refresh($this->worksheet);
}
}
/**
* Render the chart to given file (or stream).
*
* @param string $outputDestination Name of the file render to
*
* @return bool true on success
*/
public function render($outputDestination = null)
{
if ($outputDestination == 'php://output') {
$outputDestination = null;
}
$libraryName = Settings::getChartRenderer();
if ($libraryName === null) {
return false;
}
// Ensure that data series values are up-to-date before we render
$this->refresh();
$renderer = new $libraryName($this);
< return $renderer->render($outputDestination);
> return $renderer->render($outputDestination); // @phpstan-ignore-line
> }
>
> public function getRotX(): ?int
> {
> return $this->rotX;
> }
>
> public function setRotX(?int $rotX): self
> {
> $this->rotX = $rotX;
>
> return $this;
> }
>
> public function getRotY(): ?int
> {
> return $this->rotY;
> }
>
> public function setRotY(?int $rotY): self
> {
> $this->rotY = $rotY;
>
> return $this;
> }
>
> public function getRAngAx(): ?int
> {
> return $this->rAngAx;
> }
>
> public function setRAngAx(?int $rAngAx): self
> {
> $this->rAngAx = $rAngAx;
>
> return $this;
> }
>
> public function getPerspective(): ?int
> {
> return $this->perspective;
> }
>
> public function setPerspective(?int $perspective): self
> {
> $this->perspective = $perspective;
>
> return $this;
> }
>
> public function getOneCellAnchor(): bool
> {
> return $this->oneCellAnchor;
> }
>
> public function setOneCellAnchor(bool $oneCellAnchor): self
> {
> $this->oneCellAnchor = $oneCellAnchor;
>
> return $this;
> }
>
> public function getAutoTitleDeleted(): bool
> {
> return $this->autoTitleDeleted;
> }
>
> public function setAutoTitleDeleted(bool $autoTitleDeleted): self
> {
> $this->autoTitleDeleted = $autoTitleDeleted;
>
> return $this;
> }
>
> public function getNoFill(): bool
> {
> return $this->noFill;
> }
>
> public function setNoFill(bool $noFill): self
> {
> $this->noFill = $noFill;
>
> return $this;
> }
>
> public function getRoundedCorners(): bool
> {
> return $this->roundedCorners;
> }
>
> public function setRoundedCorners(?bool $roundedCorners): self
> {
> if ($roundedCorners !== null) {
> $this->roundedCorners = $roundedCorners;
> }
>
> return $this;
> }
>
> public function getBorderLines(): GridLines
> {
> return $this->borderLines;
> }
>
> public function setBorderLines(GridLines $borderLines): self
> {
> $this->borderLines = $borderLines;
>
> return $this;
> }
>
> public function getFillColor(): ChartColor
> {
> return $this->fillColor;
}
}