Angel Angel - 2 months ago 29
PHP Question

CodeIgniter + PHPExcel: Error when try to create a chart

I have to develop a program which consists of creating new Excel worksheets containing different cells. The next step would be to generate a vertical bar chart to display various data retrieved from data cells.

I'm using the "33chartcreate-column" example, given by PHPExcel, to understand how the libraty works with codeigniter framework. But when I try to create the chart by calling the setIncludeCharts method I get the error:
"Fatal error: Call to a member function cellExists() on a non-object in *\application\third_party\PHPExcel\Calculation.php on line 3327"

$this->load->library('excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('Nombre de la hoja');

$this->excel->getActiveSheet()->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),
)
);

$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_STANDARD, // 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_RIGHT, NULL,false);
$title = new PHPExcel_Chart_Title('Test Column 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
);

$this->excel->getActiveSheet()->addChart($chart);
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
<b>$objWriter->setIncludeCharts(TRUE);</b> //line 1327
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

Answer

I have found my error:

$this->excel->getActiveSheet()->setTitle('Nombre de la hoja');

I can't call setTitle method after I call the setActiveSheetIndex method.