From bfc34cc6ef7d202ca61362fab85f9c3d963560bd Mon Sep 17 00:00:00 2001
From: oleibman <10341515+oleibman@users.noreply.github.com>
Date: Sun, 26 Feb 2023 13:37:03 -0800
Subject: [PATCH] Misplaced Xml Writing Chart Label FillColor
Fix #3397. Move code to correct location.
---
src/PhpSpreadsheet/Writer/Xlsx/Chart.php | 14 +-
.../Chart/Issue3397Test.php | 182 ++++++++++++++++++
2 files changed, 188 insertions(+), 8 deletions(-)
create mode 100644 tests/PhpSpreadsheetTests/Chart/Issue3397Test.php
diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php
index 6465904ee8..e77233668d 100644
--- a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php
+++ b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php
@@ -1055,14 +1055,6 @@ private function writePlotGroup(?DataSeries $plotGroup, string $groupType, XMLWr
$labelFill = $plotLabel->getFillColorObject();
$labelFill = ($labelFill instanceof ChartColor) ? $labelFill : null;
}
- if ($plotLabel && $groupType !== DataSeries::TYPE_LINECHART) {
- $fillColor = $plotLabel->getFillColorObject();
- if ($fillColor !== null && !is_array($fillColor) && $fillColor->isUsable()) {
- $objWriter->startElement('c:spPr');
- $this->writeColor($objWriter, $fillColor);
- $objWriter->endElement(); // c:spPr
- }
- }
// Values
$plotSeriesValues = $plotGroup->getPlotValuesByIndex($plotSeriesIdx);
@@ -1094,6 +1086,12 @@ private function writePlotGroup(?DataSeries $plotGroup, string $groupType, XMLWr
$plotSeriesValues !== false
) {
$objWriter->startElement('c:spPr');
+ if ($plotLabel && $groupType !== DataSeries::TYPE_LINECHART) {
+ $fillColor = $plotLabel->getFillColorObject();
+ if ($fillColor !== null && !is_array($fillColor) && $fillColor->isUsable()) {
+ $this->writeColor($objWriter, $fillColor);
+ }
+ }
$fillObject = $labelFill ?? $plotSeriesValues->getFillColorObject();
$callLineStyles = true;
if ($fillObject instanceof ChartColor && $fillObject->isUsable()) {
diff --git a/tests/PhpSpreadsheetTests/Chart/Issue3397Test.php b/tests/PhpSpreadsheetTests/Chart/Issue3397Test.php
new file mode 100644
index 0000000000..81991c9ff8
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Chart/Issue3397Test.php
@@ -0,0 +1,182 @@
+getActiveSheet();
+ $dataSheet->setTitle('Summary_report');
+ $label1 = 'Before 10 a.m.';
+ $label2 = 'Between 10 a.m. and 2 p.m.';
+ $label3 = 'After 2 p.m.';
+ $dataSheet->getCell('D8')->setValue($label1);
+ $dataSheet->getCell('D9')->setValue($label2);
+ $dataSheet->getCell('D10')->setValue($label3);
+
+ $dataSheet->getCell('E7')->setValue(100);
+ $dataSheet->getCell('E8')->setValue(101);
+ $dataSheet->getCell('E9')->setValue(102);
+ $dataSheet->getCell('E10')->setValue(103);
+ $dataSheet->getCell('F7')->setValue(200);
+ $dataSheet->getCell('F8')->setValue(201);
+ $dataSheet->getCell('F9')->setValue(202);
+ $dataSheet->getCell('F10')->setValue(203);
+ $dataSheet->getCell('G7')->setValue(300);
+ $dataSheet->getCell('G8')->setValue(301);
+ $dataSheet->getCell('G9')->setValue(302);
+ $dataSheet->getCell('G10')->setValue(303);
+ $dataSheet->getCell('H7')->setValue(400);
+ $dataSheet->getCell('H8')->setValue(401);
+ $dataSheet->getCell('H9')->setValue(402);
+ $dataSheet->getCell('H10')->setValue(403);
+ $dataSheet->getCell('I7')->setValue(500);
+ $dataSheet->getCell('I8')->setValue(501);
+ $dataSheet->getCell('I9')->setValue(502);
+ $dataSheet->getCell('I10')->setValue(503);
+ $dataSheet->getCell('J7')->setValue(600);
+ $dataSheet->getCell('J8')->setValue(601);
+ $dataSheet->getCell('J9')->setValue(602);
+ $dataSheet->getCell('J10')->setValue(603);
+
+ $sheet = $spreadsheet->createSheet();
+ $sheet->setTitle('Chart');
+
+ $col = 'J';
+ $colNumber = 7;
+
+ $dataSeriesLabels = [
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_STRING, // label is string
+ 'Summary_report!$D$8', // data source
+ null, // format code
+ 1, // point count
+ null, // label values come from data source
+ null, // marker
+ 'ff0000' // rgb fill color
+ ),
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_STRING, // label is string
+ 'Summary_report!$D$9', // data source
+ null, // format code
+ 1, // point count
+ null, // label values come from data source
+ null, // marker
+ '70ad47' // rgb fill color
+ ),
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_STRING, // label is string
+ 'Summary_report!$D$10', // data source
+ null, // format code
+ 1, // point count
+ null, // label values come from data source
+ null, // marker
+ 'ffff00' // rgb fill color
+ ),
+ ];
+
+ $xAxisTickValues = [
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_STRING,
+ 'Summary_report!$E$7:$' . $col . '$7',
+ null,
+ $colNumber
+ ),
+ ];
+ $dataSeriesValues = [
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_NUMBER,
+ 'Summary_report!$E$8:$' . $col . '$8',
+ null,
+ $colNumber
+ ),
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_NUMBER,
+ 'Summary_report!$E$9:$' . $col . '$9',
+ null,
+ $colNumber
+ ),
+ new DataSeriesValues(
+ DataSeriesValues::DATASERIES_TYPE_NUMBER,
+ 'Summary_report!$E$10:$' . $col . '$10',
+ null,
+ $colNumber
+ ),
+ ];
+ $series = new DataSeries(
+ DataSeries::TYPE_BARCHART, // plotType
+ DataSeries::GROUPING_STACKED, // plotGrouping
+ range(0, count($dataSeriesValues) - 1), // plotOrder
+ $dataSeriesLabels, // plotLabel
+ $xAxisTickValues, // plotCategory
+ $dataSeriesValues // plotValues
+ );
+ $layout1 = new Layout();
+ $layout1->setShowVal(true);
+ $plotArea = new PlotArea($layout1, [$series]);
+
+ $legend = new Legend(Legend::POSITION_BOTTOM, null, false);
+ $title = new Title('Submission Report');
+ $yAxisLabel = new Title('Count');
+ $xAxisLabel = new Title('period');
+ $yaxis = new Axis();
+ $yaxis->setAxisOptionsProperties('low');
+
+ // Create the chart
+ $chart = new Chart(
+ 'chart1', // name
+ $title, // title
+ $legend, // legend
+ $plotArea, // plotArea
+ true, // plotVisibleOnly
+ 'gap', // displayBlanksAs
+ $xAxisLabel, // xAxisLabel
+ $yAxisLabel, // yAxisLabel
+ null,
+ $yaxis
+ );
+
+ // Set the position where the chart should appear in the worksheet
+ $chart->setTopLeftPosition('A7');
+ $chart->setBottomRightPosition('H20');
+
+ // Add the chart to the worksheet
+ $sheet->addChart($chart);
+
+ $writer = new Xlsx($spreadsheet);
+ $writer->setIncludeCharts(true);
+ $outputFilename = File::temporaryFilename();
+ $writer->save($outputFilename);
+ $spreadsheet->disconnectWorksheets();
+
+ $file = 'zip://';
+ $file .= $outputFilename;
+ $file .= '#xl/charts/chart1.xml';
+ $data = file_get_contents($file);
+ unlink($outputFilename);
+ // confirm that file contains expected namespaced xml tag
+ if ($data === false) {
+ self::fail('Unable to read file');
+ } else {
+ self::assertStringContainsString('Summary_report!$D$8Before 10 a.m.', $data);
+ self::assertStringContainsString('Summary_report!$D$9Between 10 a.m. and 2 p.m.', $data);
+ self::assertStringContainsString('Summary_report!$D$10After 2 p.m.', $data);
+ }
+ }
+}