Skip to content

Commit 7a1370c

Browse files
committed
Allow Omitting Chart Border
Fix #562.
1 parent 88c517f commit 7a1370c

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

samples/Chart33a/33_Chart_create_area.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
$chart->setTopLeftPosition('A7');
9292
$chart->setBottomRightPosition('H20');
9393

94+
$chart->setNoBorder(true);
95+
9496
// Add the chart to the worksheet
9597
$worksheet->addChart($chart);
9698

src/PhpSpreadsheet/Chart/Chart.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class Chart
106106

107107
private bool $noFill = false;
108108

109+
private bool $noBorder = false;
110+
109111
private bool $roundedCorners = false;
110112

111113
private GridLines $borderLines;
@@ -696,6 +698,18 @@ public function setNoFill(bool $noFill): self
696698
return $this;
697699
}
698700

701+
public function getNoBorder(): bool
702+
{
703+
return $this->noBorder;
704+
}
705+
706+
public function setNoBorder(bool $noBorder): self
707+
{
708+
$this->noBorder = $noBorder;
709+
710+
return $this;
711+
}
712+
699713
public function getRoundedCorners(): bool
700714
{
701715
return $this->roundedCorners;

src/PhpSpreadsheet/Reader/Xlsx/Chart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function readChart(SimpleXMLElement $chartElements, string $chartName): \
9595
$gapWidth = null;
9696
$useUpBars = null;
9797
$useDownBars = null;
98+
$noBorder = false;
9899
foreach ($chartElementsC as $chartElementKey => $chartElement) {
99100
switch ($chartElementKey) {
100101
case 'spPr':
@@ -108,6 +109,9 @@ public function readChart(SimpleXMLElement $chartElements, string $chartName): \
108109
if (isset($children->ln)) {
109110
$chartBorderLines = new GridLines();
110111
$this->readLineStyle($chartElementsC, $chartBorderLines);
112+
if (isset($children->ln->noFill)) {
113+
$noBorder = true;
114+
}
111115
}
112116

113117
break;
@@ -470,6 +474,7 @@ public function readChart(SimpleXMLElement $chartElements, string $chartName): \
470474
if ($chartBorderLines !== null) {
471475
$chart->setBorderLines($chartBorderLines);
472476
}
477+
$chart->setNoBorder($noBorder);
473478
$chart->setRoundedCorners($roundedCorners);
474479
if (is_bool($autoTitleDeleted)) {
475480
$chart->setAutoTitleDeleted($autoTitleDeleted);

src/PhpSpreadsheet/Writer/Xlsx/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function writeChart(\PhpOffice\PhpSpreadsheet\Chart\Chart $chart, bool $c
118118
$this->writeColor($objWriter, $fillColor);
119119
}
120120
$borderLines = $chart->getBorderLines();
121-
$this->writeLineStyles($objWriter, $borderLines);
121+
$this->writeLineStyles($objWriter, $borderLines, $chart->getNoBorder());
122122
$this->writeEffects($objWriter, $borderLines);
123123
$objWriter->endElement(); // c:spPr
124124

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Chart;
6+
7+
use PhpOffice\PhpSpreadsheet\Chart\Chart;
8+
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
9+
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
10+
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
11+
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
12+
use PhpOffice\PhpSpreadsheet\Chart\Title;
13+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
14+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
15+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
16+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
17+
18+
class Issue562Test extends AbstractFunctional
19+
{
20+
// based on 33_Char_create_area
21+
// test for chart borders
22+
public function readCharts(XlsxReader $reader): void
23+
{
24+
$reader->setIncludeCharts(true);
25+
}
26+
27+
public function writeCharts(XlsxWriter $writer): void
28+
{
29+
$writer->setIncludeCharts(true);
30+
}
31+
32+
/**
33+
* @dataProvider providerNoBorder
34+
*/
35+
public function testNoBorder(?bool $noBorder, bool $expectedResult): void
36+
{
37+
$spreadsheet = new Spreadsheet();
38+
$worksheet = $spreadsheet->getActiveSheet();
39+
$worksheet->fromArray(
40+
[
41+
['', 2010, 2011, 2012],
42+
['Q1', 12, 15, 21],
43+
['Q2', 56, 73, 86],
44+
['Q3', 52, 61, 69],
45+
['Q4', 30, 32, 0],
46+
]
47+
);
48+
49+
$dataSeriesLabels = [
50+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
51+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
52+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
53+
];
54+
55+
$xAxisTickValues = [
56+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
57+
];
58+
59+
$dataSeriesValues = [
60+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
61+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
62+
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
63+
];
64+
65+
// Build the dataseries
66+
$series = new DataSeries(
67+
DataSeries::TYPE_AREACHART, // plotType
68+
DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
69+
range(0, count($dataSeriesValues) - 1), // plotOrder
70+
$dataSeriesLabels, // plotLabel
71+
$xAxisTickValues, // plotCategory
72+
$dataSeriesValues // plotValues
73+
);
74+
75+
$plotArea = new PlotArea(null, [$series]);
76+
$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
77+
78+
$title = new Title('Test %age-Stacked Area Chart');
79+
$yAxisLabel = new Title('Value ($k)');
80+
81+
$chart = new Chart(
82+
'chart1', // name
83+
$title, // title
84+
$legend, // legend
85+
$plotArea, // plotArea
86+
true, // plotVisibleOnly
87+
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
88+
null, // xAxisLabel
89+
$yAxisLabel // yAxisLabel
90+
);
91+
92+
// Set the position where the chart should appear in the worksheet
93+
$chart->setTopLeftPosition('A7');
94+
$chart->setBottomRightPosition('H20');
95+
96+
if ($noBorder !== null) {
97+
$chart->setNoBorder($noBorder);
98+
}
99+
100+
// Add the chart to the worksheet
101+
$worksheet->addChart($chart);
102+
103+
/** @var callable */
104+
$callableReader = [$this, 'readCharts'];
105+
/** @var callable */
106+
$callableWriter = [$this, 'writeCharts'];
107+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
108+
$spreadsheet->disconnectWorksheets();
109+
110+
$sheet = $reloadedSpreadsheet->getActiveSheet();
111+
$charts2 = $sheet->getChartCollection();
112+
self::assertCount(1, $charts2);
113+
$chart2 = $charts2[0];
114+
self::assertNotNull($chart2);
115+
self::assertSame($expectedResult, $chart2->getNoBorder());
116+
117+
$reloadedSpreadsheet->disconnectWorksheets();
118+
}
119+
120+
public static function providerNoBorder(): array
121+
{
122+
return [
123+
[true, true],
124+
[false, false],
125+
[null, false],
126+
];
127+
}
128+
}

0 commit comments

Comments
 (0)