diff --git a/CHANGELOG.md b/CHANGELOG.md index 35be7d1298..3e1e07d2a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - MATCH Function Problems with Int/Float Compare and Wildcards [Issue #3141](https://github.com/PHPOffice/PhpSpreadsheet/issues/3141) [PR #3142](https://github.com/PHPOffice/PhpSpreadsheet/pull/3142) - Fix ODS Read Filter on number-columns-repeated cell [Issue #3148](https://github.com/PHPOffice/PhpSpreadsheet/issues/3148) [PR #3149](https://github.com/PHPOffice/PhpSpreadsheet/pull/3149) - Problems Formatting Very Small and Very Large Numbers [Issue #3128](https://github.com/PHPOffice/PhpSpreadsheet/issues/3128) [PR #3152](https://github.com/PHPOffice/PhpSpreadsheet/pull/3152) +- XlsxWrite does now preserve line styles for y-axis, not only x-axis [PR #3163](https://github.com/PHPOffice/PhpSpreadsheet/pull/3163) ## 1.25.2 - 2022-09-25 diff --git a/src/PhpSpreadsheet/Reader/Xlsx/Chart.php b/src/PhpSpreadsheet/Reader/Xlsx/Chart.php index c22334cac9..93e0c5d98c 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/Chart.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/Chart.php @@ -150,6 +150,7 @@ public function readChart(SimpleXMLElement $chartElements, $chartName) } $xAxis->setAxisType($chartDetailKey); $this->readEffects($chartDetail, $xAxis); + $this->readLineStyle($chartDetail, $xAxis); if (isset($chartDetail->spPr)) { $sppr = $chartDetail->spPr->children($this->aNamespace); if (isset($sppr->solidFill)) { @@ -219,6 +220,7 @@ public function readChart(SimpleXMLElement $chartElements, $chartName) } } $this->readEffects($chartDetail, $whichAxis); + $this->readLineStyle($chartDetail, $whichAxis); if ($whichAxis !== null && isset($chartDetail->spPr)) { $sppr = $chartDetail->spPr->children($this->aNamespace); if (isset($sppr->solidFill)) { diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php index 4cad791c95..7d76823e27 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php @@ -626,6 +626,7 @@ private function writeCategoryAxis(XMLWriter $objWriter, ?Title $xAxisLabel, $id $objWriter->startElement('c:spPr'); $this->writeColor($objWriter, $yAxis->getFillColorObject()); + $this->writeLineStyles($objWriter, $yAxis); $this->writeEffects($objWriter, $yAxis); $objWriter->endElement(); // spPr diff --git a/tests/PhpSpreadsheetTests/Chart/PR3163Test.php b/tests/PhpSpreadsheetTests/Chart/PR3163Test.php new file mode 100644 index 0000000000..154bc18da8 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Chart/PR3163Test.php @@ -0,0 +1,155 @@ +setIncludeCharts(true); + } + + public function writeCharts(XlsxWriter $writer): void + { + $writer->setIncludeCharts(true); + } + + public function testYAxisLineStyle(): void + { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + $worksheet->fromArray( + [ + ['', 2010, 2011, 2012], + ['Q1', 12, 15, 21], + ['Q2', 56, 73, 86], + ['Q3', 52, 61, 69], + ['Q4', 30, 32, 0], + ] + ); + + // Set the Labels for each data series we want to plot + // Datatype + // Cell reference for data + // Format Code + // Number of datapoints in series + // Data values + // Data Marker + $dataSeriesLabels = [ + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010 + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012 + ]; + // Set the X-Axis Labels + // Datatype + // Cell reference for data + // Format Code + // Number of datapoints in series + // Data values + // Data Marker + $xAxisTickValues = [ + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 + ]; + // Set the Data values for each data series we want to plot + // Datatype + // Cell reference for data + // Format Code + // Number of datapoints in series + // Data values + // Data Marker + $dataSeriesValues = [ + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4), + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), + ]; + + // Build the dataseries + $series = new DataSeries( + DataSeries::TYPE_BARCHART, // plotType + DataSeries::GROUPING_CLUSTERED, // plotGrouping + range(0, count($dataSeriesValues) - 1), // plotOrder + $dataSeriesLabels, // plotLabel + $xAxisTickValues, // plotCategory + $dataSeriesValues // plotValues + ); + // Set additional dataseries parameters + // Make it a horizontal bar rather than a vertical column graph + $series->setPlotDirection(DataSeries::DIRECTION_BAR); + + // Set the series in the plot area + $plotArea = new PlotArea(null, [$series]); + // Set the chart legend + $legend = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false); + + $title = new Title('Test Bar Chart'); + $yAxisLabel = new Title('Value ($k)'); + + // Create x- and y-axis + $xAxis = new Axis(); + $xAxis->setLineColorProperties('FF0000'); + + $yAxis = new Axis(); + $yAxis->setLineColorProperties('00FF00'); + + // Create the chart + $chart1 = new Chart( + 'chart1', // name + $title, // title + $legend, // legend + $plotArea, // plotArea + true, // plotVisibleOnly + DataSeries::EMPTY_AS_GAP, // displayBlanksAs + null, // xAxisLabel + $yAxisLabel, // yAxisLabel + $xAxis, // xAxis + $yAxis // yAxis + ); + + // Set the position where the chart should appear in the worksheet + $chart1->setTopLeftPosition('A7'); + $chart1->setBottomRightPosition('H20'); + + // Add the chart to the worksheet + $worksheet->addChart($chart1); + + /** @var callable */ + $callableReader = [$this, 'readCharts']; + /** @var callable */ + $callableWriter = [$this, 'writeCharts']; + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter); + $spreadsheet->disconnectWorksheets(); + + $sheet = $reloadedSpreadsheet->getActiveSheet(); + $charts2 = $sheet->getChartCollection(); + self::assertCount(1, $charts2); + $chart2 = $charts2[0]; + self::assertNotNull($chart2); + + $xAxis2 = $chart2->getChartAxisX(); + self::assertNotNull($xAxis2); + $lineColorX2 = $xAxis2->getLineColorProperty('value'); + self::assertSame($lineColorX2, 'FF0000'); + + $yAxis2 = $chart2->getChartAxisY(); + self::assertNotNull($yAxis2); + $lineColorY2 = $yAxis2->getLineColorProperty('value'); + self::assertSame($lineColorY2, '00FF00'); + + $reloadedSpreadsheet->disconnectWorksheets(); + } +}