Topics: Developer Forum
May 24, 2012 at 4:20 PM


       I downloaded the new version, and I try to use PHPExcel_Chart. But there isn't exemple. 

I have some problems to generate a graph in my Excel with PHPExcel 1.7.7

      Can you give me an exemple for create a simple graph ?

Thank you.

May 24, 2012 at 8:19 PM

I'm trying to write up some documentation for that now, although the code doesn't have particularly friendly methods for generating charts yet: the focus so far has been on the reading and writing rather than direct creation

May 25, 2012 at 6:27 AM

Ok thank you for your answer.

Can you reply to this discussion when the documentation will be ready ?

Jun 1, 2012 at 9:20 AM


       When I want create a Chart, the library PHPExcel 1.7.7 doesn't return an error. But when I open my file.xlsx, I have the following error "Excel has encountered an unreadable content".

      This error appears only when I put a chart in my file.xlsx. This is my code perhaps I have forgot something.

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle(utf8_encode('try graphique'));
$ws = $objPHPExcel->getActiveSheet();

$ws->setCellValue('A1', 10);
$ws->setCellValue('A2', 20);
$ws->setCellValue('A3', 30);

$dataValue1 = new PHPExcel_Chart_DataSeriesValues(PHPExcel_Chart_DataSeries::TYPE_BARCHART, null, null, 3, array('10', '20', '30'));
$dataValue2 = new PHPExcel_Chart_DataSeriesValues(PHPExcel_Chart_DataSeries::TYPE_BARCHART, null, null, 3, array('15', '25', '35'));
$area1 = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART, null, array(), array('label plot1', 'label plot2', 'label plot3'), array('cat1', 'cat2', 'cat3'), array($dataValue1), null, null);
$area2 = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART, null, array(), array('label plot1', 'label plot2', 'label plot3'), array('cat1', 'cat2', 'cat3'), array($dataValue2), null, null);

$plot_area = new PHPExcel_Chart_PlotArea(null);
$plot_area->setPlotSeries(array($area1, $area2));
//$title = new PHPExcel_Chart_Title('sss');

$graph = new PHPExcel_Chart( 'Graph1Test', null, null, $plot_area );

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

1/ Can you help me ?

2/ You told me "the focus so far has been on the reading and writing rather than direct creation".
Ok but for the moment it's possible or not to create a chart ?

Thank you.

Jun 1, 2012 at 10:30 AM
Edited Jun 1, 2012 at 10:33 AM

Missed this thread until now. I've also spent the last few days playing with creating charts from scratch. Just on my way out the office but in case it helps here's some demo code that definitely works well.

  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="graphdemo.xlsx"');
  header('Cache-Control: max-age=0');

  $sheet = $workbook->getActiveSheet();

  $data = array('10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%');
  $row = 1;
  foreach($data as $point) {
    $sheet->setCellValueByColumnAndRow(0, $row++, $point);

  $data = array(12, 56, 89, 45, 42, 22, 15, 8, 2, 0);
  $row = 1;
  foreach($data as $point) {
    $sheet->setCellValueByColumnAndRow(1, $row++, $point);

  $values = new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$1:$B$10');
  $categories = new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$1:$A$10');

  $series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
    PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  // plotGrouping
    array(0),                                       // plotOrder
    array(),                                        // plotLabel
    array($categories),                             // plotCategory
    array($values)                                  // plotValues

  $layout = new PHPExcel_Chart_Layout();
  $plotarea = new PHPExcel_Chart_PlotArea($layout, array($series));

  $chart = new PHPExcel_Chart('sample', null, null, $plotarea);



  $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');

Jun 1, 2012 at 3:22 PM

Thank you for reply


I tried with your code and that's work. I didn't understand that in the construct of PHPExcel_Chart_DataSeries object, the parameter $plotCategory  was an array of PHPExcel_Chart_DataSeriesValues.

    In my opinion, you may indicate in the documentation of the construct of the object  PHPExcel_Chart_DataSeries  the parameter's type.


Thank you for your help.

Jun 17, 2012 at 6:29 AM

If anyone stumbles across this then whilst I still don't claim to understand this completely I have posted a slightly clearer demo here:

Jun 17, 2012 at 10:14 AM
Edited Jun 17, 2012 at 10:17 AM

I'm trying to find the time to get the documentation together, and to build up the docblocks in the code so that the API is correct. There are also still missing methods to add components like title, legend and axis labels that I'm adding. Please be patient, as we're also in the middle of transferring all the code to github at the moment.

Jun 17, 2012 at 10:16 AM

Building up the phpdoc comments would be a good start. I'm really impressed with what you've got so far with charts (well the whole library I guess) Let me know if I can help with anything useful.

Jun 17, 2012 at 10:22 AM
Edited Jun 17, 2012 at 10:27 AM

Thanks for the offer of help.


The missing setter methods is my first priority... when reading from a file, everything is set in the constructor, so I overlooked them as a method needed for adding those features manually... my original tests only modified existing titles, etc. There's also a few bugs that have been highlighted already in the issues list, that I'm trying to resolve.

Docblocks is my second priority, because once they're all correct, then developers can at least read those to figure out how to build a chart.

Documentation will take a long time, and be built slowly because of the myriad options available... charts turned out to be nearly as complicated as worksheets themselves. If anybody wants to start writing up some documentation in advance of that, I'd be more than happy.... even if it's just a few blog posts on how to read charts from workbook file and render them as image files.

Jun 19, 2012 at 6:50 AM

I've added mdryans example code as 33chartcreate.php in /Tests to demonstrate creating a chart, including setting a title with the latest github code. Thanks mdryan for that example.