1
votes

FINAL EDIT - ANSWERED BELOW - ISSUE: GROUPING_TYPE

I am new to PHPExcel and am having an issue when adding a chart via PHPExcel. I am currently only testing the Excel 2007 output version.

The remainder of the code (not included) is working properly, as a simple data file (without the chart code below) is downloadable and readable by Excel 2007 without error, however, when I add a chart (using the copy/pasted code below), upon opening the downloaded file in Excel I get the error Excel found unreadable content in:####. Do you want to recover the contents...

EDIT: Upon recovering the contents, the chart IS STILL PRESENT WITH ALL DATA INTACT.

Here is the code used to generate the chart:

//ADD THE REPORT SUMMARY CHART
$chrtCols = "'Report Summary'!B2:B$rowNum";
$chrtVals = "'Report Summary'!C2:C$rowNum";
$periods = new PHPExcel_Chart_DataSeriesValues('String', $chrtCols, null, $rowNum-1);
$values = new PHPExcel_Chart_DataSeriesValues('Number', $chrtVals, null, $rowNum-1);

$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_LINECHART,      
    PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  
    array(0),                                       
    array(),                                       
    array($periods),                               
    array($values)                                  
  );
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$layout = new PHPExcel_Chart_Layout();
$plotarea = new PHPExcel_Chart_PlotArea($layout, array($series));
$chart = new PHPExcel_Chart('sample', null, null, $plotarea);
$chart->setTopLeftPosition('A1');
$chart->setBottomRightPosition('A18');
$actSheet->addChart($chart);

$rowNum is the last row of data.

Also, I am including the line $objWriter->setIncludeCharts(TRUE); prior to actually writing the file.

The file is also being downloaded using the proper MIME type for the Excel 2007 extension: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, so I do not believe it is a header issue?

Anyone out there that can help? Basically, I just need to get rid of the error upon opening the file in Excel. Any thoughts?

1
Possibly setting a chart direction for Column when you're creating a line chart - Mark Baker
@MarkB: I commented out the $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL); line of code and still receive the same error. Any other thoughts? - IIIOXIII
@MarkB: Also, please see my EDIT. After repairing the file, the chart is available (it was previously improperly sized and hard to locate) and the data is in tact. The main problem I am having now is just getting rid of the "unreadable content" error upon opening the file. Thanks for your help. - IIIOXIII
It's difficult to assess without knowing the data: GROUPING_CLUSTERED should perhaps be GROUPING_STANDARD as you're only working with a single dataseries - Mark Baker
@MarkB: You were right in your last comment here. It had to do with the grouping_type I had set. See my answer below (figured it out and was writing my answer as you were responding). Thanks for your help though. Seems all the other wizards were busy this evening. - IIIOXIII

1 Answers

0
votes

The issue was the GROUPING_TYPE defined in my original code. I changed

PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED

to

PHPExcel_Chart_DataSeries::GROUPING_STANDARD

and viola, all works as expected.

So to anyone else out there copying/pasting code-bits for creating a chart in PHP Excel:

If you are receiving the "unreadable content" error upon opening the file in Excel, (and your headers have been set correctly), be sure you are declaring the proper GROUP_TYPE per the CHART_TYPE you have defined.