Additional chart creation examples

This commit is contained in:
Mark Baker 2012-09-02 12:06:48 +01:00
parent 6f98d9dcb7
commit 13f227c4f2
13 changed files with 332 additions and 23 deletions

View File

@ -697,7 +697,6 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
$dxfs[] = $style; $dxfs[] = $style;
} }
} }
// Cell Styles // Cell Styles
if ($xmlStyles->cellStyles) { if ($xmlStyles->cellStyles) {
foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) { foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) {
@ -1859,9 +1858,13 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
private static function _readStyle($docStyle, $style) { private static function _readStyle($docStyle, $style) {
// format code // format code
if (isset($style->numFmt)) { // if (isset($style->numFmt)) {
// if (isset($style->numFmt['formatCode'])) {
// $docStyle->getNumberFormat()->setFormatCode((string) $style->numFmt['formatCode']);
// } else {
$docStyle->getNumberFormat()->setFormatCode($style->numFmt); $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
} // }
// }
// font // font
if (isset($style->font)) { if (isset($style->font)) {

View File

@ -183,6 +183,7 @@ class PHPExcel_Reader_Excel2007_Chart
} }
} }
} }
$chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel); $chart = new PHPExcel_Chart($chartName,$title,$legend,$plotArea,$plotVisOnly,$dispBlanksAs,$XaxisLabel,$YaxisLabel);
return $chart; return $chart;

View File

@ -1900,7 +1900,12 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
/** /**
* Freeze Pane * 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 * @throws Exception
* @return PHPExcel_Worksheet * @return PHPExcel_Worksheet
*/ */

View File

@ -352,8 +352,6 @@ class PHPExcel_Worksheet_AutoFilter
*/ */
private static function _filterTestInCustomDataSet($cellValue,$ruleSet) private static function _filterTestInCustomDataSet($cellValue,$ruleSet)
{ {
var_dump($ruleSet);
$dataSet = $ruleSet['filterRules']; $dataSet = $ruleSet['filterRules'];
$join = $ruleSet['join']; $join = $ruleSet['join'];
$customRuleForBlanks = isset($ruleSet['customRuleForBlanks']) ? $ruleSet['customRuleForBlanks'] : FALSE; $customRuleForBlanks = isset($ruleSet['customRuleForBlanks']) ? $ruleSet['customRuleForBlanks'] : FALSE;

View File

@ -252,8 +252,8 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa
$objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB()); $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
$objWriter->endElement(); $objWriter->endElement();
} }
}
if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_SOLID) { if ($pFill->getFillType() !== PHPExcel_Style_Fill::FILL_NONE) {
// bgColor // bgColor
if ($pFill->getEndColor()->getARGB()) { if ($pFill->getEndColor()->getARGB()) {
$objWriter->startElement('bgColor'); $objWriter->startElement('bgColor');
@ -261,7 +261,6 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa
$objWriter->endElement(); $objWriter->endElement();
} }
} }
}
$objWriter->endElement(); $objWriter->endElement();
@ -623,7 +622,6 @@ class PHPExcel_Writer_Excel2007_Style extends PHPExcel_Writer_Excel2007_WriterPa
$fill1 = new PHPExcel_Style_Fill(); $fill1 = new PHPExcel_Style_Fill();
$fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125); $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
$aFills[] = $fill1; $aFills[] = $fill1;
// The remaining fills // The remaining fills
$aStyles = $this->allStyles($pPHPExcel); $aStyles = $this->allStyles($pPHPExcel);
foreach ($aStyles as $style) { foreach ($aStyles as $style) {

View File

@ -464,7 +464,6 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_Writ
// if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ) == '') { // if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ) == '') {
// continue; // continue;
// } // }
if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) { if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) {
// conditionalFormatting // conditionalFormatting
$objWriter->startElement('conditionalFormatting'); $objWriter->startElement('conditionalFormatting');

View File

@ -104,7 +104,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
// Set the chart legend // Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false); $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)'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

View File

@ -0,0 +1,147 @@
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
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;

View File

@ -107,7 +107,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
// Set the chart legend // Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); $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)'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

View File

@ -0,0 +1,156 @@
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
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;

View File

@ -92,7 +92,7 @@ $dataSeriesValues = array(
// Build the dataseries // Build the dataseries
$series = new PHPExcel_Chart_DataSeries( $series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues)-1), // plotOrder range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel $dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory $xAxisTickValues, // plotCategory
@ -107,7 +107,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
// Set the chart legend // Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); $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)'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

View File

@ -104,7 +104,7 @@ $plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
// Set the chart legend // Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false); $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)'); $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

View File

@ -74,7 +74,9 @@ $aTests = array(
, '32chartreadwrite.php' , '32chartreadwrite.php'
, '33chartcreate-area.php' , '33chartcreate-area.php'
, '33chartcreate-bar.php' , '33chartcreate-bar.php'
, '33chartcreate-bar-stacked.php'
, '33chartcreate-column.php' , '33chartcreate-column.php'
, '33chartcreate-column-2.php'
, '33chartcreate-line.php' , '33chartcreate-line.php'
, '34chartupdate.php' , '34chartupdate.php'
, 'OOCalcReader.php' , 'OOCalcReader.php'