Skip to content

Commit ccee869

Browse files
authored
getDataLabel defends against empty labels
1 parent 17fc623 commit ccee869

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/PhpSpreadsheet/Chart/Renderer/JpGraphRendererBase.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ private function renderRadarPlotArea(): void
281281
$this->renderTitle();
282282
}
283283

284+
private function getDataLabel(int $groupId, int $index): mixed
285+
{
286+
$plotLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupId)->getPlotLabelByIndex($index);
287+
if (!$plotLabel) {
288+
return '';
289+
} else {
290+
return $plotLabel->getDataValue();
291+
}
292+
}
293+
284294
private function renderPlotLine(int $groupID, bool $filled = false, bool $combination = false): void
285295
{
286296
$grouping = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotGrouping();
@@ -334,10 +344,8 @@ private function renderPlotLine(int $groupID, bool $filled = false, bool $combin
334344
// Set the appropriate plot marker
335345
$this->formatPointMarker($seriesPlot, $marker);
336346
}
337-
if ($this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($index)) {
338-
$dataLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($index)->getDataValue();
339-
$seriesPlot->SetLegend($dataLabel);
340-
}
347+
348+
$seriesPlot->SetLegend($this->getDataLabel($groupID, $index));
341349

342350
$seriesPlots[] = $seriesPlot;
343351
}
@@ -410,12 +418,8 @@ private function renderPlotBar(int $groupID, ?string $dimensions = '2d'): void
410418
if ($dimensions == '3d') {
411419
$seriesPlot->SetShadow();
412420
}
413-
if (!$this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($j)) {
414-
$dataLabel = '';
415-
} else {
416-
$dataLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($j)->getDataValue();
417-
}
418-
$seriesPlot->SetLegend($dataLabel);
421+
422+
$seriesPlot->SetLegend($this->getDataLabel($groupID, $j));
419423

420424
$seriesPlots[] = $seriesPlot;
421425
}
@@ -494,8 +498,7 @@ private function renderPlotScatter(int $groupID, bool $bubble): void
494498
$marker = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
495499
$this->formatPointMarker($seriesPlot, $marker);
496500
}
497-
$dataLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue();
498-
$seriesPlot->SetLegend($dataLabel);
501+
$seriesPlot->SetLegend($this->getDataLabel($groupID, $i));
499502

500503
$this->graph->Add($seriesPlot);
501504
}
@@ -526,13 +529,12 @@ private function renderPlotRadar(int $groupID): void
526529

527530
$seriesPlot = new RadarPlot(array_reverse($dataValuesX));
528531

529-
$dataLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue();
530532
$seriesPlot->SetColor(self::$colourSet[self::$plotColour++]);
531533
if ($radarStyle == 'filled') {
532534
$seriesPlot->SetFillColor(self::$colourSet[self::$plotColour]);
533535
}
534536
$this->formatPointMarker($seriesPlot, $marker);
535-
$seriesPlot->SetLegend($dataLabel);
537+
$seriesPlot->SetLegend($this->getDataLabel($groupID, $i));
536538

537539
$this->graph->Add($seriesPlot);
538540
}

0 commit comments

Comments
 (0)