PieFillTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Reader\Xlsx as XlsxReader;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  13. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  14. class PieFillTest extends AbstractFunctional
  15. {
  16. public function readCharts(XlsxReader $reader): void
  17. {
  18. $reader->setIncludeCharts(true);
  19. }
  20. public function writeCharts(XlsxWriter $writer): void
  21. {
  22. $writer->setIncludeCharts(true);
  23. }
  24. public function testPieFill(): void
  25. {
  26. $spreadsheet = new Spreadsheet();
  27. $worksheet = $spreadsheet->getActiveSheet();
  28. $worksheet->fromArray(
  29. [
  30. ['', 2010, 2011, 2012],
  31. ['Q1', 12, 15, 21],
  32. ['Q2', 56, 73, 86],
  33. ['Q3', 52, 61, 69],
  34. ['Q4', 30, 32, 0],
  35. ]
  36. );
  37. // Custom colors for dataSeries (gray, blue, red, orange)
  38. $colors = [
  39. 'cccccc',
  40. '*accent1', // use schemeClr, was '00abb8',
  41. '/green', // use prstClr, was 'b8292f',
  42. 'eb8500',
  43. ];
  44. // Set the Labels for each data series we want to plot
  45. // Datatype
  46. // Cell reference for data
  47. // Format Code
  48. // Number of datapoints in series
  49. // Data values
  50. // Data Marker
  51. $dataSeriesLabels1 = [
  52. new DataSeriesValues(
  53. DataSeriesValues::DATASERIES_TYPE_STRING,
  54. 'Worksheet!$C$1',
  55. null,
  56. 1
  57. ), // 2011
  58. ];
  59. // Set the X-Axis Labels
  60. // Datatype
  61. // Cell reference for data
  62. // Format Code
  63. // Number of datapoints in series
  64. // Data values
  65. // Data Marker
  66. $xAxisTickValues1 = [
  67. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  68. ];
  69. // Set the Data values for each data series we want to plot
  70. // Datatype
  71. // Cell reference for data
  72. // Format Code
  73. // Number of datapoints in series
  74. // Data values
  75. // Data Marker
  76. // Custom Colors
  77. $dataSeriesValues1Element = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4);
  78. $dataSeriesValues1Element->setFillColor($colors);
  79. $dataSeriesValues1 = [$dataSeriesValues1Element];
  80. // Build the dataseries
  81. $series1 = new DataSeries(
  82. DataSeries::TYPE_PIECHART, // plotType
  83. null, // plotGrouping (Pie charts don't have any grouping)
  84. range(0, count($dataSeriesValues1) - 1), // plotOrder
  85. $dataSeriesLabels1, // plotLabel
  86. $xAxisTickValues1, // plotCategory
  87. $dataSeriesValues1 // plotValues
  88. );
  89. // Set up a layout object for the Pie chart
  90. $layout1 = new Layout();
  91. $layout1->setShowVal(true);
  92. $layout1->setShowPercent(true);
  93. // Set the series in the plot area
  94. $plotArea1 = new PlotArea($layout1, [$series1]);
  95. // Set the chart legend
  96. $legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
  97. $title1 = new Title('Test Pie Chart');
  98. // Create the chart
  99. $chart1 = new Chart(
  100. 'chart1', // name
  101. $title1, // title
  102. $legend1, // legend
  103. $plotArea1, // plotArea
  104. true, // plotVisibleOnly
  105. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  106. null, // xAxisLabel
  107. null // no Y-Axis for Pie Chart
  108. );
  109. // Set the position where the chart should appear in the worksheet
  110. $chart1->setTopLeftPosition('A7');
  111. $chart1->setBottomRightPosition('H20');
  112. // Add the chart to the worksheet
  113. $worksheet->addChart($chart1);
  114. /** @var callable */
  115. $callableReader = [$this, 'readCharts'];
  116. /** @var callable */
  117. $callableWriter = [$this, 'writeCharts'];
  118. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  119. $spreadsheet->disconnectWorksheets();
  120. $sheet = $reloadedSpreadsheet->getActiveSheet();
  121. $charts2 = $sheet->getChartCollection();
  122. self::assertCount(1, $charts2);
  123. $chart2 = $charts2[0];
  124. self::assertNotNull($chart2);
  125. $plotArea2 = $chart2->getPlotArea();
  126. self::assertNotNull($plotArea2);
  127. $dataSeries2 = $plotArea2->getPlotGroup();
  128. self::assertCount(1, $dataSeries2);
  129. $plotValues = $dataSeries2[0]->getPlotValues();
  130. self::assertCount(1, $plotValues);
  131. $fillColors = $plotValues[0]->getFillColor();
  132. self::assertSame($colors, $fillColors);
  133. $writer = new XlsxWriter($reloadedSpreadsheet);
  134. $writer->setIncludeCharts(true);
  135. $writerChart = new XlsxWriter\Chart($writer);
  136. $data = $writerChart->writeChart($chart2);
  137. self::assertSame(1, substr_count($data, '<a:srgbClr val="cccccc"/>'));
  138. self::assertSame(1, substr_count($data, '<a:schemeClr val="accent1"/>'));
  139. self::assertSame(1, substr_count($data, '<a:prstClr val="green"/>'));
  140. self::assertSame(1, substr_count($data, '<a:srgbClr val="eb8500"/>'));
  141. self::assertSame(4, substr_count($data, '<c:dPt>'));
  142. $reloadedSpreadsheet->disconnectWorksheets();
  143. }
  144. }