Domanda

I used PHPExcel to generate stacked bar chart all things are done but bar chart with multi level(multi color) is not working correctly, bellow is my code.

     $workbook = new PHPExcel();
    $sheet = $workbook->getActiveSheet();
    $sheet->fromArray(  
            array(
                array('Courses','A','B','C','D'),
                array('PHP','13','7','9','3'),  
                array('JAVA','10','5','11','8'),  
                array('ASP.NET','11','2','4','14'),  
                array('C#','6','8','6','4'),
                )  
        );
    $labels = array(
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1),
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), 
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', null, 1), 
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', null, 1), 
    );
    $categories = array(
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4),   
    );
    $values = 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),  
      new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$5', null, 4),  
    );
    $series = new PHPExcel_Chart_DataSeries(
      PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,     // plotType
      PHPExcel_Chart_DataSeries::GROUPING_STACKED,  // plotGrouping
      array(0,1),                                     // plotOrder
      $labels,                                        // plotLabel
      $categories,                                    // plotCategory
      $values                                         // plotValues
    );  

    $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
    $layout1 = new PHPExcel_Chart_Layout();    // Create object of chart layout to set data label 
    $layout1->setShowVal(TRUE);                   
    $layout1->setManual3dAlign(true);
    $layout1->setXRotation(20);
    $layout1->setYRotation(20);
    $layout1->setPerspective(15);                                              
    $layout1->setRightAngleAxes(TRUE);  
    $plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series));
    $title    = new PHPExcel_Chart_Title($this->lang->line('co_report'), null);  
    $legend   = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
    $xTitle   = new PHPExcel_Chart_Title($this->lang->line('course_names'));
    $yTitle   = new PHPExcel_Chart_Title($this->lang->line('no_std'));
    $chart    = new PHPExcel_Chart(
      'chart1',                                       // name
      $title,                                         // title
      $legend,                                        // legend 
      $plotarea,                                      // plotArea
      true,                                           // plotVisibleOnly
      0,                                              // displayBlanksAs
      $xTitle,                                        // xAxisLabel
      $yTitle                                         // yAxisLabel
    );                      
    $chart->setTopLeftPosition('B7');
    $chart->setBottomRightPosition('K25');
    $sheet->addChart($chart);
    $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
    $writer->setIncludeCharts(TRUE);
    $writer->save('mobasher5.xlsx');

and the output of the above code is as bellow. as you see the D and E level of bar is not generated

but what i need is the bellow result. but the real one should be like this show all the levels in bar

È stato utile?

Soluzione

Change array(0,1) to array(0,1,2,3) as value of so-commented "plotOrder" argument:

$series = new PHPExcel_Chart_DataSeries(
      PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,     // plotType
      PHPExcel_Chart_DataSeries::GROUPING_STACKED,  // plotGrouping
      array(0,1,2,3),                                     // plotOrder
      $labels,                                        // plotLabel
      $categories,                                    // plotCategory
      $values                                         // plotValues
    );  

I've just checked - it works for me (I'm using PHPExcel v1.8.0).

Altri suggerimenti

I Got Solution,This will Compatible with phpExcel 2.0 Also

<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
/** PHPExcel */
include 'PHPExcel.php';
$workbook = new PHPExcel();
    $sheet = $workbook->getActiveSheet();
    $sheet->fromArray(  
            array(
                array('Courses','A','B','C','D'),
                array('PHP','130','170','90','30'),  
                array('JAVA','100','50','110','80'),  
                array('ASP.NET','110','200','40','140'),  
                array('C#','60','80','60','40'),
                array('Python','120','130','150','100'),
                array('Perl','160','180','160','140'),
                )  
        );
    $labels = array(
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1),
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1), 
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', null, 1), 
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', null, 1), 
    );
    $categories = array(
      new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$7', null, 6),   
    );
    $values = array(
      new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$7', null, 4),
      new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$7', null, 4),  
      new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$7', null, 4),  
      new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$7', null, 4),  
    );
    $series = new PHPExcel_Chart_DataSeries(
      PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,     // plotType
      PHPExcel_Chart_DataSeries::GROUPING_STACKED,  // plotGrouping
      array(0,1,2,3),                                     // plotOrder
      $labels,                                        // plotLabel
      $categories,                                    // plotCategory
      $values                                         // plotValues
    );  

    $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
    $layout1 = new PHPExcel_Chart_Layout();    // Create object of chart layout to set data label 
/*    $layout1->setShowVal(TRUE);                   
    $layout1->setManual3dAlign(true);
    $layout1->setXRotation(20);
    $layout1->setYRotation(20);
    $layout1->setPerspective(15);                                              
    $layout1->setRightAngleAxes(TRUE);  
    */
    $plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series));
    $title    = new PHPExcel_Chart_Title('3-D Chart');  
    $legend   = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
    $xTitle   = new PHPExcel_Chart_Title('3-D Chart');
    $yTitle   = new PHPExcel_Chart_Title('Languages');
    $chart    = new PHPExcel_Chart(
      'chart1',                                       // name
      $title,                                         // title
      $legend,                                        // legend 
      $plotarea,                                      // plotArea
      true,                                           // plotVisibleOnly
      0,                                              // displayBlanksAs
      $xTitle,                                        // xAxisLabel
      $yTitle                                         // yAxisLabel
    );                      
    $chart->setTopLeftPosition('B12');
    $chart->setBottomRightPosition('K32');
    $sheet->addChart($chart);
    $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
    $writer->setIncludeCharts(TRUE);
    $writer->save('D:/opt/lampp/htdocs/phpexcel/ExcelFiles/GenerateGraph_'.date ( "Y_m_d_h_m_s" ).'.xlsx');

?>

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top