MultiplierTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Shared\File;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  13. use PHPUnit\Framework\TestCase;
  14. class MultiplierTest extends TestCase
  15. {
  16. public function testMultiplier(): void
  17. {
  18. $spreadsheet = new Spreadsheet();
  19. $worksheet = $spreadsheet->getActiveSheet();
  20. $worksheet->fromArray(
  21. [
  22. ['', 2010, 2011, 2012],
  23. ['Q1', 12, 15, 21],
  24. ['Q2', 56, 73, 86],
  25. ['Q3', 52, 61, 69],
  26. ['Q4', 30, 32, 0],
  27. ]
  28. );
  29. // Set the Labels for each data series we want to plot
  30. // Datatype
  31. // Cell reference for data
  32. // Format Code
  33. // Number of datapoints in series
  34. // Data values
  35. // Data Marker
  36. $dataSeriesLabels = [
  37. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  38. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  39. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  40. ];
  41. // Set the X-Axis Labels
  42. // Datatype
  43. // Cell reference for data
  44. // Format Code
  45. // Number of datapoints in series
  46. // Data values
  47. // Data Marker
  48. $xAxisTickValues = [
  49. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  50. ];
  51. // Set the Data values for each data series we want to plot
  52. // Datatype
  53. // Cell reference for data
  54. // Format Code
  55. // Number of datapoints in series
  56. // Data values
  57. // Data Marker
  58. $dataSeriesValues = [
  59. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  60. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  61. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  62. ];
  63. // Build the dataseries
  64. $series = new DataSeries(
  65. DataSeries::TYPE_AREACHART, // plotType
  66. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  67. range(0, count($dataSeriesValues) - 1), // plotOrder
  68. $dataSeriesLabels, // plotLabel
  69. $xAxisTickValues, // plotCategory
  70. $dataSeriesValues // plotValues
  71. );
  72. // Set the series in the plot area
  73. $plotArea = new PlotArea(null, [$series]);
  74. // Set the chart legend
  75. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  76. $title = new Title('Test %age-Stacked Area Chart');
  77. $yAxisLabel = new Title('Value ($k)');
  78. // Create the chart
  79. $chart = new Chart(
  80. 'chart1', // name
  81. $title, // title
  82. $legend, // legend
  83. $plotArea, // plotArea
  84. true, // plotVisibleOnly
  85. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  86. null, // xAxisLabel
  87. $yAxisLabel // yAxisLabel
  88. );
  89. $xAxis = $chart->getChartAxisX();
  90. $expectedX = [
  91. 'effect' => 'outerShdw',
  92. 'algn' => 'bl',
  93. 'blur' => 6,
  94. 'direction' => 315,
  95. 'distance' => 3,
  96. 'rotWithShape' => 0,
  97. 'size' => [
  98. 'sx' => null,
  99. 'sy' => 254,
  100. 'kx' => -94,
  101. 'ky' => null,
  102. ],
  103. 'color' => [
  104. 'type' => ChartColor::EXCEL_COLOR_TYPE_RGB,
  105. 'value' => 'FF0000',
  106. 'alpha' => 20,
  107. ],
  108. ];
  109. $expectedXmlX = [
  110. '<a:outerShdw ',
  111. ' algn="bl"',
  112. ' blurRad="76200"',
  113. ' dir="18900000"',
  114. ' dist="38100"',
  115. ' rotWithShape="0"',
  116. ' sy="25400000"',
  117. ' kx="-5640000"',
  118. '<a:srgbClr val="FF0000">',
  119. '<a:alpha val="80000"/>',
  120. ];
  121. $expectedXmlNoX = [
  122. ' sx=',
  123. ' ky=',
  124. ];
  125. foreach ($expectedX as $key => $value) {
  126. $xAxis->setShadowProperty($key, $value);
  127. }
  128. // Set the position where the chart should appear in the worksheet
  129. $chart->setTopLeftPosition('A7');
  130. $chart->setBottomRightPosition('H20');
  131. // Add the chart to the worksheet
  132. $worksheet->addChart($chart);
  133. $writer = new XlsxWriter($spreadsheet);
  134. $writer->setIncludeCharts(true);
  135. $writerChart = new XlsxWriter\Chart($writer);
  136. $data = $writerChart->writeChart($chart);
  137. // confirm that file contains expected tags
  138. foreach ($expectedXmlX as $expected) {
  139. self::assertSame(1, substr_count($data, $expected), $expected);
  140. }
  141. foreach ($expectedXmlNoX as $expected) {
  142. self::assertSame(0, substr_count($data, $expected), $expected);
  143. }
  144. $spreadsheet->disconnectWorksheets();
  145. }
  146. }