DataSeriesValues2Test.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  5. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  6. use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
  7. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  8. use PhpOffice\PhpSpreadsheet\Chart\Title;
  9. use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  12. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  13. class DataSeriesValues2Test extends AbstractFunctional
  14. {
  15. public function readCharts(XlsxReader $reader): void
  16. {
  17. $reader->setIncludeCharts(true);
  18. }
  19. public function writeCharts(XlsxWriter $writer): void
  20. {
  21. $writer->setIncludeCharts(true);
  22. }
  23. public function testDataSeriesValues(): void
  24. {
  25. $spreadsheet = new Spreadsheet();
  26. $worksheet = $spreadsheet->getActiveSheet();
  27. $worksheet->fromArray(
  28. [
  29. ['', 2010, 2011, 2012],
  30. ['Q1', 12, 15, 21],
  31. ['Q2', 56, 73, 86],
  32. ['Q3', 52, 61, 69],
  33. ['Q4', 30, 32, 0],
  34. ]
  35. );
  36. // Set the Labels for each data series we want to plot
  37. // Datatype
  38. // Cell reference for data
  39. // Format Code
  40. // Number of datapoints in series
  41. // Data values
  42. // Data Marker
  43. $dataSeriesLabels = [
  44. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  45. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  46. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  47. ];
  48. // Set the X-Axis Labels
  49. // Datatype
  50. // Cell reference for data
  51. // Format Code
  52. // Number of datapoints in series
  53. // Data values
  54. // Data Marker
  55. $xAxisTickValues = [
  56. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  57. ];
  58. // Set the Data values for each data series we want to plot
  59. // Datatype
  60. // Cell reference for data
  61. // Format Code
  62. // Number of datapoints in series
  63. // Data values
  64. // Data Marker
  65. $dataSeriesValues = [
  66. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  67. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  68. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  69. ];
  70. // Build the dataseries
  71. $series = new DataSeries(
  72. null, // plotType
  73. null, // plotGrouping
  74. range(0, count($dataSeriesValues) - 1), // plotOrder
  75. $dataSeriesLabels, // plotLabel
  76. $xAxisTickValues, // plotCategory
  77. $dataSeriesValues // plotValues
  78. );
  79. self::assertEmpty($series->getPlotType());
  80. self::assertEmpty($series->getPlotGrouping());
  81. self::assertFalse($series->getSmoothLine());
  82. $series->setPlotType(DataSeries::TYPE_AREACHART);
  83. $series->setPlotGrouping(DataSeries::GROUPING_PERCENT_STACKED);
  84. $series->setSmoothLine(true);
  85. self::assertSame(DataSeries::TYPE_AREACHART, $series->getPlotType());
  86. self::assertSame(DataSeries::GROUPING_PERCENT_STACKED, $series->getPlotGrouping());
  87. self::assertTrue($series->getSmoothLine());
  88. // Set the series in the plot area
  89. $plotArea = new PlotArea(null, [$series]);
  90. // Set the chart legend
  91. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  92. $title = new Title('Test %age-Stacked Area Chart');
  93. $yAxisLabel = new Title('Value ($k)');
  94. // Create the chart
  95. $chart = new Chart(
  96. 'chart1', // name
  97. $title, // title
  98. $legend, // legend
  99. $plotArea, // plotArea
  100. true, // plotVisibleOnly
  101. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  102. null, // xAxisLabel
  103. $yAxisLabel // yAxisLabel
  104. );
  105. // Set the position where the chart should appear in the worksheet
  106. $chart->setTopLeftPosition('A7');
  107. $chart->setBottomRightPosition('H20');
  108. // Add the chart to the worksheet
  109. $worksheet->addChart($chart);
  110. $plotArea = $chart->getPlotArea();
  111. self::assertNotNull($plotArea);
  112. self::assertSame(1, $plotArea->getPlotGroupCount());
  113. $plotValues = $plotArea->getPlotGroup()[0]->getPlotValues();
  114. self::assertCount(3, $plotValues);
  115. self::assertSame([], $plotValues[1]->getDataValues());
  116. self::assertNull($plotValues[1]->getDataValue());
  117. /** @var callable */
  118. $callableReader = [$this, 'readCharts'];
  119. /** @var callable */
  120. $callableWriter = [$this, 'writeCharts'];
  121. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
  122. $spreadsheet->disconnectWorksheets();
  123. $sheet = $reloadedSpreadsheet->getActiveSheet();
  124. $charts2 = $sheet->getChartCollection();
  125. self::assertCount(1, $charts2);
  126. $chart2 = $charts2[0];
  127. self::assertNotNull($chart2);
  128. $plotArea2 = $chart2->getPlotArea();
  129. self::assertNotNull($plotArea2);
  130. $plotGroup2 = $plotArea2->getPlotGroup()[0];
  131. self::assertNotNull($plotGroup2);
  132. $plotValues2 = $plotGroup2->getPlotValues();
  133. self::assertCount(3, $plotValues2);
  134. self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValues());
  135. self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValue());
  136. $labels2 = $plotGroup2->getPlotLabels();
  137. self::assertCount(3, $labels2);
  138. self::assertEquals(2010, $labels2[0]->getDataValue());
  139. $dataSeries = $plotArea2->getPlotGroup()[0];
  140. self::assertFalse($dataSeries->getPlotValuesByIndex(99));
  141. self::assertNotFalse($dataSeries->getPlotValuesByIndex(0));
  142. self::assertEquals([12, 56, 52, 30], $dataSeries->getPlotValuesByIndex(0)->getDataValues());
  143. self::assertSame(DataSeries::TYPE_AREACHART, $dataSeries->getPlotType());
  144. self::assertSame(DataSeries::GROUPING_PERCENT_STACKED, $dataSeries->getPlotGrouping());
  145. // SmoothLine written out for DataSeries only for LineChart.
  146. // Original test was wrong - used $chart rather than $chart2
  147. // to retrieve data which was read in.
  148. //self::assertTrue($dataSeries->getSmoothLine());
  149. $reloadedSpreadsheet->disconnectWorksheets();
  150. }
  151. public function testSomeProperties(): void
  152. {
  153. $dataSeriesValues = new DataSeriesValues();
  154. self::assertNull($dataSeriesValues->getDataSource());
  155. self::assertEmpty($dataSeriesValues->getPointMarker());
  156. self::assertSame(3, $dataSeriesValues->getPointSize());
  157. $dataSeriesValues->setDataSource('Worksheet!$B$1')
  158. ->setPointMarker('square')
  159. ->setPointSize(6);
  160. self::assertSame('Worksheet!$B$1', $dataSeriesValues->getDataSource());
  161. self::assertSame('square', $dataSeriesValues->getPointMarker());
  162. self::assertSame(6, $dataSeriesValues->getPointSize());
  163. }
  164. }