| 1 |
<?php |
| 2 |
|
| 3 |
/** Error reporting */ |
| 4 |
error_reporting(E_ALL); |
| 5 |
ini_set('display_errors', TRUE); |
| 6 |
ini_set('display_startup_errors', TRUE); |
| 7 |
date_default_timezone_set('Europe/London'); |
| 8 |
|
| 9 |
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); |
| 10 |
|
| 11 |
date_default_timezone_set('Europe/London'); |
| 12 |
|
| 13 |
/** |
| 14 |
* PHPExcel |
| 15 |
* |
| 16 |
* Copyright (C) 2006 - 2014 PHPExcel |
| 17 |
* |
| 18 |
* This library is free software; you can redistribute it and/or |
| 19 |
* modify it under the terms of the GNU Lesser General Public |
| 20 |
* License as published by the Free Software Foundation; either |
| 21 |
* version 2.1 of the License, or (at your option) any later version. |
| 22 |
* |
| 23 |
* This library is distributed in the hope that it will be useful, |
| 24 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 26 |
* Lesser General Public License for more details. |
| 27 |
* |
| 28 |
* You should have received a copy of the GNU Lesser General Public |
| 29 |
* License along with this library; if not, write to the Free Software |
| 30 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 31 |
* |
| 32 |
* @category PHPExcel |
| 33 |
* @package PHPExcel |
| 34 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 35 |
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 36 |
* @version ##VERSION##, ##DATE## |
| 37 |
*/ |
| 38 |
|
| 39 |
/** PHPExcel */ |
| 40 |
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; |
| 41 |
|
| 42 |
|
| 43 |
$objPHPExcel = new PHPExcel(); |
| 44 |
$objWorksheet = $objPHPExcel->getActiveSheet(); |
| 45 |
$objWorksheet->fromArray( |
| 46 |
array( |
| 47 |
array('', 2010, 2011, 2012), |
| 48 |
array('Q1', 12, 15, 21), |
| 49 |
array('Q2', 56, 73, 86), |
| 50 |
array('Q3', 52, 61, 69), |
| 51 |
array('Q4', 30, 32, 0), |
| 52 |
) |
| 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!$B$1', NULL, 1), // 2010 |
| 64 |
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011 |
| 65 |
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 2012 |
| 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:$A$5', NULL, 4), // Q1 to Q4 |
| 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!$B$2:$B$5', NULL, 4), |
| 86 |
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4), |
| 87 |
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4), |
| 88 |
); |
| 89 |
|
| 90 |
// Build the dataseries |
| 91 |
$series = new PHPExcel_Chart_DataSeries( |
| 92 |
PHPExcel_Chart_DataSeries::TYPE_AREACHART, // plotType |
| 93 |
PHPExcel_Chart_DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping |
| 94 |
range(0, count($dataSeriesValues)-1), // plotOrder |
| 95 |
$dataSeriesLabels, // plotLabel |
| 96 |
$xAxisTickValues, // plotCategory |
| 97 |
$dataSeriesValues // plotValues |
| 98 |
); |
| 99 |
|
| 100 |
// Set the series in the plot area |
| 101 |
$plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series)); |
| 102 |
// Set the chart legend |
| 103 |
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, NULL, false); |
| 104 |
|
| 105 |
$title = new PHPExcel_Chart_Title('Test %age-Stacked Area Chart'); |
| 106 |
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); |
| 107 |
|
| 108 |
|
| 109 |
// Create the chart |
| 110 |
$chart = new PHPExcel_Chart( |
| 111 |
'chart1', // name |
| 112 |
$title, // title |
| 113 |
$legend, // legend |
| 114 |
$plotArea, // plotArea |
| 115 |
true, // plotVisibleOnly |
| 116 |
0, // displayBlanksAs |
| 117 |
NULL, // xAxisLabel |
| 118 |
$yAxisLabel // yAxisLabel |
| 119 |
); |
| 120 |
|
| 121 |
// Set the position where the chart should appear in the worksheet |
| 122 |
$chart->setTopLeftPosition('A7'); |
| 123 |
$chart->setBottomRightPosition('H20'); |
| 124 |
|
| 125 |
// Add the chart to the worksheet |
| 126 |
$objWorksheet->addChart($chart); |
| 127 |
|
| 128 |
|
| 129 |
// Save Excel 2007 file |
| 130 |
echo date('H:i:s') , " Write to Excel2007 format" , EOL; |
| 131 |
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); |
| 132 |
$objWriter->setIncludeCharts(TRUE); |
| 133 |
$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); |
| 134 |
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; |
| 135 |
|
| 136 |
|
| 137 |
// Echo memory peak usage |
| 138 |
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; |
| 139 |
|
| 140 |
// Echo done |
| 141 |
echo date('H:i:s') , " Done writing file" , EOL; |
| 142 |
echo 'File has been created in ' , getcwd() , EOL; |
| 143 |
|