33_Chart_create_scatter2.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis;
  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\Properties;
  10. use PhpOffice\PhpSpreadsheet\Chart\Title;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. require __DIR__ . '/../Header.php';
  14. $spreadsheet = new Spreadsheet();
  15. $worksheet = $spreadsheet->getActiveSheet();
  16. // changed data to simulate a trend chart - Xaxis are dates; Yaxis are 3 meausurements from each date
  17. $worksheet->fromArray(
  18. [
  19. ['', 'metric1', 'metric2', 'metric3'],
  20. ['=DATEVALUE("2021-01-01")', 12.1, 15.1, 21.1],
  21. ['=DATEVALUE("2021-01-04")', 56.2, 73.2, 86.2],
  22. ['=DATEVALUE("2021-01-07")', 52.2, 61.2, 69.2],
  23. ['=DATEVALUE("2021-01-10")', 30.2, 32.2, 0.2],
  24. ]
  25. );
  26. $worksheet->getStyle('A2:A5')->getNumberFormat()->setFormatCode(Properties::FORMAT_CODE_DATE_ISO8601);
  27. $worksheet->getColumnDimension('A')->setAutoSize(true);
  28. $worksheet->setSelectedCells('A1');
  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), // was 2010
  38. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // was 2011
  39. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // was 2012
  40. ];
  41. // Set the X-Axis Labels
  42. // changed from STRING to NUMBER
  43. // added 2 additional x-axis values associated with each of the 3 metrics
  44. // added FORMATE_CODE_NUMBER
  45. $xAxisTickValues = [
  46. //new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4),
  48. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4),
  49. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4),
  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. // added FORMAT_CODE_NUMBER
  59. $dataSeriesValues = [
  60. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', Properties::FORMAT_CODE_NUMBER, 4),
  61. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', Properties::FORMAT_CODE_NUMBER, 4),
  62. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', Properties::FORMAT_CODE_NUMBER, 4),
  63. ];
  64. // series 1
  65. // marker details
  66. $dataSeriesValues[0]
  67. ->setPointMarker('diamond')
  68. ->setPointSize(5)
  69. ->getMarkerFillColor()
  70. ->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
  71. $dataSeriesValues[0]
  72. ->getMarkerBorderColor()
  73. ->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
  74. // line details - smooth line, connected
  75. $dataSeriesValues[0]
  76. ->setScatterLines(true)
  77. ->setSmoothLine(true)
  78. ->setLineColorProperties('accent1', 40, ChartColor::EXCEL_COLOR_TYPE_SCHEME); // value, alpha, type
  79. $dataSeriesValues[0]->setLineStyleProperties(
  80. 2.5, // width in points
  81. Properties::LINE_STYLE_COMPOUND_TRIPLE, // compound
  82. Properties::LINE_STYLE_DASH_SQUARE_DOT, // dash
  83. Properties::LINE_STYLE_CAP_SQUARE, // cap
  84. Properties::LINE_STYLE_JOIN_MITER, // join
  85. Properties::LINE_STYLE_ARROW_TYPE_OPEN, // head type
  86. Properties::LINE_STYLE_ARROW_SIZE_4, // head size preset index
  87. Properties::LINE_STYLE_ARROW_TYPE_ARROW, // end type
  88. Properties::LINE_STYLE_ARROW_SIZE_6 // end size preset index
  89. );
  90. // series 2 - straight line - no special effects, connected, straight line
  91. $dataSeriesValues[1] // square fill
  92. ->setPointMarker('square')
  93. ->setPointSize(6)
  94. ->getMarkerBorderColor()
  95. ->setColorProperties('accent6', 3, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
  96. $dataSeriesValues[1] // square border
  97. ->getMarkerFillColor()
  98. ->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
  99. $dataSeriesValues[1]
  100. ->setScatterLines(true)
  101. ->setSmoothLine(false)
  102. ->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_RGB);
  103. $dataSeriesValues[1]->setLineWidth(2.0);
  104. // series 3 - markers, no line
  105. $dataSeriesValues[2] // triangle fill
  106. //->setPointMarker('triangle') // let Excel choose shape
  107. ->setPointSize(7)
  108. ->getMarkerFillColor()
  109. ->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
  110. $dataSeriesValues[2] // triangle border
  111. ->getMarkerBorderColor()
  112. ->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME);
  113. $dataSeriesValues[2]->setScatterLines(false); // points not connected
  114. // Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers
  115. $xAxis = new ChartAxis();
  116. //$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE );
  117. $xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true);
  118. $xAxis->setAxisOption('textRotation', '45');
  119. $yAxis = new ChartAxis();
  120. $yAxis->setLineStyleProperties(
  121. 2.5, // width in points
  122. Properties::LINE_STYLE_COMPOUND_SIMPLE,
  123. Properties::LINE_STYLE_DASH_DASH_DOT,
  124. Properties::LINE_STYLE_CAP_FLAT,
  125. Properties::LINE_STYLE_JOIN_BEVEL
  126. );
  127. $yAxis->setLineColorProperties('ffc000', null, ChartColor::EXCEL_COLOR_TYPE_RGB);
  128. // Build the dataseries
  129. $series = new DataSeries(
  130. DataSeries::TYPE_SCATTERCHART, // plotType
  131. null, // plotGrouping (Scatter charts don't have any grouping)
  132. range(0, count($dataSeriesValues) - 1), // plotOrder
  133. $dataSeriesLabels, // plotLabel
  134. $xAxisTickValues, // plotCategory
  135. $dataSeriesValues, // plotValues
  136. null, // plotDirection
  137. false, // smooth line
  138. DataSeries::STYLE_SMOOTHMARKER // plotStyle
  139. );
  140. // Set the series in the plot area
  141. $plotArea = new PlotArea(null, [$series]);
  142. // Set the chart legend
  143. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  144. $title = new Title('Test Scatter Trend Chart');
  145. $yAxisLabel = new Title('Value ($k)');
  146. // Create the chart
  147. $chart = new Chart(
  148. 'chart1', // name
  149. $title, // title
  150. $legend, // legend
  151. $plotArea, // plotArea
  152. true, // plotVisibleOnly
  153. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  154. null, // xAxisLabel
  155. $yAxisLabel, // yAxisLabel
  156. // added xAxis for correct date display
  157. $xAxis, // xAxis
  158. $yAxis, // yAxis
  159. );
  160. // Set the position where the chart should appear in the worksheet
  161. $chart->setTopLeftPosition('A7');
  162. $chart->setBottomRightPosition('P20');
  163. // Add the chart to the worksheet
  164. $worksheet->addChart($chart);
  165. // Save Excel 2007 file
  166. $filename = $helper->getFilename(__FILE__);
  167. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  168. $writer->setIncludeCharts(true);
  169. $callStartTime = microtime(true);
  170. $writer->save($filename);
  171. $spreadsheet->disconnectWorksheets();
  172. $helper->logWrite($writer, $filename, $callStartTime);