1

The library in question is PHPExcel 1.7.7

I've used sample code found in a thread at codeplex to create charts with PHPExcel. However, the sample code presented in the forums only deal with two columns of data, and I'm looking to expand the number of columns that are displayed in the chart. The third set of data (column D and beyond) are not being displayed no matter what I try to do with it.

There isn't a problem with the labels/categories strings - so there's no collision with any reserved names (even though they're strings), and I returned the plotValues to see if anything got lost, and I still see that the defined columns are shown in there, but can't figure out why the third column is not shown in the chart.

$excel->getActiveSheet()->fromArray(array(
    $header ,
    array('Central',   12,   15 , 12 ),
    array('Northeast',   56,   73 , 10 ),
    array('Southeast',   52,   61 , 33 ),
    array('Western',   30,   32 , 55 ),
));


$labels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$B$1', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$C$1', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$D$1', null, 1),
);

$categories = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$A$2:$A$5', null, 4),
);

// this is where the problem arises - the third DataSeriesValues is not being displayed
$values = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$B$2:$B$5', null, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$C$2:$C$5', null, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$D$2:$D$5', null, 4),
/*
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$C$2:$C$5', null, 5),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$D$2:$D$5', null, 5)
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$E$2:$E$5', null, 5),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$F$2:$F$5', null, 5),
*/
);

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

$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);


$chart = new PHPExcel_Chart(
    'chart1',                                       // name
    null,                                           // title
    $legend,                                        // legend
    $plotarea,                                      // plotArea
    true,                                           // plotVisibleOnly
    0,                                              // displayBlanksAs
    null,                                           // xAxisLabel
    null                                            // yAxisLabel
);

$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');
$excel->getActiveSheet()->addChart($chart);
$excel->getActiveSheet()->setTitle("Summary");
Duniyadnd
  • 4,013
  • 1
  • 22
  • 29
  • 2
    Have you set the plotOrder to include other plots i.e. array(0, 1, 2) ? – Robbie Oct 11 '12 at 02:00
  • @Robbie - that did the trick - thank you - didn't know what plotOrder was meant for. If you post this as an answer - I'll mark you as correct answer. If not, then Mark Baker will get the credit. – Duniyadnd Oct 11 '12 at 10:38
  • Thanks. Set Mark's as correct, no point having two answers saying the same thing and he explained it better! – Robbie Oct 11 '12 at 11:24

1 Answers1

2

As Robbie points out: when you set the plotOrder value in creating your series, you're only telling the chart to use plots 0 and 1

array(0, 1),

not plots 0, 1 and 2

array(0, 1, 2),

I'd be inclined to use

range(0, count($values)-1),

to ensure that all the data series (no matter how many) are included

Mark Baker
  • 209,507
  • 32
  • 346
  • 385