Nav apraksta

33chartcreate-composite.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /** Error reporting */
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. ini_set('display_startup_errors', TRUE);
  6. date_default_timezone_set('Europe/London');
  7. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  8. date_default_timezone_set('Europe/London');
  9. /**
  10. * PHPExcel
  11. *
  12. * Copyright (C) 2006 - 2014 PHPExcel
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * @category PHPExcel
  29. * @package PHPExcel
  30. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  31. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  32. * @version ##VERSION##, ##DATE##
  33. */
  34. /** PHPExcel */
  35. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  36. $objPHPExcel = new PHPExcel();
  37. $objWorksheet = $objPHPExcel->getActiveSheet();
  38. $objWorksheet->fromArray(
  39. array(
  40. array('', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'),
  41. array('Jan', 78, 52, 61),
  42. array('Feb', 64, 54, 62),
  43. array('Mar', 62, 57, 63),
  44. array('Apr', 21, 62, 59),
  45. array('May', 11, 75, 60),
  46. array('Jun', 1, 75, 57),
  47. array('Jul', 1, 79, 56),
  48. array('Aug', 1, 79, 59),
  49. array('Sep', 10, 75, 60),
  50. array('Oct', 40, 68, 63),
  51. array('Nov', 69, 62, 64),
  52. array('Dec', 89, 57, 66),
  53. )
  54. );
  55. // Set the Labels for each data series we want to plot
  56. // Datatype
  57. // Cell reference for data
  58. // Format Code
  59. // Number of datapoints in series
  60. // Data values
  61. // Data Marker
  62. $dataSeriesLabels1 = array(
  63. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), // Temperature
  64. );
  65. $dataSeriesLabels2 = array(
  66. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // Rainfall
  67. );
  68. $dataSeriesLabels3 = array(
  69. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // Humidity
  70. );
  71. // Set the X-Axis Labels
  72. // Datatype
  73. // Cell reference for data
  74. // Format Code
  75. // Number of datapoints in series
  76. // Data values
  77. // Data Marker
  78. $xAxisTickValues = array(
  79. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$13', NULL, 12), // Jan to Dec
  80. );
  81. // Set the Data values for each data series we want to plot
  82. // Datatype
  83. // Cell reference for data
  84. // Format Code
  85. // Number of datapoints in series
  86. // Data values
  87. // Data Marker
  88. $dataSeriesValues1 = array(
  89. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$13', NULL, 12),
  90. );
  91. // Build the dataseries
  92. $series1 = new PHPExcel_Chart_DataSeries(
  93. PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
  94. PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
  95. range(0, count($dataSeriesValues1)-1), // plotOrder
  96. $dataSeriesLabels1, // plotLabel
  97. $xAxisTickValues, // plotCategory
  98. $dataSeriesValues1 // plotValues
  99. );
  100. // Set additional dataseries parameters
  101. // Make it a vertical column rather than a horizontal bar graph
  102. $series1->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
  103. // Set the Data values for each data series we want to plot
  104. // Datatype
  105. // Cell reference for data
  106. // Format Code
  107. // Number of datapoints in series
  108. // Data values
  109. // Data Marker
  110. $dataSeriesValues2 = array(
  111. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', NULL, 12),
  112. );
  113. // Build the dataseries
  114. $series2 = new PHPExcel_Chart_DataSeries(
  115. PHPExcel_Chart_DataSeries::TYPE_LINECHART, // plotType
  116. PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
  117. range(0, count($dataSeriesValues2)-1), // plotOrder
  118. $dataSeriesLabels2, // plotLabel
  119. NULL, // plotCategory
  120. $dataSeriesValues2 // plotValues
  121. );
  122. // Set the Data values for each data series we want to plot
  123. // Datatype
  124. // Cell reference for data
  125. // Format Code
  126. // Number of datapoints in series
  127. // Data values
  128. // Data Marker
  129. $dataSeriesValues3 = array(
  130. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', NULL, 12),
  131. );
  132. // Build the dataseries
  133. $series3 = new PHPExcel_Chart_DataSeries(
  134. PHPExcel_Chart_DataSeries::TYPE_AREACHART, // plotType
  135. PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
  136. range(0, count($dataSeriesValues2)-1), // plotOrder
  137. $dataSeriesLabels3, // plotLabel
  138. NULL, // plotCategory
  139. $dataSeriesValues3 // plotValues
  140. );
  141. // Set the series in the plot area
  142. $plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series1, $series2, $series3));
  143. // Set the chart legend
  144. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  145. $title = new PHPExcel_Chart_Title('Average Weather Chart for Crete');
  146. // Create the chart
  147. $chart = new PHPExcel_Chart(
  148. 'chart1', // name
  149. $title, // title
  150. $legend, // legend
  151. $plotArea, // plotArea
  152. true, // plotVisibleOnly
  153. 0, // displayBlanksAs
  154. NULL, // xAxisLabel
  155. NULL // yAxisLabel
  156. );
  157. // Set the position where the chart should appear in the worksheet
  158. $chart->setTopLeftPosition('F2');
  159. $chart->setBottomRightPosition('O16');
  160. // Add the chart to the worksheet
  161. $objWorksheet->addChart($chart);
  162. // Save Excel 2007 file
  163. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  164. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  165. $objWriter->setIncludeCharts(TRUE);
  166. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  167. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  168. // Echo memory peak usage
  169. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  170. // Echo done
  171. echo date('H:i:s') , " Done writing file" , EOL;
  172. echo 'File has been created in ' , getcwd() , EOL;