【问题标题】:PHPExcel dataseries not workingPHPExcel数据系列不起作用
【发布时间】:2023-03-10 17:17:02
【问题描述】:

我使用 PHPExcel 生成堆叠条形图所有事情都已完成,但多级(多色)条形图无法正常工作,下面是我的代码。

     $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');

以上代码的输出如下。

但我需要的是下面的结果。

【问题讨论】:

    标签: php charts phpexcel


    【解决方案1】:

    array(0,1) 更改为array(0,1,2,3) 作为注释的“plotOrder”参数的值:

    $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
        );  
    

    我刚刚检查过 - 它对我有用(我使用的是 PHPExcel v1.8.0)。

    【讨论】:

    • 但是另一个问题,我可以标记每个系列顶部的所有级别的总和吗?例如,当绘制条 A 时,总数是 4o,我可以在系列 A 的顶部显示这个值吗?
    • @MobasherFasihy,不幸的是,我不知道这是否可行,如果可行,该怎么做。我并没有真正使用 PHPExcel。我帮你解决了这个问题,因为发现错误比找到功能更容易。您可以尝试在 Stack Overflow 上提出另一个问题。
    【解决方案2】:

    我有解决方案,这将与 phpExcel 2.0 也兼容

    <?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');
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多