AxisPropertiesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\Axis;
  4. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  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\Properties;
  10. use PhpOffice\PhpSpreadsheet\Chart\Title;
  11. use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  14. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  15. class AxisPropertiesTest extends AbstractFunctional
  16. {
  17. public function readCharts(XlsxReader $reader): void
  18. {
  19. $reader->setIncludeCharts(true);
  20. }
  21. public function writeCharts(XlsxWriter $writer): void
  22. {
  23. $writer->setIncludeCharts(true);
  24. }
  25. public function testAxisProperties(): void
  26. {
  27. $spreadsheet = new Spreadsheet();
  28. $worksheet = $spreadsheet->getActiveSheet();
  29. $worksheet->fromArray(
  30. [
  31. ['', 2010, 2011, 2012],
  32. ['Q1', 12, 15, 21],
  33. ['Q2', 56, 73, 86],
  34. ['Q3', 52, 61, 69],
  35. ['Q4', 30, 32, 0],
  36. ]
  37. );
  38. // Set the Labels for each data series we want to plot
  39. // Datatype
  40. // Cell reference for data
  41. // Format Code
  42. // Number of datapoints in series
  43. // Data values
  44. // Data Marker
  45. $dataSeriesLabels = [
  46. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  48. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  49. ];
  50. // Set the X-Axis Labels
  51. // Datatype
  52. // Cell reference for data
  53. // Format Code
  54. // Number of datapoints in series
  55. // Data values
  56. // Data Marker
  57. $xAxisTickValues = [
  58. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  59. ];
  60. // Set the Data values for each data series we want to plot
  61. // Datatype
  62. // Cell reference for data
  63. // Format Code
  64. // Number of datapoints in series
  65. // Data values
  66. // Data Marker
  67. $dataSeriesValues = [
  68. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  69. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  70. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  71. ];
  72. // Build the dataseries
  73. $series = new DataSeries(
  74. DataSeries::TYPE_LINECHART, // plotType
  75. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  76. range(0, count($dataSeriesValues) - 1), // plotOrder
  77. $dataSeriesLabels, // plotLabel
  78. $xAxisTickValues, // plotCategory
  79. $dataSeriesValues // plotValues
  80. );
  81. // Set the series in the plot area
  82. $plotArea = new PlotArea(null, [$series]);
  83. // Set the chart legend
  84. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  85. $title = new Title('Test %age-Stacked Area Chart');
  86. $yAxisLabel = new Title('Value ($k)');
  87. $xAxis = new Axis();
  88. $xAxis->setFillParameters('FF0000', null, 'srgbClr');
  89. self::assertSame('FF0000', $xAxis->getFillProperty('value'));
  90. self::assertSame('', $xAxis->getFillProperty('alpha'));
  91. self::assertSame('srgbClr', $xAxis->getFillProperty('type'));
  92. $xAxis->setAxisOptionsProperties(
  93. Properties::AXIS_LABELS_HIGH, // axisLabels,
  94. null, // $horizontalCrossesValue,
  95. Properties::HORIZONTAL_CROSSES_MAXIMUM, //horizontalCrosses
  96. Properties::ORIENTATION_REVERSED, //axisOrientation
  97. Properties::TICK_MARK_INSIDE, //majorTmt
  98. Properties::TICK_MARK_OUTSIDE, //minorTmt
  99. '8', //minimum
  100. '68', //maximum
  101. '20', //majorUnit
  102. '5', //minorUnit
  103. '6', //textRotation
  104. '0', //hidden
  105. );
  106. self::assertSame(Properties::AXIS_LABELS_HIGH, $xAxis->getAxisOptionsProperty('axis_labels'));
  107. self::assertNull($xAxis->getAxisOptionsProperty('horizontal_crosses_value'));
  108. self::assertSame(Properties::HORIZONTAL_CROSSES_MAXIMUM, $xAxis->getAxisOptionsProperty('horizontal_crosses'));
  109. self::assertSame(Properties::ORIENTATION_REVERSED, $xAxis->getAxisOptionsProperty('orientation'));
  110. self::assertSame(Properties::TICK_MARK_INSIDE, $xAxis->getAxisOptionsProperty('major_tick_mark'));
  111. self::assertSame(Properties::TICK_MARK_OUTSIDE, $xAxis->getAxisOptionsProperty('minor_tick_mark'));
  112. self::assertSame('8', $xAxis->getAxisOptionsProperty('minimum'));
  113. self::assertSame('68', $xAxis->getAxisOptionsProperty('maximum'));
  114. self::assertSame('20', $xAxis->getAxisOptionsProperty('major_unit'));
  115. self::assertSame('5', $xAxis->getAxisOptionsProperty('minor_unit'));
  116. self::assertSame('6', $xAxis->getAxisOptionsProperty('textRotation'));
  117. self::assertSame('0', $xAxis->getAxisOptionsProperty('hidden'));
  118. $yAxis = new Axis();
  119. $yAxis->setFillParameters('accent1', 30, 'schemeClr');
  120. self::assertSame('accent1', $yAxis->getFillProperty('value'));
  121. self::assertSame('30', $yAxis->getFillProperty('alpha'));
  122. self::assertSame('schemeClr', $yAxis->getFillProperty('type'));
  123. // Create the chart
  124. $chart = new Chart(
  125. 'chart1', // name
  126. $title, // title
  127. $legend, // legend
  128. $plotArea, // plotArea
  129. true, // plotVisibleOnly
  130. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  131. null, // xAxisLabel
  132. $yAxisLabel, // yAxisLabel
  133. $xAxis, // xAxis
  134. $yAxis, // yAxis
  135. null, //majorGridlines,
  136. null // minorGridlines
  137. );
  138. $xAxis2 = $chart->getChartAxisX();
  139. self::assertSame('FF0000', $xAxis2->getFillProperty('value'));
  140. self::assertSame('', $xAxis2->getFillProperty('alpha'));
  141. self::assertSame('srgbClr', $xAxis2->getFillProperty('type'));
  142. self::assertSame(Properties::AXIS_LABELS_HIGH, $xAxis2->getAxisOptionsProperty('axis_labels'));
  143. self::assertNull($xAxis2->getAxisOptionsProperty('horizontal_crosses_value'));
  144. self::assertSame(Properties::HORIZONTAL_CROSSES_MAXIMUM, $xAxis2->getAxisOptionsProperty('horizontal_crosses'));
  145. self::assertSame(Properties::ORIENTATION_REVERSED, $xAxis2->getAxisOptionsProperty('orientation'));
  146. self::assertSame(Properties::TICK_MARK_INSIDE, $xAxis2->getAxisOptionsProperty('major_tick_mark'));
  147. self::assertSame(Properties::TICK_MARK_OUTSIDE, $xAxis2->getAxisOptionsProperty('minor_tick_mark'));
  148. self::assertSame('8', $xAxis2->getAxisOptionsProperty('minimum'));
  149. self::assertSame('68', $xAxis2->getAxisOptionsProperty('maximum'));
  150. self::assertSame('20', $xAxis2->getAxisOptionsProperty('major_unit'));
  151. self::assertSame('5', $xAxis2->getAxisOptionsProperty('minor_unit'));
  152. self::assertSame('6', $xAxis2->getAxisOptionsProperty('textRotation'));
  153. self::assertSame('0', $xAxis2->getAxisOptionsProperty('hidden'));
  154. $yAxis2 = $chart->getChartAxisY();
  155. self::assertSame('accent1', $yAxis2->getFillProperty('value'));
  156. self::assertSame('30', $yAxis2->getFillProperty('alpha'));
  157. self::assertSame('schemeClr', $yAxis2->getFillProperty('type'));
  158. // Set the position where the chart should appear in the worksheet
  159. $chart->setTopLeftPosition('A7');
  160. $chart->setBottomRightPosition('H20');
  161. // Add the chart to the worksheet
  162. $worksheet->addChart($chart);
  163. /** @var callable */
  164. $callableReader = [$this, 'readCharts'];
  165. /** @var callable */
  166. $callableWriter = [$this, 'writeCharts'];
  167. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  168. $spreadsheet->disconnectWorksheets();
  169. $sheet = $reloadedSpreadsheet->getActiveSheet();
  170. $charts2 = $sheet->getChartCollection();
  171. self::assertCount(1, $charts2);
  172. $chart2 = $charts2[0];
  173. self::assertNotNull($chart2);
  174. $xAxis3 = $chart2->getChartAxisX();
  175. self::assertSame('FF0000', $xAxis3->getFillProperty('value'));
  176. self::assertSame('', $xAxis3->getFillProperty('alpha'));
  177. self::assertSame('srgbClr', $xAxis3->getFillProperty('type'));
  178. self::assertSame(Properties::AXIS_LABELS_HIGH, $xAxis3->getAxisOptionsProperty('axis_labels'));
  179. self::assertSame(Properties::TICK_MARK_INSIDE, $xAxis3->getAxisOptionsProperty('major_tick_mark'));
  180. self::assertSame(Properties::TICK_MARK_OUTSIDE, $xAxis3->getAxisOptionsProperty('minor_tick_mark'));
  181. self::assertNull($xAxis3->getAxisOptionsProperty('horizontal_crosses_value'));
  182. self::assertSame(Properties::HORIZONTAL_CROSSES_MAXIMUM, $xAxis3->getAxisOptionsProperty('horizontal_crosses'));
  183. self::assertSame(Properties::ORIENTATION_REVERSED, $xAxis3->getAxisOptionsProperty('orientation'));
  184. self::assertSame('8', $xAxis3->getAxisOptionsProperty('minimum'));
  185. self::assertSame('68', $xAxis3->getAxisOptionsProperty('maximum'));
  186. self::assertSame('20', $xAxis3->getAxisOptionsProperty('major_unit'));
  187. self::assertSame('5', $xAxis3->getAxisOptionsProperty('minor_unit'));
  188. self::assertSame('6', $xAxis3->getAxisOptionsProperty('textRotation'));
  189. self::assertSame('0', $xAxis3->getAxisOptionsProperty('hidden'));
  190. $yAxis3 = $chart2->getChartAxisY();
  191. self::assertSame('accent1', $yAxis3->getFillProperty('value'));
  192. self::assertSame('30', $yAxis3->getFillProperty('alpha'));
  193. self::assertSame('schemeClr', $yAxis3->getFillProperty('type'));
  194. $xAxis3->setAxisOrientation(Properties::ORIENTATION_NORMAL);
  195. self::assertSame(Properties::ORIENTATION_NORMAL, $xAxis3->getAxisOptionsProperty('orientation'));
  196. $xAxis3->setAxisOptionsProperties(
  197. Properties::AXIS_LABELS_HIGH, // axisLabels,
  198. '5' // $horizontalCrossesValue,
  199. );
  200. self::assertSame('5', $xAxis3->getAxisOptionsProperty('horizontal_crosses_value'));
  201. $yAxis3->setLineColorProperties('0000FF', null, 'srgbClr');
  202. self::assertSame('0000FF', $yAxis3->getLineProperty('value'));
  203. self::assertNull($yAxis3->getLineProperty('alpha'));
  204. self::assertSame('srgbClr', $yAxis3->getLineProperty('type'));
  205. $yAxis3->setAxisNumberProperties(Properties::FORMAT_CODE_GENERAL);
  206. self::assertFalse($yAxis3->getAxisIsNumericFormat());
  207. $yAxis3->setAxisNumberProperties(Properties::FORMAT_CODE_NUMBER);
  208. self::assertTrue($yAxis3->getAxisIsNumericFormat());
  209. $reloadedSpreadsheet->disconnectWorksheets();
  210. }
  211. }