No Description

33chartcreate-column-2.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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('', '', 'Budget', 'Forecast', 'Actual'),
  41. array('2010', 'Q1', 47, 44, 43 ),
  42. array('', 'Q2', 56, 53, 50 ),
  43. array('', 'Q3', 52, 46, 45 ),
  44. array('', 'Q4', 45, 40, 40 ),
  45. array('2011', 'Q1', 51, 42, 46 ),
  46. array('', 'Q2', 53, 58, 56 ),
  47. array('', 'Q3', 64, 66, 69 ),
  48. array('', 'Q4', 54, 55, 56 ),
  49. array('2012', 'Q1', 49, 52, 58 ),
  50. array('', 'Q2', 68, 73, 86 ),
  51. array('', 'Q3', 72, 78, 0 ),
  52. array('', 'Q4', 50, 60, 0 ),
  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. $dataSeriesLabels = array(
  63. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 'Budget'
  64. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 'Forecast'
  65. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', NULL, 1), // 'Actual'
  66. );
  67. // Set the X-Axis Labels
  68. // Datatype
  69. // Cell reference for data
  70. // Format Code
  71. // Number of datapoints in series
  72. // Data values
  73. // Data Marker
  74. $xAxisTickValues = array(
  75. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$B$13', NULL, 12), // Q1 to Q4 for 2010 to 2012
  76. );
  77. // Set the Data values for each data series we want to plot
  78. // Datatype
  79. // Cell reference for data
  80. // Format Code
  81. // Number of datapoints in series
  82. // Data values
  83. // Data Marker
  84. $dataSeriesValues = array(
  85. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', NULL, 12),
  86. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', NULL, 12),
  87. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$13', NULL, 12),
  88. );
  89. // Build the dataseries
  90. $series = new PHPExcel_Chart_DataSeries(
  91. PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
  92. PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
  93. range(0, count($dataSeriesValues)-1), // plotOrder
  94. $dataSeriesLabels, // plotLabel
  95. $xAxisTickValues, // plotCategory
  96. $dataSeriesValues // plotValues
  97. );
  98. // Set additional dataseries parameters
  99. // Make it a vertical column rather than a horizontal bar graph
  100. $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
  101. // Set the series in the plot area
  102. $plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
  103. // Set the chart legend
  104. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_BOTTOM, NULL, false);
  105. $title = new PHPExcel_Chart_Title('Test Grouped Column Chart');
  106. $xAxisLabel = new PHPExcel_Chart_Title('Financial Period');
  107. $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
  108. // Create the chart
  109. $chart = new PHPExcel_Chart(
  110. 'chart1', // name
  111. $title, // title
  112. $legend, // legend
  113. $plotArea, // plotArea
  114. true, // plotVisibleOnly
  115. 0, // displayBlanksAs
  116. $xAxisLabel, // xAxisLabel
  117. $yAxisLabel // yAxisLabel
  118. );
  119. // Set the position where the chart should appear in the worksheet
  120. $chart->setTopLeftPosition('G2');
  121. $chart->setBottomRightPosition('P20');
  122. // Add the chart to the worksheet
  123. $objWorksheet->addChart($chart);
  124. // Save Excel 2007 file
  125. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  126. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  127. $objWriter->setIncludeCharts(TRUE);
  128. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  129. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  130. // Echo memory peak usage
  131. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  132. // Echo done
  133. echo date('H:i:s') , " Done writing file" , EOL;
  134. echo 'File has been created in ' , getcwd() , EOL;