Example of creating Pie and Donut charts

Bugfix: Discard single cell merge ranges when reading (stupid that Excel allows them in the first place)
Bugfix: Discard hidden autoFilter named ranges
This commit is contained in:
Mark Baker 2012-09-20 00:26:49 +01:00
parent d7e4aaf80b
commit 660e7e723b
7 changed files with 468 additions and 11 deletions

View File

@ -1206,9 +1206,12 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->_readDataOnly) { if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->_readDataOnly) {
foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) { foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) {
$mergeRef = (string) $mergeCell["ref"];
if (strpos($mergeRef,':') !== FALSE) {
$docSheet->mergeCells((string) $mergeCell["ref"]); $docSheet->mergeCells((string) $mergeCell["ref"]);
} }
} }
}
if ($xmlSheet && $xmlSheet->pageMargins && !$this->_readDataOnly) { if ($xmlSheet && $xmlSheet->pageMargins && !$this->_readDataOnly) {
$docPageMargins = $docSheet->getPageMargins(); $docPageMargins = $docSheet->getPageMargins();
@ -1654,7 +1657,7 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
} }
// Valid range? // Valid range?
if (stripos((string)$definedName, '#REF!') !== false || $extractedRange == '') { if (stripos((string)$definedName, '#REF!') !== FALSE || $extractedRange == '') {
continue; continue;
} }
@ -1664,7 +1667,9 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader
switch ((string)$definedName['name']) { switch ((string)$definedName['name']) {
case '_xlnm._FilterDatabase': case '_xlnm._FilterDatabase':
if ((string)$definedName['hidden'] !== '1') {
$docSheet->getAutoFilter()->setRange($extractedRange); $docSheet->getAutoFilter()->setRange($extractedRange);
}
break; break;
case '_xlnm.Print_Titles': case '_xlnm.Print_Titles':

View File

@ -4275,7 +4275,8 @@ class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader
if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) { if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
$cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData); $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) { foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
if ($this->_includeCellRangeFiltered($cellRangeAddress)) { if ((strpos($cellRangeAddress,':') !== FALSE) &&
($this->_includeCellRangeFiltered($cellRangeAddress))) {
$this->_phpSheet->mergeCells($cellRangeAddress); $this->_phpSheet->mergeCells($cellRangeAddress);
} }
} }

View File

@ -870,9 +870,11 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
// Handle Merged Cells in this worksheet // Handle Merged Cells in this worksheet
if (isset($sheet->MergedRegions)) { if (isset($sheet->MergedRegions)) {
foreach($sheet->MergedRegions->Merge as $mergeCells) { foreach($sheet->MergedRegions->Merge as $mergeCells) {
if (strpos($mergeCells,':') !== FALSE) {
$objPHPExcel->getActiveSheet()->mergeCells($mergeCells); $objPHPExcel->getActiveSheet()->mergeCells($mergeCells);
} }
} }
}
$worksheetID++; $worksheetID++;
} }
@ -901,7 +903,8 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader
} }
private static function _parseBorderAttributes($borderAttributes) { private static function _parseBorderAttributes($borderAttributes)
{
$styleArray = array(); $styleArray = array();
if (isset($borderAttributes["Color"])) { if (isset($borderAttributes["Color"])) {

View File

@ -513,10 +513,15 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
} }
// echo '<b>'.$columnID.$rowID.'</b><br />'; // echo '<b>'.$columnID.$rowID.'</b><br />';
if ($cellData->children) {
$cellDataText = $cellData->children($namespacesContent['text']); $cellDataText = $cellData->children($namespacesContent['text']);
$cellDataOffice = $cellData->children($namespacesContent['office']); $cellDataOffice = $cellData->children($namespacesContent['office']);
$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']); $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
$cellDataTableAttributes = $cellData->attributes($namespacesContent['table']); $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
} else {
$cellDataText = '';
$cellDataOffice = $cellDataOfficeAttributes = $cellDataTableAttributes = array();
}
// echo 'Office Attributes: '; // echo 'Office Attributes: ';
// print_r($cellDataOfficeAttributes); // print_r($cellDataOfficeAttributes);

View File

@ -0,0 +1,222 @@
<?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
$dataseriesLabels1 = 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
$xAxisTickValues1 = 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
$dataSeriesValues1 = 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
$series1 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_AREACHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
range(0, count($dataSeriesValues1)-1), // plotOrder
$dataseriesLabels1, // plotLabel
$xAxisTickValues1, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set the series in the plot area
$plotarea1 = new PHPExcel_Chart_PlotArea(null, array($series1));
// Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false);
$title1 = new PHPExcel_Chart_Title('Test %age-Stacked Area Chart');
$yAxisLabel1 = new PHPExcel_Chart_Title('Value ($k)');
// Create the chart
$chart1 = new PHPExcel_Chart(
'chart1', // name
$title1, // title
$legend1, // legend
$plotarea1, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
$yAxisLabel1 // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart1->setTopLeftPosition('A7');
$chart1->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart1);
// 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
$dataseriesLabels2 = 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
$xAxisTickValues2 = 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
$dataSeriesValues2 = 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
$series2 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues2)-1), // plotOrder
$dataseriesLabels2, // plotLabel
$xAxisTickValues2, // plotCategory
$dataSeriesValues2 // plotValues
);
// Set additional dataseries parameters
// Make it a vertical column rather than a horizontal bar graph
$series2->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
// Set the series in the plot area
$plotarea2 = new PHPExcel_Chart_PlotArea(null, array($series2));
// Set the chart legend
$legend2 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$title2 = new PHPExcel_Chart_Title('Test Column Chart');
$yAxisLabel2 = new PHPExcel_Chart_Title('Value ($k)');
// Create the chart
$chart2 = new PHPExcel_Chart(
'chart1', // name
$title2, // title
$legend2, // legend
$plotarea2, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
$yAxisLabel2 // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart2->setTopLeftPosition('I7');
$chart2->setBottomRightPosition('P20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart2);
// 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;

217
Tests/33chartcreate-pie.php Normal file
View File

@ -0,0 +1,217 @@
<?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
$dataseriesLabels1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), // 2011
);
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues1 = 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
$dataSeriesValues1 = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4),
);
// Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues1)-1), // plotOrder
$dataseriesLabels1, // plotLabel
$xAxisTickValues1, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set up a layout object for the Pie chart
$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE);
$layout1->setShowPercent(TRUE);
// Set the series in the plot area
$plotarea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
// Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$title1 = new PHPExcel_Chart_Title('Test Pie Chart');
// Create the chart
$chart1 = new PHPExcel_Chart(
'chart1', // name
$title1, // title
$legend1, // legend
$plotarea1, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel - Pie charts don't have a Y-Axis
);
// Set the position where the chart should appear in the worksheet
$chart1->setTopLeftPosition('A7');
$chart1->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart1);
// 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
$dataseriesLabels2 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), // 2011
);
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues2 = 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
$dataSeriesValues2 = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4),
);
// Build the dataseries
$series2 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_DONUTCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues2)-1), // plotOrder
$dataseriesLabels2, // plotLabel
$xAxisTickValues2, // plotCategory
$dataSeriesValues2 // plotValues
);
// Set up a layout object for the Pie chart
$layout2 = new PHPExcel_Chart_Layout();
$layout2->setShowVal(TRUE);
$layout2->setShowCatName(TRUE);
// Set the series in the plot area
$plotarea2 = new PHPExcel_Chart_PlotArea($layout2, array($series2));
$title2 = new PHPExcel_Chart_Title('Test Donut Chart');
// Create the chart
$chart2 = new PHPExcel_Chart(
'chart1', // name
$title2, // title
NULL, // legend
$plotarea2, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel - Like Pie charts, Donut charts don't have a Y-Axis
);
// Set the position where the chart should appear in the worksheet
$chart2->setTopLeftPosition('I7');
$chart2->setBottomRightPosition('P20');
// Add the chart to the worksheet
$objWorksheet->addChart($chart2);
// 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

@ -116,6 +116,10 @@ Fixed in develop branch:
- Bugfix: (MBaker) Work item 18425 - Problems with $_activeSheetIndex when decreased below 0. - Bugfix: (MBaker) Work item 18425 - Problems with $_activeSheetIndex when decreased below 0.
- Bugfix: (MBaker) Work item 18597 - PHPExcel_CachedObjectStorage_SQLite3::cacheMethodIsAvailable() uses class_exists - autoloader throws error - Bugfix: (MBaker) Work item 18597 - PHPExcel_CachedObjectStorage_SQLite3::cacheMethodIsAvailable() uses class_exists - autoloader throws error
- Bugfix: (MBaker) Work item 18598 - Cannot access private property PHPExcel_CachedObjectStorageFactory::$_cacheStorageMethod - Bugfix: (MBaker) Work item 18598 - Cannot access private property PHPExcel_CachedObjectStorageFactory::$_cacheStorageMethod
- Bugfix: (MBaker) Work item 18397 - Data titles for charts
PHPExcel_Chart_Layout now has methods for getting/setting switches for displaying/hiding chart data labels
- Bugfix: (MBaker) Discard single cell merge ranges when reading (stupid that Excel allows them in the first place)
- Bugfix: (MBaker) Discard hidden autoFilter named ranges
2012-05-19 (v1.7.7): 2012-05-19 (v1.7.7):