33_Chart_create_composite.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  5. use PhpOffice\PhpSpreadsheet\Chart\Legend;
  6. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  7. use PhpOffice\PhpSpreadsheet\Chart\Title;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. require __DIR__ . '/../Header.php';
  11. $spreadsheet = new Spreadsheet();
  12. $worksheet = $spreadsheet->getActiveSheet();
  13. $worksheet->fromArray(
  14. [
  15. ['', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'],
  16. ['Jan', 78, 52, 61],
  17. ['Feb', 64, 54, 62],
  18. ['Mar', 62, 57, 63],
  19. ['Apr', 21, 62, 59],
  20. ['May', 11, 75, 60],
  21. ['Jun', 1, 75, 57],
  22. ['Jul', 1, 79, 56],
  23. ['Aug', 1, 79, 59],
  24. ['Sep', 10, 75, 60],
  25. ['Oct', 40, 68, 63],
  26. ['Nov', 69, 62, 64],
  27. ['Dec', 89, 57, 66],
  28. ]
  29. );
  30. // Set the Labels for each data series we want to plot
  31. // Datatype
  32. // Cell reference for data
  33. // Format Code
  34. // Number of datapoints in series
  35. // Data values
  36. // Data Marker
  37. $dataSeriesLabels1 = [
  38. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // Temperature
  39. ];
  40. $dataSeriesLabels2 = [
  41. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // Rainfall
  42. ];
  43. $dataSeriesLabels3 = [
  44. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // Humidity
  45. ];
  46. // Set the X-Axis Labels
  47. // Datatype
  48. // Cell reference for data
  49. // Format Code
  50. // Number of datapoints in series
  51. // Data values
  52. // Data Marker
  53. $xAxisTickValues = [
  54. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec
  55. ];
  56. // Set the Data values for each data series we want to plot
  57. // Datatype
  58. // Cell reference for data
  59. // Format Code
  60. // Number of datapoints in series
  61. // Data values
  62. // Data Marker
  63. $dataSeriesValues1 = [
  64. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$13', null, 12),
  65. ];
  66. // Build the dataseries
  67. $series1 = new DataSeries(
  68. DataSeries::TYPE_BARCHART, // plotType
  69. DataSeries::GROUPING_CLUSTERED, // plotGrouping
  70. range(0, count($dataSeriesValues1) - 1), // plotOrder
  71. $dataSeriesLabels1, // plotLabel
  72. $xAxisTickValues, // plotCategory
  73. $dataSeriesValues1 // plotValues
  74. );
  75. // Set additional dataseries parameters
  76. // Make it a vertical column rather than a horizontal bar graph
  77. $series1->setPlotDirection(DataSeries::DIRECTION_COL);
  78. // Set the Data values for each data series we want to plot
  79. // Datatype
  80. // Cell reference for data
  81. // Format Code
  82. // Number of datapoints in series
  83. // Data values
  84. // Data Marker
  85. $dataSeriesValues2 = [
  86. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$13', null, 12),
  87. ];
  88. // Build the dataseries
  89. $series2 = new DataSeries(
  90. DataSeries::TYPE_LINECHART, // plotType
  91. DataSeries::GROUPING_STANDARD, // plotGrouping
  92. range(0, count($dataSeriesValues2) - 1), // plotOrder
  93. $dataSeriesLabels2, // plotLabel
  94. [], // plotCategory
  95. $dataSeriesValues2 // plotValues
  96. );
  97. // Set the Data values for each data series we want to plot
  98. // Datatype
  99. // Cell reference for data
  100. // Format Code
  101. // Number of datapoints in series
  102. // Data values
  103. // Data Marker
  104. $dataSeriesValues3 = [
  105. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$13', null, 12),
  106. ];
  107. // Build the dataseries
  108. $series3 = new DataSeries(
  109. DataSeries::TYPE_AREACHART, // plotType
  110. DataSeries::GROUPING_STANDARD, // plotGrouping
  111. range(0, count($dataSeriesValues2) - 1), // plotOrder
  112. $dataSeriesLabels3, // plotLabel
  113. [], // plotCategory
  114. $dataSeriesValues3 // plotValues
  115. );
  116. // Set the series in the plot area
  117. $plotArea = new PlotArea(null, [$series1, $series2, $series3]);
  118. // Set the chart legend
  119. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  120. $title = new Title('Average Weather Chart for Crete');
  121. // Create the chart
  122. $chart = new Chart(
  123. 'chart1', // name
  124. $title, // title
  125. $legend, // legend
  126. $plotArea, // plotArea
  127. true, // plotVisibleOnly
  128. 0, // displayBlanksAs
  129. null, // xAxisLabel
  130. null // yAxisLabel
  131. );
  132. // Set the position where the chart should appear in the worksheet
  133. $chart->setTopLeftPosition('F2');
  134. $chart->setBottomRightPosition('O16');
  135. // Add the chart to the worksheet
  136. $worksheet->addChart($chart);
  137. // Save Excel 2007 file
  138. $filename = $helper->getFilename(__FILE__);
  139. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  140. $writer->setIncludeCharts(true);
  141. $callStartTime = microtime(true);
  142. $writer->save($filename);
  143. $helper->logWrite($writer, $filename, $callStartTime);