GridlinesShadowGlowTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\Axis;
  4. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  5. use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
  6. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  7. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  8. use PhpOffice\PhpSpreadsheet\Chart\GridLines;
  9. use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
  10. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  11. use PhpOffice\PhpSpreadsheet\Chart\Title;
  12. use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  15. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  16. class GridlinesShadowGlowTest extends AbstractFunctional
  17. {
  18. public function readCharts(XlsxReader $reader): void
  19. {
  20. $reader->setIncludeCharts(true);
  21. }
  22. public function writeCharts(XlsxWriter $writer): void
  23. {
  24. $writer->setIncludeCharts(true);
  25. }
  26. public function testGlowY(): void
  27. {
  28. $spreadsheet = new Spreadsheet();
  29. $worksheet = $spreadsheet->getActiveSheet();
  30. $worksheet->fromArray(
  31. [
  32. ['', 2010, 2011, 2012],
  33. ['Q1', 12, 15, 21],
  34. ['Q2', 56, 73, 86],
  35. ['Q3', 52, 61, 69],
  36. ['Q4', 30, 32, 0],
  37. ]
  38. );
  39. // Set the Labels for each data series we want to plot
  40. // Datatype
  41. // Cell reference for data
  42. // Format Code
  43. // Number of datapoints in series
  44. // Data values
  45. // Data Marker
  46. $dataSeriesLabels = [
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  48. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  49. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  50. ];
  51. // Set the X-Axis Labels
  52. // Datatype
  53. // Cell reference for data
  54. // Format Code
  55. // Number of datapoints in series
  56. // Data values
  57. // Data Marker
  58. $xAxisTickValues = [
  59. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  60. ];
  61. // Set the Data values for each data series we want to plot
  62. // Datatype
  63. // Cell reference for data
  64. // Format Code
  65. // Number of datapoints in series
  66. // Data values
  67. // Data Marker
  68. $dataSeriesValues = [
  69. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  70. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  71. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  72. ];
  73. // Build the dataseries
  74. $series = new DataSeries(
  75. DataSeries::TYPE_LINECHART, // plotType
  76. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  77. range(0, count($dataSeriesValues) - 1), // plotOrder
  78. $dataSeriesLabels, // plotLabel
  79. $xAxisTickValues, // plotCategory
  80. $dataSeriesValues // plotValues
  81. );
  82. // Set the series in the plot area
  83. $plotArea = new PlotArea(null, [$series]);
  84. // Set the chart legend
  85. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  86. $title = new Title('Test %age-Stacked Area Chart');
  87. $yAxisLabel = new Title('Value ($k)');
  88. $yAxis = new Axis();
  89. $majorGridlines = new GridLines();
  90. $yAxis->setMajorGridlines($majorGridlines);
  91. $majorGlowSize = 10.0;
  92. $majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
  93. $softEdgeSize = 2.5;
  94. $majorGridlines->setSoftEdges($softEdgeSize);
  95. $expectedGlowColor = [
  96. 'type' => 'srgbClr',
  97. 'value' => 'FFFF00',
  98. 'alpha' => 30,
  99. ];
  100. self::assertEquals($majorGlowSize, $majorGridlines->getGlowProperty('size'));
  101. self::assertEquals($majorGlowSize, $majorGridlines->getGlowSize());
  102. self::assertEquals($expectedGlowColor['value'], $majorGridlines->getGlowColor('value'));
  103. self::assertEquals($expectedGlowColor, $majorGridlines->getGlowProperty('color'));
  104. self::assertEquals($softEdgeSize, $majorGridlines->getSoftEdgesSize());
  105. $minorGridlines = new GridLines();
  106. $yAxis->setMinorGridlines($minorGridlines);
  107. $expectedShadow = [
  108. 'effect' => 'outerShdw',
  109. 'algn' => 'tl',
  110. 'blur' => 4,
  111. 'direction' => 45,
  112. 'distance' => 3,
  113. 'rotWithShape' => 0,
  114. 'color' => [
  115. 'type' => ChartColor::EXCEL_COLOR_TYPE_STANDARD,
  116. 'value' => 'black',
  117. 'alpha' => 40,
  118. ],
  119. ];
  120. foreach ($expectedShadow as $key => $value) {
  121. $minorGridlines->setShadowProperty($key, $value);
  122. }
  123. foreach ($expectedShadow as $key => $value) {
  124. self::assertEquals($value, $minorGridlines->getShadowProperty($key), $key);
  125. }
  126. $testShadow2 = $minorGridlines->getShadowArray();
  127. self::assertNull($testShadow2['presets']);
  128. self::assertEquals(['sx' => null, 'sy' => null, 'kx' => null, 'ky' => null], $testShadow2['size']);
  129. unset($testShadow2['presets'], $testShadow2['size']);
  130. self::assertEquals($expectedShadow, $testShadow2);
  131. // Create the chart
  132. $chart = new Chart(
  133. 'chart1', // name
  134. $title, // title
  135. $legend, // legend
  136. $plotArea, // plotArea
  137. true, // plotVisibleOnly
  138. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  139. null, // xAxisLabel
  140. $yAxisLabel, // yAxisLabel
  141. null, // xAxis
  142. $yAxis // yAxis
  143. );
  144. $yAxis2 = $chart->getChartAxisY();
  145. $majorGridlines2 = $yAxis2->getMajorGridlines();
  146. self::assertNotNull($majorGridlines2);
  147. self::assertEquals($majorGlowSize, $majorGridlines2->getGlowProperty('size'));
  148. self::assertEquals($expectedGlowColor, $majorGridlines2->getGlowProperty('color'));
  149. self::assertEquals($softEdgeSize, $majorGridlines2->getSoftEdgesSize());
  150. $minorGridlines2 = $yAxis2->getMinorGridlines();
  151. self::assertNotNull($minorGridlines2);
  152. foreach ($expectedShadow as $key => $value) {
  153. self::assertEquals($value, $minorGridlines2->getShadowProperty($key), $key);
  154. }
  155. // Set the position where the chart should appear in the worksheet
  156. $chart->setTopLeftPosition('A7');
  157. $chart->setBottomRightPosition('H20');
  158. // Add the chart to the worksheet
  159. $worksheet->addChart($chart);
  160. /** @var callable */
  161. $callableReader = [$this, 'readCharts'];
  162. /** @var callable */
  163. $callableWriter = [$this, 'writeCharts'];
  164. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  165. $spreadsheet->disconnectWorksheets();
  166. $sheet = $reloadedSpreadsheet->getActiveSheet();
  167. $charts2 = $sheet->getChartCollection();
  168. self::assertCount(1, $charts2);
  169. $chart2 = $charts2[0];
  170. self::assertNotNull($chart2);
  171. $yAxis3 = $chart2->getChartAxisY();
  172. $majorGridlines3 = $yAxis3->getMajorGridlines();
  173. self::assertNotNull($majorGridlines3);
  174. self::assertEquals($majorGlowSize, $majorGridlines3->getGlowProperty('size'));
  175. self::assertEquals($expectedGlowColor, $majorGridlines3->getGlowProperty('color'));
  176. self::assertEquals($softEdgeSize, $majorGridlines3->getSoftEdgesSize());
  177. $minorGridlines3 = $yAxis3->getMinorGridlines();
  178. self::assertNotNull($minorGridlines3);
  179. foreach ($expectedShadow as $key => $value) {
  180. self::assertEquals($value, $minorGridlines3->getShadowProperty($key), $key);
  181. }
  182. $reloadedSpreadsheet->disconnectWorksheets();
  183. }
  184. }