Horizontal Stacked Barchart

Topics: User Forum
Oct 15, 2012 at 8:51 AM

Hi there.

 

I'm trying to create a horizontal stacked barchart. The labels are in Column 'A', the Values in Column 'D'.

I use the following code to create the $series array:

$series = array();
for ($r = 2; $r <= 7; $r++)
{
    $label = new PHPExcel_Chart_DataSeriesValues('String', 'A'.$r,null,1);
    $value = new PHPExcel_Chart_DataSeriesValues('Number', 'D'.$r,null,1);
    $series[count($series)] = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART 
, PHPExcel_Chart_DataSeries::GROUPING_STACKED // plotGrouping , array(0) // plotOrder , array($label) // plotLabel , null // plotCategory , array($value) // plotValues );
}

 Unfortunately the resulting plot is a) vertical and not horizontal, and b) every series has the same color.

 

What is the correct way to create a horizontal stacked barchart?

 

 

mfg Tobias

Coordinator
Oct 15, 2012 at 10:00 AM

There is an example setting bar direction provided in /Tests

$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);

Can't understand how you're getting every series with the same colour though.

Oct 15, 2012 at 10:50 AM

Ah, yes, that puts it horizontally, thanks.

Is there a way to set the colors manually?

 

 

mfg Tobias

Coordinator
Oct 15, 2012 at 12:46 PM

At the moment there isn't a way to set the colours manually: and I can't understand how it is setting all the colours to be identical.... the code written to the Excel xml chart file for every single chart says explicitly to use different default colours using c:varyColors

Can you post a more complete version of your code so that I can run some tests

Oct 15, 2012 at 1:04 PM

Sure:

$series = array();
for ($r = 11; $r <= 16; $r++)
{
    $label = new PHPExcel_Chart_DataSeriesValues('String', 'A'.$r,null,1);
    $value = new PHPExcel_Chart_DataSeriesValues('Number', 'D'.$r,null,1);
    $series[count($series)] = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART      
                                                           , PHPExcel_Chart_DataSeries::GROUPING_STACKED   
                                                           , array(0)                                      
                                                           , array($label)                                 
                                                           , null                                          
                                                           , array($value)                                 
                                                           );
    $series[count($series)-1]->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);
}

$label = new PHPExcel_Chart_DataSeriesValues('String', 'A4',null,1);
$value = new PHPExcel_Chart_DataSeriesValues('Number', 'G4',null,1);
$series[count($series)] = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_BARCHART        
                                                        , PHPExcel_Chart_DataSeries::GROUPING_STACKED    
                                                        , array(0)                                       
                                                        , array($label)                                 
                                                        , null                                          
                                                        , array($value)                                  
                                                        );
$series[count($series)-1]->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);


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

$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$chart = new PHPExcel_Chart( $title
                           , null
                           , $legend
                           , $plotarea
                           , true
                           , 0
                           , null
                           , null
                           );
$chart->setTopLeftPosition('A'.($mr+2));
$chart->setBottomRightPosition('K'.($mr+22));
$sheet->addChart($chart);


Here's a screenshot of the result: http://user.math.uzh.ch/berner/files/bar_graph.png 

 

 

Thanks.

 

mfg Tobias