| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace PhpOffice\PhpSpreadsheetTests\Chart;
- use PhpOffice\PhpSpreadsheet\Chart\Chart;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
- use PhpOffice\PhpSpreadsheet\Chart\Layout;
- use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
- use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
- use PhpOffice\PhpSpreadsheet\Chart\Title;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
- use PHPUnit\Framework\TestCase;
- class Issue2077Test extends TestCase
- {
- public function testPercentLabels(): 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, 60],
- ]
- );
- // Set the Labels for each data series we want to plot
- $dataSeriesLabels1 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2011
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2012
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2013
- ];
- // Set the X-Axis Labels
- $xAxisTickValues1 = [
- 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
- // TODO I think the third parameter can be set,but I didn't succeed
- $dataSeriesValues1 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', NumberFormat::FORMAT_NUMBER_00, 4),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', NumberFormat::FORMAT_PERCENTAGE_00, 4),
- ];
- // Build the dataseries
- $series1 = [
- new DataSeries(
- DataSeries::TYPE_PIECHART, // plotType
- null, // plotGrouping (Pie charts don't have any grouping)
- range(0, count($dataSeriesValues1) - 1), // plotOrder
- $dataSeriesLabels1, // plotLabel
- $xAxisTickValues1, // plotCategory
- $dataSeriesValues1 // plotValues
- ),
- ];
- // Set up a layout object for the Pie chart
- $layout1 = new Layout();
- $layout1->setShowVal(true);
- // Set the layout to show percentage with 2 decimal points
- $layout1->setShowPercent(true);
- $layout1->setNumFmtCode(NumberFormat::FORMAT_PERCENTAGE_00);
- // Set the series in the plot area
- $plotArea1 = new PlotArea($layout1, $series1);
- // Set the chart legend
- $legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
- $title1 = new Title('Test Pie Chart');
- $yAxisLabel = new Title('Value ($k)');
- // Create the chart
- $chart1 = new Chart(
- 'chart1', // name
- $title1, // title
- $legend1, // legend
- $plotArea1, // plotArea
- true, // plotVisibleOnly
- 'gap', // displayBlanksAs
- null, // xAxisLabel
- $yAxisLabel
- );
- // 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);
- $writer = new XlsxWriter($spreadsheet);
- $writer->setIncludeCharts(true);
- $writerChart = new XlsxWriter\Chart($writer);
- $data = $writerChart->writeChart($chart1);
- self::assertStringContainsString('<c:dLbls><c:numFmt formatCode="0.00%" sourceLinked="0"/><c:showVal val="1"/><c:showPercent val="1"/></c:dLbls>', $data);
- $spreadsheet->disconnectWorksheets();
- }
- }
|