<?php
namespace PhpOffice\PhpSpreadsheet\Chart;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Chart
{
private $name = '';
private $worksheet;
private $title;
private $legend;
private $xAxisLabel;
private $yAxisLabel;
private $plotArea;
private $plotVisibleOnly = true;
private $displayBlanksAs = DataSeries::EMPTY_AS_GAP;
private $yAxis;
private $xAxis;
private $majorGridlines;
private $minorGridlines;
private $topLeftCellRef = 'A1';
private $topLeftXOffset = 0;
private $topLeftYOffset = 0;
private $bottomRightCellRef = 'A1';
private $bottomRightXOffset = 10;
private $bottomRightYOffset = 10;
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->name = $name;
$this->title = $title;
$this->legend = $legend;
$this->xAxisLabel = $xAxisLabel;
$this->yAxisLabel = $yAxisLabel;
$this->plotArea = $plotArea;
$this->plotVisibleOnly = $plotVisibleOnly;
$this->displayBlanksAs = $displayBlanksAs;
$this->xAxis = $xAxis;
$this->yAxis = $yAxis;
$this->majorGridlines = $majorGridlines;
$this->minorGridlines = $minorGridlines;
}
public function getName()
{
return $this->name;
}
public function getWorksheet()
{
return $this->worksheet;
}
public function setWorksheet(?Worksheet $pValue = null)
{
$this->worksheet = $pValue;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setTitle(Title $title)
{
$this->title = $title;
return $this;
}
public function getLegend()
{
return $this->legend;
}
public function setLegend(Legend $legend)
{
$this->legend = $legend;
return $this;
}
public function getXAxisLabel()
{
return $this->xAxisLabel;
}
public function setXAxisLabel(Title $label)
{
$this->xAxisLabel = $label;
return $this;
}
public function getYAxisLabel()
{
return $this->yAxisLabel;
}
public function setYAxisLabel(Title $label)
{
$this->yAxisLabel = $label;
return $this;
}
public function getPlotArea()
{
return $this->plotArea;
}
public function getPlotVisibleOnly()
{
return $this->plotVisibleOnly;
}
public function setPlotVisibleOnly($plotVisibleOnly)
{
$this->plotVisibleOnly = $plotVisibleOnly;
return $this;
}
public function getDisplayBlanksAs()
{
return $this->displayBlanksAs;
}
public function setDisplayBlanksAs($displayBlanksAs)
{
$this->displayBlanksAs = $displayBlanksAs;
return $this;
}
public function getChartAxisY()
{
if ($this->yAxis !== null) {
return $this->yAxis;
}
return new Axis();
}
public function getChartAxisX()
{
if ($this->xAxis !== null) {
return $this->xAxis;
}
return new Axis();
}
public function getMajorGridlines()
{
if ($this->majorGridlines !== null) {
return $this->majorGridlines;
}
return new GridLines();
}
public function getMinorGridlines()
{
if ($this->minorGridlines !== null) {
return $this->minorGridlines;
}
return new GridLines();
}
public function setTopLeftPosition($cell, $xOffset = null, $yOffset = null)
{
$this->topLeftCellRef = $cell;
if ($xOffset !== null) {
$this->setTopLeftXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setTopLeftYOffset($yOffset);
}
return $this;
}
public function getTopLeftPosition()
{
return [
'cell' => $this->topLeftCellRef,
'xOffset' => $this->topLeftXOffset,
'yOffset' => $this->topLeftYOffset,
];
}
public function getTopLeftCell()
{
return $this->topLeftCellRef;
}
public function setTopLeftCell($cell)
{
$this->topLeftCellRef = $cell;
return $this;
}
public function setTopLeftOffset($xOffset, $yOffset)
{
if ($xOffset !== null) {
$this->setTopLeftXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setTopLeftYOffset($yOffset);
}
return $this;
}
public function getTopLeftOffset()
{
return [
'X' => $this->topLeftXOffset,
'Y' => $this->topLeftYOffset,
];
}
public function setTopLeftXOffset($xOffset)
{
$this->topLeftXOffset = $xOffset;
return $this;
}
public function getTopLeftXOffset()
{
return $this->topLeftXOffset;
}
public function setTopLeftYOffset($yOffset)
{
$this->topLeftYOffset = $yOffset;
return $this;
}
public function getTopLeftYOffset()
{
return $this->topLeftYOffset;
}
public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
{
$this->bottomRightCellRef = $cell;
if ($xOffset !== null) {
$this->setBottomRightXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setBottomRightYOffset($yOffset);
}
return $this;
}
public function getBottomRightPosition()
{
return [
'cell' => $this->bottomRightCellRef,
'xOffset' => $this->bottomRightXOffset,
'yOffset' => $this->bottomRightYOffset,
];
}
public function setBottomRightCell($cell)
{
$this->bottomRightCellRef = $cell;
return $this;
}
public function getBottomRightCell()
{
return $this->bottomRightCellRef;
}
public function setBottomRightOffset($xOffset, $yOffset)
{
if ($xOffset !== null) {
$this->setBottomRightXOffset($xOffset);
}
if ($yOffset !== null) {
$this->setBottomRightYOffset($yOffset);
}
return $this;
}
public function getBottomRightOffset()
{
return [
'X' => $this->bottomRightXOffset,
'Y' => $this->bottomRightYOffset,
];
}
public function setBottomRightXOffset($xOffset)
{
$this->bottomRightXOffset = $xOffset;
return $this;
}
public function getBottomRightXOffset()
{
return $this->bottomRightXOffset;
}
public function setBottomRightYOffset($yOffset)
{
$this->bottomRightYOffset = $yOffset;
return $this;
}
public function getBottomRightYOffset()
{
return $this->bottomRightYOffset;
}
public function refresh(): void
{
if ($this->worksheet !== null) {
$this->plotArea->refresh($this->worksheet);
}
}
public function render($outputDestination = null)
{
if ($outputDestination == 'php://output') {
$outputDestination = null;
}
$libraryName = Settings::getChartRenderer();
if ($libraryName === null) {
return false;
}
$this->refresh();
$renderer = new $libraryName($this);
return $renderer->render($outputDestination);
}
}