Skip to content

Issue #3783 taking graph size from chart's PlotArea instead of using hard-coded values #3785

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

Closed
Closed
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
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