From 13f227c4f2b13bcdac274b753d6f091dcdcf8595 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 2 Sep 2012 12:06:48 +0100 Subject: [PATCH] Additional chart creation examples --- Classes/PHPExcel/Reader/Excel2007.php | 13 +- Classes/PHPExcel/Reader/Excel2007/Chart.php | 1 + Classes/PHPExcel/Worksheet.php | 7 +- Classes/PHPExcel/Worksheet/AutoFilter.php | 2 - Classes/PHPExcel/Writer/Excel2007/Style.php | 16 +- .../PHPExcel/Writer/Excel2007/Worksheet.php | 1 - Tests/33chartcreate-area.php | 2 +- Tests/33chartcreate-bar-stacked.php | 147 +++++++++++++++++ Tests/33chartcreate-bar.php | 2 +- Tests/33chartcreate-column-2.php | 156 ++++++++++++++++++ Tests/33chartcreate-column.php | 4 +- Tests/33chartcreate-line.php | 2 +- Tests/runall.php | 2 + 13 files changed, 332 insertions(+), 23 deletions(-) create mode 100644 Tests/33chartcreate-bar-stacked.php create mode 100644 Tests/33chartcreate-column-2.php diff --git a/Classes/PHPExcel/Reader/Excel2007.php b/Classes/PHPExcel/Reader/Excel2007.php index 04416b59..98a10309 100644 --- a/Classes/PHPExcel/Reader/Excel2007.php +++ b/Classes/PHPExcel/Reader/Excel2007.php @@ -697,7 +697,6 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader $dxfs[] = $style; } } - // Cell Styles if ($xmlStyles->cellStyles) { foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) { @@ -1083,7 +1082,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader } } } - + $aKeys = array("sheet", "objects", "scenarios", "formatCells", "formatColumns", "formatRows", "insertColumns", "insertRows", "insertHyperlinks", "deleteColumns", "deleteRows", "selectLockedCells", "sort", "autoFilter", "pivotTables", "selectUnlockedCells"); if (!$this->_readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) { foreach ($aKeys as $key) { @@ -1859,9 +1858,13 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader private static function _readStyle($docStyle, $style) { // format code - if (isset($style->numFmt)) { - $docStyle->getNumberFormat()->setFormatCode($style->numFmt); - } +// if (isset($style->numFmt)) { +// if (isset($style->numFmt['formatCode'])) { +// $docStyle->getNumberFormat()->setFormatCode((string) $style->numFmt['formatCode']); +// } else { + $docStyle->getNumberFormat()->setFormatCode($style->numFmt); +// } +// } // font if (isset($style->font)) { diff --git a/Classes/PHPExcel/Reader/Excel2007/Chart.php b/Classes/PHPExcel/Reader/Excel2007/Chart.php index 4a6981ca..ac697143 100644 --- a/Classes/PHPExcel/Reader/Excel2007/Chart.php +++ b/Classes/PHPExcel/Reader/Excel2007/Chart.php @@ -183,6 +183,7 @@ class PHPExcel_Reader_Excel2007_Chart } } } + $chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel); return $chart; diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index e3bcd06f..fff57cd5 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -1900,7 +1900,12 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable /** * Freeze Pane * - * @param string $pCell Cell (i.e. A1) + * @param string $pCell Cell (i.e. A2) + * Examples: + * A2 will freeze the rows above cell A2 (i.e row 1) + * B1 will freeze the columns to the left of cell B1 (i.e column A) + * B2 will freeze the rows above and to the left of cell A2 + * (i.e row 1 and column A) * @throws Exception * @return PHPExcel_Worksheet */ diff --git a/Classes/PHPExcel/Worksheet/AutoFilter.php b/Classes/PHPExcel/Worksheet/AutoFilter.php index 6b7c8e46..2448d35d 100644 --- a/Classes/PHPExcel/Worksheet/AutoFilter.php +++ b/Classes/PHPExcel/Worksheet/AutoFilter.php @@ -352,8 +352,6 @@ class PHPExcel_Worksheet_AutoFilter */ private static function _filterTestInCustomDataSet($cellValue,$ruleSet) { -var_dump($ruleSet); - $dataSet = $ruleSet['filterRules']; $join = $ruleSet['join']; $customRuleForBlanks = isset($ruleSet['customRuleForBlanks']) ? $ruleSet['customRuleForBlanks'] : FALSE; diff --git a/Classes/PHPExcel/Writer/Excel2007/Style.php b/Classes/PHPExcel/Writer/Excel2007/Style.php index b830f9e9..b980a167 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Style.php +++ b/Classes/PHPExcel/Writer/Excel2007/Style.php @@ -252,14 +252,13 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB()); $objWriter->endElement(); } - - if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_SOLID) { - // bgColor - if ($pFill->getEndColor()->getARGB()) { - $objWriter->startElement('bgColor'); - $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB()); - $objWriter->endElement(); - } + } + if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_NONE) { + // bgColor + if ($pFill->getEndColor()->getARGB()) { + $objWriter->startElement('bgColor'); + $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB()); + $objWriter->endElement(); } } @@ -623,7 +622,6 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa $fill1 = new PHPExcel_Style_Fill(); $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125); $aFills[] = $fill1; - // The remaining fills $aStyles = $this->allStyles($pPHPExcel); foreach ($aStyles as $style) { diff --git a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php index 80603777..038891cf 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php +++ b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php @@ -464,7 +464,6 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_Writ // if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ) == '') { // continue; // } - if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) { // conditionalFormatting $objWriter->startElement('conditionalFormatting'); diff --git a/Tests/33chartcreate-area.php b/Tests/33chartcreate-area.php index 0a00e659..34679c54 100644 --- a/Tests/33chartcreate-area.php +++ b/Tests/33chartcreate-area.php @@ -104,7 +104,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false); -$title = new PHPExcel_Chart_Title('Test Chart'); +$title = new PHPExcel_Chart_Title('Test %age-Stacked Area Chart'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); diff --git a/Tests/33chartcreate-bar-stacked.php b/Tests/33chartcreate-bar-stacked.php new file mode 100644 index 00000000..180985f1 --- /dev/null +++ b/Tests/33chartcreate-bar-stacked.php @@ -0,0 +1,147 @@ +'); + +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 ##VERSION##, ##DATE## + */ + +/** 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 data series we want to plot +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$dataseriesLabels = 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 +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$xAxisTickValues = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 +); +// Set the Data values for each data series we want to plot +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$dataSeriesValues = 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_STACKED, // plotGrouping + range(0, count($dataSeriesValues)-1), // plotOrder + $dataseriesLabels, // plotLabel + $xAxisTickValues, // plotCategory + $dataSeriesValues // plotValues +); +// Set additional dataseries parameters +// Make it a horizontal bar rather than a vertical column graph +$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR); + +// 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); + +$title = new PHPExcel_Chart_Title('Test Chart'); +$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); + + +// Create the chart +$chart = new PHPExcel_Chart( + 'chart1', // name + $title, // title + $legend, // legend + $plotarea, // plotArea + true, // plotVisibleOnly + 0, // displayBlanksAs + null, // xAxisLabel + $yAxisLabel // 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" , 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', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , EOL; +echo 'File has been created in ' , getcwd() , EOL; diff --git a/Tests/33chartcreate-bar.php b/Tests/33chartcreate-bar.php index f80649d3..866fa696 100644 --- a/Tests/33chartcreate-bar.php +++ b/Tests/33chartcreate-bar.php @@ -107,7 +107,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); -$title = new PHPExcel_Chart_Title('Test Chart'); +$title = new PHPExcel_Chart_Title('Test Bar Chart'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); diff --git a/Tests/33chartcreate-column-2.php b/Tests/33chartcreate-column-2.php new file mode 100644 index 00000000..7f1e1a25 --- /dev/null +++ b/Tests/33chartcreate-column-2.php @@ -0,0 +1,156 @@ +'); + +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 ##VERSION##, ##DATE## + */ + +/** 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('', '', 'Budget', 'Forecast', 'Actual'), + array('2010', 'Q1', 47, 44, 43 ), + array('', 'Q2', 56, 53, 50 ), + array('', 'Q3', 52, 46, 45 ), + array('', 'Q4', 45, 40, 40 ), + array('2011', 'Q1', 51, 42, 46 ), + array('', 'Q2', 53, 58, 56 ), + array('', 'Q3', 64, 66, 69 ), + array('', 'Q4', 54, 55, 56 ), + array('2012', 'Q1', 49, 52, 58 ), + array('', 'Q2', 68, 73, 86 ), + array('', 'Q3', 72, 78, 0 ), + array('', 'Q4', 50, 60, 0 ), + ) +); + +// Set the Labels for each data series we want to plot +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$dataseriesLabels = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), // 'Budget' + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', null, 1), // 'Forecast' + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', null, 1), // 'Actual' +); +// Set the X-Axis Labels +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$xAxisTickValues = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$B$13', null, 12), // Q1 to Q4 for 2010 to 2012 +); +// Set the Data values for each data series we want to plot +// Datatype +// Cell reference for data +// Format Code +// Number of datapoints in series +// Data values +// Data Marker +$dataSeriesValues = array( + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', null, 12), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', null, 12), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$13', null, 12), +); + +// Build the dataseries +$series = new PHPExcel_Chart_DataSeries( + PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType + PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping + range(0, count($dataSeriesValues)-1), // plotOrder + $dataseriesLabels, // plotLabel + $xAxisTickValues, // plotCategory + $dataSeriesValues // plotValues +); +// Set additional dataseries parameters +// Make it a vertical column rather than a horizontal bar graph +$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_BOTTOM, null, false); + +$title = new PHPExcel_Chart_Title('Test Grouped Column Chart'); +$xAxisLabel = new PHPExcel_Chart_Title('Financial Period'); +$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); + + +// Create the chart +$chart = new PHPExcel_Chart( + 'chart1', // name + $title, // title + $legend, // legend + $plotarea, // plotArea + true, // plotVisibleOnly + 0, // displayBlanksAs + $xAxisLabel, // xAxisLabel + $yAxisLabel // yAxisLabel +); + +// Set the position where the chart should appear in the worksheet +$chart->setTopLeftPosition('G2'); +$chart->setBottomRightPosition('P20'); + +// Add the chart to the worksheet +$objWorksheet->addChart($chart); + + +// Save Excel 2007 file +echo date('H:i:s') , " Write to Excel2007 format" , 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', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , EOL; +echo 'File has been created in ' , getcwd() , EOL; diff --git a/Tests/33chartcreate-column.php b/Tests/33chartcreate-column.php index bc364d8c..768c06dd 100644 --- a/Tests/33chartcreate-column.php +++ b/Tests/33chartcreate-column.php @@ -92,7 +92,7 @@ $dataSeriesValues = array( // Build the dataseries $series = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType - PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping + PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping range(0, count($dataSeriesValues)-1), // plotOrder $dataseriesLabels, // plotLabel $xAxisTickValues, // plotCategory @@ -107,7 +107,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); -$title = new PHPExcel_Chart_Title('Test Chart'); +$title = new PHPExcel_Chart_Title('Test Column Chart'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); diff --git a/Tests/33chartcreate-line.php b/Tests/33chartcreate-line.php index def7b3af..13722706 100644 --- a/Tests/33chartcreate-line.php +++ b/Tests/33chartcreate-line.php @@ -104,7 +104,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false); -$title = new PHPExcel_Chart_Title('Test Chart'); +$title = new PHPExcel_Chart_Title('Test Stacked Line Chart'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)'); diff --git a/Tests/runall.php b/Tests/runall.php index d36cf81d..b0ac8a84 100644 --- a/Tests/runall.php +++ b/Tests/runall.php @@ -74,7 +74,9 @@ $aTests = array( , '32chartreadwrite.php' , '33chartcreate-area.php' , '33chartcreate-bar.php' + , '33chartcreate-bar-stacked.php' , '33chartcreate-column.php' + , '33chartcreate-column-2.php' , '33chartcreate-line.php' , '34chartupdate.php' , 'OOCalcReader.php'