Issue2077Test.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  5. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  6. use PhpOffice\PhpSpreadsheet\Chart\Layout;
  7. use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
  8. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  9. use PhpOffice\PhpSpreadsheet\Chart\Title;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  13. use PHPUnit\Framework\TestCase;
  14. class Issue2077Test extends TestCase
  15. {
  16. public function testPercentLabels(): void
  17. {
  18. $spreadsheet = new Spreadsheet();
  19. $worksheet = $spreadsheet->getActiveSheet();
  20. $worksheet->fromArray(
  21. [
  22. ['', '2010', '2011', '2012'],
  23. ['Q1', 12, 15, 21],
  24. ['Q2', 56, 73, 86],
  25. ['Q3', 52, 61, 69],
  26. ['Q4', 30, 32, 60],
  27. ]
  28. );
  29. // Set the Labels for each data series we want to plot
  30. $dataSeriesLabels1 = [
  31. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2011
  32. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2012
  33. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2013
  34. ];
  35. // Set the X-Axis Labels
  36. $xAxisTickValues1 = [
  37. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  38. ];
  39. // Set the Data values for each data series we want to plot
  40. // TODO I think the third parameter can be set,but I didn't succeed
  41. $dataSeriesValues1 = [
  42. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  43. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', NumberFormat::FORMAT_NUMBER_00, 4),
  44. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', NumberFormat::FORMAT_PERCENTAGE_00, 4),
  45. ];
  46. // Build the dataseries
  47. $series1 = [
  48. new DataSeries(
  49. DataSeries::TYPE_PIECHART, // plotType
  50. null, // plotGrouping (Pie charts don't have any grouping)
  51. range(0, count($dataSeriesValues1) - 1), // plotOrder
  52. $dataSeriesLabels1, // plotLabel
  53. $xAxisTickValues1, // plotCategory
  54. $dataSeriesValues1 // plotValues
  55. ),
  56. ];
  57. // Set up a layout object for the Pie chart
  58. $layout1 = new Layout();
  59. $layout1->setShowVal(true);
  60. // Set the layout to show percentage with 2 decimal points
  61. $layout1->setShowPercent(true);
  62. $layout1->setNumFmtCode(NumberFormat::FORMAT_PERCENTAGE_00);
  63. // Set the series in the plot area
  64. $plotArea1 = new PlotArea($layout1, $series1);
  65. // Set the chart legend
  66. $legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
  67. $title1 = new Title('Test Pie Chart');
  68. $yAxisLabel = new Title('Value ($k)');
  69. // Create the chart
  70. $chart1 = new Chart(
  71. 'chart1', // name
  72. $title1, // title
  73. $legend1, // legend
  74. $plotArea1, // plotArea
  75. true, // plotVisibleOnly
  76. 'gap', // displayBlanksAs
  77. null, // xAxisLabel
  78. $yAxisLabel
  79. );
  80. // Set the position where the chart should appear in the worksheet
  81. $chart1->setTopLeftPosition('A7');
  82. $chart1->setBottomRightPosition('H20');
  83. // Add the chart to the worksheet
  84. $worksheet->addChart($chart1);
  85. $writer = new XlsxWriter($spreadsheet);
  86. $writer->setIncludeCharts(true);
  87. $writerChart = new XlsxWriter\Chart($writer);
  88. $data = $writerChart->writeChart($chart1);
  89. self::assertStringContainsString('<c:dLbls><c:numFmt formatCode="0.00%" sourceLinked="0"/><c:showVal val="1"/><c:showPercent val="1"/></c:dLbls>', $data);
  90. $spreadsheet->disconnectWorksheets();
  91. }
  92. }