Issue589Test.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Chart;
  3. use DOMDocument;
  4. use DOMNode;
  5. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  6. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  7. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  8. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  9. use PhpOffice\PhpSpreadsheet\Shared\File;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
  12. use PHPUnit\Framework\TestCase;
  13. use ZipArchive;
  14. class Issue589Test extends TestCase
  15. {
  16. /**
  17. * Build a testable chart in a spreadsheet and set fill color for series.
  18. *
  19. * @param string|string[] $color HEX color or array with HEX colors
  20. */
  21. private function buildChartSpreadsheet($color): Spreadsheet
  22. {
  23. // Problem occurs when setting plot line color
  24. // The output chart xml file is missing the a:ln tag
  25. $spreadsheet = new Spreadsheet();
  26. $worksheet = $spreadsheet->getActiveSheet();
  27. $worksheet->fromArray(
  28. [
  29. [2010],
  30. [12],
  31. [56],
  32. ]
  33. );
  34. $dataSeriesLabels = [
  35. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$1', null, 1),
  36. ];
  37. $dataSeriesLabels[0]->setFillColor($color);
  38. $dataSeriesValues = [
  39. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$3', null, 2),
  40. ];
  41. // Build the dataseries
  42. $series = new DataSeries(
  43. DataSeries::TYPE_LINECHART,
  44. DataSeries::GROUPING_STACKED,
  45. range(0, count($dataSeriesValues) - 1),
  46. $dataSeriesLabels,
  47. [],
  48. $dataSeriesValues
  49. );
  50. // Set the series in the plot area
  51. $plotArea = new PlotArea(null, [$series]);
  52. // Create the chart
  53. $chart = new Chart(
  54. 'chart1',
  55. null,
  56. null,
  57. $plotArea
  58. );
  59. // Add the chart to the worksheet
  60. $worksheet->addChart($chart);
  61. return $spreadsheet;
  62. }
  63. public function testLineChartFill(): void
  64. {
  65. $outputFilename = File::temporaryFilename();
  66. $spreadsheet = $this->buildChartSpreadsheet('98B954');
  67. $writer = new Writer($spreadsheet);
  68. $writer->setIncludeCharts(true);
  69. $writer->save($outputFilename);
  70. $zip = new ZipArchive();
  71. $zip->open($outputFilename);
  72. $resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
  73. $zip->close();
  74. unlink($outputFilename);
  75. $dom = new DOMDocument();
  76. if ($resultChart1Raw === false) {
  77. self::fail('Unable to open the chart file');
  78. } else {
  79. $loaded = $dom->loadXML($resultChart1Raw);
  80. if (!$loaded) {
  81. self::fail('Unable to load the chart xml');
  82. } else {
  83. $series = $dom->getElementsByTagName('ser');
  84. $firstSeries = $series->item(0);
  85. if ($firstSeries === null) {
  86. self::fail('The chart XML does not contain a \'ser\' tag!');
  87. } else {
  88. $spPrList = $firstSeries->getElementsByTagName('spPr');
  89. // expect to see only one element with name 'c:spPr'
  90. self::assertCount(1, $spPrList);
  91. /** @var DOMNode $node */
  92. $node = $spPrList->item(0); // Get the spPr element
  93. $actualXml = $dom->saveXML($node);
  94. if ($actualXml === false) {
  95. self::fail('Failure saving the spPr element as xml string!');
  96. } else {
  97. self::assertXmlStringEqualsXmlString('<c:spPr><a:ln><a:solidFill><a:srgbClr val="98B954"/></a:solidFill></a:ln></c:spPr>', $actualXml);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. public function testLineChartFillIgnoresColorArray(): void
  104. {
  105. $outputFilename = File::temporaryFilename();
  106. $spreadsheet = $this->buildChartSpreadsheet(['98B954']);
  107. $writer = new Writer($spreadsheet);
  108. $writer->setIncludeCharts(true);
  109. $writer->save($outputFilename);
  110. $zip = new ZipArchive();
  111. $zip->open($outputFilename);
  112. $resultChart1Raw = $zip->getFromName('xl/charts/chart1.xml');
  113. $zip->close();
  114. unlink($outputFilename);
  115. $dom = new DOMDocument();
  116. if ($resultChart1Raw === false) {
  117. self::fail('Unable to open the chart file');
  118. } else {
  119. $loaded = $dom->loadXML($resultChart1Raw);
  120. if (!$loaded) {
  121. self::fail('Unable to load the chart xml');
  122. } else {
  123. $series = $dom->getElementsByTagName('ser');
  124. $firstSeries = $series->item(0);
  125. if ($firstSeries === null) {
  126. self::fail('The chart XML does not contain a \'ser\' tag!');
  127. } else {
  128. $spPrList = $firstSeries->getElementsByTagName('spPr');
  129. // expect to see only one element with name 'c:spPr'
  130. self::assertCount(1, $spPrList);
  131. /** @var DOMNode $node */
  132. $node = $spPrList->item(0); // Get the spPr element
  133. $actualXml = $dom->saveXML($node);
  134. if ($actualXml === false) {
  135. self::fail('Failure saving the spPr element as xml string!');
  136. } else {
  137. self::assertXmlStringEqualsXmlString('<c:spPr><a:ln/></c:spPr>', $actualXml);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }