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