33chartcreate-pie.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 - 2015 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 - 2015 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('', 2010, 2011, 2012),
  41. array('Q1', 12, 15, 21),
  42. array('Q2', 56, 73, 86),
  43. array('Q3', 52, 61, 69),
  44. array('Q4', 30, 32, 0),
  45. )
  46. );
  47. // Set the Labels for each data series we want to plot
  48. // Datatype
  49. // Cell reference for data
  50. // Format Code
  51. // Number of datapoints in series
  52. // Data values
  53. // Data Marker
  54. $dataSeriesLabels1 = array(
  55. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
  56. );
  57. // Set the X-Axis Labels
  58. // Datatype
  59. // Cell reference for data
  60. // Format Code
  61. // Number of datapoints in series
  62. // Data values
  63. // Data Marker
  64. $xAxisTickValues1 = array(
  65. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4
  66. );
  67. // Set the Data values for each data series we want to plot
  68. // Datatype
  69. // Cell reference for data
  70. // Format Code
  71. // Number of datapoints in series
  72. // Data values
  73. // Data Marker
  74. $dataSeriesValues1 = array(
  75. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
  76. );
  77. // Build the dataseries
  78. $series1 = new PHPExcel_Chart_DataSeries(
  79. PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
  80. NULL, // plotGrouping (Pie charts don't have any grouping)
  81. range(0, count($dataSeriesValues1)-1), // plotOrder
  82. $dataSeriesLabels1, // plotLabel
  83. $xAxisTickValues1, // plotCategory
  84. $dataSeriesValues1 // plotValues
  85. );
  86. // Set up a layout object for the Pie chart
  87. $layout1 = new PHPExcel_Chart_Layout();
  88. $layout1->setShowVal(TRUE);
  89. $layout1->setShowPercent(TRUE);
  90. // Set the series in the plot area
  91. $plotArea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
  92. // Set the chart legend
  93. $legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  94. $title1 = new PHPExcel_Chart_Title('Test Pie Chart');
  95. // Create the chart
  96. $chart1 = new PHPExcel_Chart(
  97. 'chart1', // name
  98. $title1, // title
  99. $legend1, // legend
  100. $plotArea1, // plotArea
  101. true, // plotVisibleOnly
  102. 0, // displayBlanksAs
  103. NULL, // xAxisLabel
  104. NULL // yAxisLabel - Pie charts don't have a Y-Axis
  105. );
  106. // Set the position where the chart should appear in the worksheet
  107. $chart1->setTopLeftPosition('A7');
  108. $chart1->setBottomRightPosition('H20');
  109. // Add the chart to the worksheet
  110. $objWorksheet->addChart($chart1);
  111. // Set the Labels for each data series we want to plot
  112. // Datatype
  113. // Cell reference for data
  114. // Format Code
  115. // Number of datapoints in series
  116. // Data values
  117. // Data Marker
  118. $dataSeriesLabels2 = array(
  119. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
  120. );
  121. // Set the X-Axis Labels
  122. // Datatype
  123. // Cell reference for data
  124. // Format Code
  125. // Number of datapoints in series
  126. // Data values
  127. // Data Marker
  128. $xAxisTickValues2 = array(
  129. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4
  130. );
  131. // Set the Data values for each data series we want to plot
  132. // Datatype
  133. // Cell reference for data
  134. // Format Code
  135. // Number of datapoints in series
  136. // Data values
  137. // Data Marker
  138. $dataSeriesValues2 = array(
  139. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
  140. );
  141. // Build the dataseries
  142. $series2 = new PHPExcel_Chart_DataSeries(
  143. PHPExcel_Chart_DataSeries::TYPE_DONUTCHART, // plotType
  144. NULL, // plotGrouping (Donut charts don't have any grouping)
  145. range(0, count($dataSeriesValues2)-1), // plotOrder
  146. $dataSeriesLabels2, // plotLabel
  147. $xAxisTickValues2, // plotCategory
  148. $dataSeriesValues2 // plotValues
  149. );
  150. // Set up a layout object for the Pie chart
  151. $layout2 = new PHPExcel_Chart_Layout();
  152. $layout2->setShowVal(TRUE);
  153. $layout2->setShowCatName(TRUE);
  154. // Set the series in the plot area
  155. $plotArea2 = new PHPExcel_Chart_PlotArea($layout2, array($series2));
  156. $title2 = new PHPExcel_Chart_Title('Test Donut Chart');
  157. // Create the chart
  158. $chart2 = new PHPExcel_Chart(
  159. 'chart2', // name
  160. $title2, // title
  161. NULL, // legend
  162. $plotArea2, // plotArea
  163. true, // plotVisibleOnly
  164. 0, // displayBlanksAs
  165. NULL, // xAxisLabel
  166. NULL // yAxisLabel - Like Pie charts, Donut charts don't have a Y-Axis
  167. );
  168. // Set the position where the chart should appear in the worksheet
  169. $chart2->setTopLeftPosition('I7');
  170. $chart2->setBottomRightPosition('P20');
  171. // Add the chart to the worksheet
  172. $objWorksheet->addChart($chart2);
  173. // Save Excel 2007 file
  174. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  175. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  176. $objWriter->setIncludeCharts(TRUE);
  177. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  178. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  179. // Echo memory peak usage
  180. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  181. // Echo done
  182. echo date('H:i:s') , " Done writing file" , EOL;
  183. echo 'File has been created in ' , getcwd() , EOL;