From 5a86c55ed4a86125d88c2a384f5c5c0344755268 Mon Sep 17 00:00:00 2001 From: Mark Baker <mark@lange.demon.co.uk> Date: Mon, 18 Jun 2012 21:47:17 +0100 Subject: [PATCH] Example for basic chart creation --- Tests/33chartcreate.php | 118 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Tests/33chartcreate.php diff --git a/Tests/33chartcreate.php b/Tests/33chartcreate.php new file mode 100644 index 0000000..15710d5 --- /dev/null +++ b/Tests/33chartcreate.php @@ -0,0 +1,118 @@ +<?php + +/** Error reporting */ +error_reporting(E_ALL); + +date_default_timezone_set('Europe/London'); + +/** + * PHPExcel + * + * Copyright (C) 2006 - 2012 PHPExcel + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category PHPExcel + * @package PHPExcel + * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel) + * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL + * @version 1.7.7, 2012-05-19 + */ + +/** Include path **/ +set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/'); + +/** PHPExcel */ +include 'PHPExcel.php'; + +$objPHPExcel = new PHPExcel(); +$objWorksheet = $objPHPExcel->getActiveSheet(); +$objWorksheet->fromArray( + array( + array('', 2010, 2011, 2012), + array('Q1', 12, 15, 21), + array('Q2', 56, 73, 86), + array('Q3', 52, 61, 69), + array('Q4', 30, 32, 0), + ) +); + +// Set the Labels for each dataset we want to plot +$labels = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1), // 2010 + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), // 2011 + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', null, 1), // 2012 +); +// Set the X-Axis Labels +$categories = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 +); +// Set the Data values for each dataset we want to plot +$values = array( + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', null, 4), +); + +// Build the dataseries +$series = new PHPExcel_Chart_DataSeries( + PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType + PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping + array(0, 1, 2), // plotOrder + $labels, // plotLabel + $categories, // plotCategory + $values // plotValues +); +// Set additional dataseries parameters +$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL); + +// Set the series in the plot area +$plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); +// Set the chart legend +$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); + +// Create the chart +$chart = new PHPExcel_Chart( + 'chart1', // name + null, // title + $legend, // legend + $plotarea, // plotArea + true, // plotVisibleOnly + 0, // displayBlanksAs + null, // xAxisLabel + null // yAxisLabel +); + +// Set the position where the chart should appear in the worksheet +$chart->setTopLeftPosition('A7'); +$chart->setBottomRightPosition('H20'); + +// Add the chart to the worksheet +$objWorksheet->addChart($chart); + + +// Save Excel 2007 file +echo date('H:i:s') , " Write to Excel2007 format" , PHP_EOL; +$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); +$objWriter->setIncludeCharts(TRUE); +$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); +echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', __FILE__) , PHP_EOL; + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , PHP_EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , PHP_EOL;