AxisGlowTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 AxisGlowTest 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. $xAxis = $chart->getChartAxisX();
  99. $yGlowSize = 10.0;
  100. $yAxis->setGlowProperties($yGlowSize, 'FFFF00', 30, ChartColor::EXCEL_COLOR_TYPE_RGB);
  101. $expectedGlowColor = [
  102. 'type' => 'srgbClr',
  103. 'value' => 'FFFF00',
  104. 'alpha' => 30,
  105. ];
  106. $softEdgesY = 2.5;
  107. $yAxis->setSoftEdges($softEdgesY);
  108. $softEdgesX = 5;
  109. $xAxis->setSoftEdges($softEdgesX);
  110. self::assertEquals($yGlowSize, $yAxis->getGlowProperty('size'));
  111. self::assertEquals($expectedGlowColor, $yAxis->getGlowProperty('color'));
  112. self::assertSame($expectedGlowColor['value'], $yAxis->getGlowProperty(['color', 'value']));
  113. self::assertEquals($softEdgesY, $yAxis->getSoftEdgesSize());
  114. self::assertEquals($softEdgesX, $xAxis->getSoftEdgesSize());
  115. // Set the position where the chart should appear in the worksheet
  116. $chart->setTopLeftPosition('A7');
  117. $chart->setBottomRightPosition('H20');
  118. // Add the chart to the worksheet
  119. $worksheet->addChart($chart);
  120. /** @var callable */
  121. $callableReader = [$this, 'readCharts'];
  122. /** @var callable */
  123. $callableWriter = [$this, 'writeCharts'];
  124. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  125. $spreadsheet->disconnectWorksheets();
  126. $sheet = $reloadedSpreadsheet->getActiveSheet();
  127. $charts2 = $sheet->getChartCollection();
  128. self::assertCount(1, $charts2);
  129. $chart2 = $charts2[0];
  130. self::assertNotNull($chart2);
  131. $yAxis2 = $chart2->getChartAxisY();
  132. self::assertEquals($yGlowSize, $yAxis2->getGlowProperty('size'));
  133. self::assertEquals($expectedGlowColor, $yAxis2->getGlowProperty('color'));
  134. self::assertEquals($softEdgesY, $yAxis2->getSoftEdgesSize());
  135. $xAxis2 = $chart2->getChartAxisX();
  136. self::assertNull($xAxis2->getGlowProperty('size'));
  137. $reloadedSpreadsheet->disconnectWorksheets();
  138. }
  139. public function testGlowX(): void
  140. {
  141. $spreadsheet = new Spreadsheet();
  142. $worksheet = $spreadsheet->getActiveSheet();
  143. $worksheet->fromArray(
  144. [
  145. ['', 2010, 2011, 2012],
  146. ['Q1', 12, 15, 21],
  147. ['Q2', 56, 73, 86],
  148. ['Q3', 52, 61, 69],
  149. ['Q4', 30, 32, 0],
  150. ]
  151. );
  152. // Set the Labels for each data series we want to plot
  153. // Datatype
  154. // Cell reference for data
  155. // Format Code
  156. // Number of datapoints in series
  157. // Data values
  158. // Data Marker
  159. $dataSeriesLabels = [
  160. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  161. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  162. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  163. ];
  164. // Set the X-Axis Labels
  165. // Datatype
  166. // Cell reference for data
  167. // Format Code
  168. // Number of datapoints in series
  169. // Data values
  170. // Data Marker
  171. $xAxisTickValues = [
  172. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  173. ];
  174. // Set the Data values for each data series we want to plot
  175. // Datatype
  176. // Cell reference for data
  177. // Format Code
  178. // Number of datapoints in series
  179. // Data values
  180. // Data Marker
  181. $dataSeriesValues = [
  182. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  183. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  184. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  185. ];
  186. // Build the dataseries
  187. $series = new DataSeries(
  188. DataSeries::TYPE_AREACHART, // plotType
  189. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  190. range(0, count($dataSeriesValues) - 1), // plotOrder
  191. $dataSeriesLabels, // plotLabel
  192. $xAxisTickValues, // plotCategory
  193. $dataSeriesValues // plotValues
  194. );
  195. // Set the series in the plot area
  196. $plotArea = new PlotArea(null, [$series]);
  197. // Set the chart legend
  198. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  199. $title = new Title('Test %age-Stacked Area Chart');
  200. $yAxisLabel = new Title('Value ($k)');
  201. // Create the chart
  202. $chart = new Chart(
  203. 'chart1', // name
  204. $title, // title
  205. $legend, // legend
  206. $plotArea, // plotArea
  207. true, // plotVisibleOnly
  208. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  209. null, // xAxisLabel
  210. $yAxisLabel // yAxisLabel
  211. );
  212. $yAxis = $chart->getChartAxisX(); // deliberate
  213. $yGlowSize = 20.0;
  214. $yAxis->setGlowProperties($yGlowSize, 'accent1', 20, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
  215. $expectedGlowColor = [
  216. 'type' => 'schemeClr',
  217. 'value' => 'accent1',
  218. 'alpha' => 20,
  219. ];
  220. self::assertEquals($yGlowSize, $yAxis->getGlowProperty('size'));
  221. self::assertEquals($expectedGlowColor, $yAxis->getGlowProperty('color'));
  222. // Set the position where the chart should appear in the worksheet
  223. $chart->setTopLeftPosition('A7');
  224. $chart->setBottomRightPosition('H20');
  225. // Add the chart to the worksheet
  226. $worksheet->addChart($chart);
  227. /** @var callable */
  228. $callableReader = [$this, 'readCharts'];
  229. /** @var callable */
  230. $callableWriter = [$this, 'writeCharts'];
  231. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  232. $spreadsheet->disconnectWorksheets();
  233. $sheet = $reloadedSpreadsheet->getActiveSheet();
  234. $charts2 = $sheet->getChartCollection();
  235. self::assertCount(1, $charts2);
  236. $chart2 = $charts2[0];
  237. self::assertNotNull($chart2);
  238. $yAxis2 = $chart2->getChartAxisX(); // deliberate
  239. self::assertEquals($yGlowSize, $yAxis2->getGlowProperty('size'));
  240. self::assertEquals($expectedGlowColor, $yAxis2->getGlowProperty('color'));
  241. $xAxis2 = $chart2->getChartAxisY(); // deliberate
  242. self::assertNull($xAxis2->getGlowProperty('size'));
  243. $reloadedSpreadsheet->disconnectWorksheets();
  244. }
  245. }