AxisShadowTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  4. use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
  5. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  6. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  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 AxisShadowTest 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 testGlowY(): 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. // Set the Labels for each data series we want to plot
  38. // Datatype
  39. // Cell reference for data
  40. // Format Code
  41. // Number of datapoints in series
  42. // Data values
  43. // Data Marker
  44. $dataSeriesLabels = [
  45. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  46. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  48. ];
  49. // Set the X-Axis Labels
  50. // Datatype
  51. // Cell reference for data
  52. // Format Code
  53. // Number of datapoints in series
  54. // Data values
  55. // Data Marker
  56. $xAxisTickValues = [
  57. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  58. ];
  59. // Set the Data values for each data series we want to plot
  60. // Datatype
  61. // Cell reference for data
  62. // Format Code
  63. // Number of datapoints in series
  64. // Data values
  65. // Data Marker
  66. $dataSeriesValues = [
  67. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  68. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  69. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  70. ];
  71. // Build the dataseries
  72. $series = new DataSeries(
  73. DataSeries::TYPE_AREACHART, // plotType
  74. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  75. range(0, count($dataSeriesValues) - 1), // plotOrder
  76. $dataSeriesLabels, // plotLabel
  77. $xAxisTickValues, // plotCategory
  78. $dataSeriesValues // plotValues
  79. );
  80. // Set the series in the plot area
  81. $plotArea = new PlotArea(null, [$series]);
  82. // Set the chart legend
  83. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  84. $title = new Title('Test %age-Stacked Area Chart');
  85. $yAxisLabel = new Title('Value ($k)');
  86. // Create the chart
  87. $chart = new Chart(
  88. 'chart1', // name
  89. $title, // title
  90. $legend, // legend
  91. $plotArea, // plotArea
  92. true, // plotVisibleOnly
  93. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  94. null, // xAxisLabel
  95. $yAxisLabel // yAxisLabel
  96. );
  97. $yAxis = $chart->getChartAxisY();
  98. $expectedY = [
  99. 'effect' => 'outerShdw',
  100. 'algn' => 'tl',
  101. 'blur' => 5,
  102. 'direction' => 45,
  103. 'distance' => 3,
  104. 'rotWithShape' => 0,
  105. 'color' => [
  106. 'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
  107. 'value' => 'black',
  108. 'alpha' => 40,
  109. ],
  110. ];
  111. foreach ($expectedY as $key => $value) {
  112. $yAxis->setShadowProperty($key, $value);
  113. }
  114. foreach ($expectedY as $key => $value) {
  115. self::assertEquals($value, $yAxis->getShadowProperty($key), $key);
  116. }
  117. $xAxis = $chart->getChartAxisX();
  118. $expectedX = [
  119. 'effect' => 'outerShdw',
  120. 'algn' => 'bl',
  121. 'blur' => 6,
  122. 'direction' => 315,
  123. 'distance' => 3,
  124. 'rotWithShape' => 0,
  125. 'size' => [
  126. 'sx' => null,
  127. 'sy' => 254,
  128. 'kx' => -94,
  129. 'ky' => null,
  130. ],
  131. 'color' => [
  132. 'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
  133. 'value' => 'FF0000',
  134. 'alpha' => 20,
  135. ],
  136. ];
  137. foreach ($expectedX as $key => $value) {
  138. $xAxis->setShadowProperty($key, $value);
  139. }
  140. foreach ($expectedX as $key => $value) {
  141. self::assertEquals($value, $xAxis->getShadowProperty($key), $key);
  142. }
  143. // Set the position where the chart should appear in the worksheet
  144. $chart->setTopLeftPosition('A7');
  145. $chart->setBottomRightPosition('H20');
  146. // Add the chart to the worksheet
  147. $worksheet->addChart($chart);
  148. /** @var callable */
  149. $callableReader = [$this, 'readCharts'];
  150. /** @var callable */
  151. $callableWriter = [$this, 'writeCharts'];
  152. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  153. $spreadsheet->disconnectWorksheets();
  154. $sheet = $reloadedSpreadsheet->getActiveSheet();
  155. $charts2 = $sheet->getChartCollection();
  156. self::assertCount(1, $charts2);
  157. $chart2 = $charts2[0];
  158. self::assertNotNull($chart2);
  159. $yAxis2 = $chart2->getChartAxisY();
  160. foreach ($expectedY as $key => $value) {
  161. self::assertEquals($value, $yAxis2->getShadowProperty($key), $key);
  162. }
  163. $xAxis2 = $chart2->getChartAxisX();
  164. foreach ($expectedX as $key => $value) {
  165. self::assertEquals($value, $xAxis2->getShadowProperty($key), $key);
  166. }
  167. $reloadedSpreadsheet->disconnectWorksheets();
  168. }
  169. }