Skip to content

Commit

Permalink
Issue PHPOffice#3783 taking graph size from chart's PlotArea instead …
Browse files Browse the repository at this point in the history
…of using hard-coded values
  • Loading branch information
f1mishutka committed Nov 8, 2023
1 parent ef3890a commit 5b6501e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/PhpSpreadsheet/Chart/Renderer/JpGraphRendererBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
*/
abstract class JpGraphRendererBase implements IRenderer
{
private static $width = 640;

private static $height = 480;
private const DEFAULT_WIDTH = 640;
private const DEFAULT_HEIGHT = 480;

private static $colourSet = [
'mediumpurple1', 'palegreen3', 'gold1', 'cadetblue1',
Expand Down Expand Up @@ -75,6 +74,16 @@ public function __construct(Chart $chart)
*/
abstract protected static function init(): void;

private function getGraphWidth(): ?float
{
return $this->chart->getPlotArea()?->getLayout()?->getWidth() ?: self::DEFAULT_WIDTH;
}

private function getGraphHeight(): ?float
{
return $this->chart->getPlotArea()?->getLayout()?->getHeight() ?: self::DEFAULT_HEIGHT;
}

private function formatPointMarker($seriesPlot, $markerID)
{
$plotMarkKeys = array_keys(self::$markSet);
Expand Down Expand Up @@ -221,7 +230,7 @@ private function renderLegend(): void

private function renderCartesianPlotArea(string $type = 'textlin'): void
{
$this->graph = new Graph(self::$width, self::$height);
$this->graph = new Graph($this->getGraphWidth(), $this->getGraphHeight());
$this->graph->SetScale($type);

$this->renderTitle();
Expand Down Expand Up @@ -258,14 +267,14 @@ private function renderCartesianPlotArea(string $type = 'textlin'): void

private function renderPiePlotArea(): void
{
$this->graph = new PieGraph(self::$width, self::$height);
$this->graph = new PieGraph($this->getGraphWidth(), $this->getGraphHeight());

$this->renderTitle();
}

private function renderRadarPlotArea(): void
{
$this->graph = new RadarGraph(self::$width, self::$height);
$this->graph = new RadarGraph($this->getGraphWidth(), $this->getGraphHeight());
$this->graph->SetScale('lin');

$this->renderTitle();
Expand Down Expand Up @@ -468,7 +477,7 @@ private function renderPlotScatter(int $groupID, bool $bubble): void
$seriesPlot->link->SetColor(self::$colourSet[self::$plotColour]);
} elseif ($scatterStyle == 'smoothMarker') {
$spline = new Spline($dataValuesY, $dataValuesX);
[$splineDataY, $splineDataX] = $spline->Get(count($dataValuesX) * self::$width / 20);
[$splineDataY, $splineDataX] = $spline->Get(count($dataValuesX) * $this->getGraphWidth() / 20);
$lplot = new LinePlot($splineDataX, $splineDataY);
$lplot->SetColor(self::$colourSet[self::$plotColour]);

Expand Down

0 comments on commit 5b6501e

Please sign in to comment.