|
| 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\Layout; |
| 11 | +use PhpOffice\PhpSpreadsheet\Chart\PlotArea; |
| 12 | +use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; |
| 13 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 14 | +use PhpOffice\PhpSpreadsheet\Style\Font; |
| 15 | +use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; |
| 16 | +use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
| 17 | + |
| 18 | +class Issue4201Test extends AbstractFunctional |
| 19 | +{ |
| 20 | + public function readCharts(XlsxReader $reader): void |
| 21 | + { |
| 22 | + $reader->setIncludeCharts(true); |
| 23 | + } |
| 24 | + |
| 25 | + public function writeCharts(XlsxWriter $writer): void |
| 26 | + { |
| 27 | + $writer->setIncludeCharts(true); |
| 28 | + } |
| 29 | + |
| 30 | + public function testLabelFont(): void |
| 31 | + { |
| 32 | + $spreadsheet = new Spreadsheet(); |
| 33 | + $worksheet = $spreadsheet->getActiveSheet(); |
| 34 | + |
| 35 | + // Sample data for pie chart |
| 36 | + $data = [ |
| 37 | + ['Category', 'Value'], |
| 38 | + ['Category A', 40], |
| 39 | + ['Category B', 30], |
| 40 | + ['Category C', 20], |
| 41 | + ['Category D', 10], |
| 42 | + ]; |
| 43 | + $worksheet->fromArray($data, null, 'A1'); |
| 44 | + $worksheet->getColumnDimension('A')->setAutoSize(true); |
| 45 | + |
| 46 | + // Create data series for the pie chart |
| 47 | + $categories = [new DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4)]; |
| 48 | + $values = [new DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4)]; |
| 49 | + |
| 50 | + // Set layout for data labels |
| 51 | + $font = new Font(); |
| 52 | + $font->setName('Times New Roman'); |
| 53 | + $font->setSize(8); |
| 54 | + $layout = new Layout(); |
| 55 | + $layout->setShowVal(true); // Display values |
| 56 | + $layout->setShowCatName(true); // Display category names |
| 57 | + $layout->setLabelFont($font); |
| 58 | + |
| 59 | + $series = new DataSeries( |
| 60 | + DataSeries::TYPE_PIECHART, // Chart type: Pie chart |
| 61 | + null, |
| 62 | + range(0, count($values) - 1), |
| 63 | + [], |
| 64 | + $categories, |
| 65 | + $values |
| 66 | + ); |
| 67 | + |
| 68 | + $plotArea = new PlotArea($layout, [$series]); |
| 69 | + $chart = new Chart('Pie Chart', null, null, $plotArea); |
| 70 | + $chart->setTopLeftPosition('A7'); |
| 71 | + $chart->setBottomRightPosition('H20'); |
| 72 | + $worksheet->addChart($chart); |
| 73 | + |
| 74 | + /** @var callable */ |
| 75 | + $callableReader = [$this, 'readCharts']; |
| 76 | + /** @var callable */ |
| 77 | + $callableWriter = [$this, 'writeCharts']; |
| 78 | + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter); |
| 79 | + $spreadsheet->disconnectWorksheets(); |
| 80 | + |
| 81 | + $sheet = $reloadedSpreadsheet->getActiveSheet(); |
| 82 | + $charts = $sheet->getChartCollection(); |
| 83 | + self::assertCount(1, $charts); |
| 84 | + $chart2 = $charts[0]; |
| 85 | + self::assertNotNull($chart2); |
| 86 | + $plotArea2 = $chart2->getPlotArea(); |
| 87 | + self::assertNotNull($plotArea2); |
| 88 | + $layout2 = $plotArea2->getLayout(); |
| 89 | + self::assertNotNull($layout2); |
| 90 | + $font2 = $layout2->getLabelFont(); |
| 91 | + self::assertNotNull($font2); |
| 92 | + self::assertSame('Times New Roman', $font2->getLatin()); |
| 93 | + self::assertSame(8.0, $font2->getSize()); |
| 94 | + |
| 95 | + $reloadedSpreadsheet->disconnectWorksheets(); |
| 96 | + } |
| 97 | +} |
0 commit comments