PHPExcel在一张表中有两个单独的图表?

时间:2014-05-08 05:36:58

标签: charts phpexcel

我使用波纹管代码使用PHPExcel生成图表。但是现在我不能绘制两张图表,每张图表都应该使用一张表中的单独数据源,因此欢迎任何更改或建议。

    $workbook->getProperties()
            ->setCreator("TRAINING")
            ->setLastModifiedBy("TRAINING")
            ->setTitle("TRAINING")
            ->setSubject("TRAINING")
            ->setDescription("TRAINING")
            ->setKeywords("TRAINING Document")
            ->setCategory("TRAINING Document");
            $rotation = "";
    // Define variables for chart settings 
    $startRow_num = '3'; 
    $totalWhiteRows = 2; 
    $category_start = $startRow_num+1;
    // get the total number of rows that data source is write in plus one white row for A1 
    $total_rows = count($course_rep)+$totalWhiteRows;
    $start_ofChart = $total_rows+1;    
    for($sh = 0; $sh <= 1; $sh++)
    {

        //$sheet = $sheet.''.$sh;              
        //$excel->setActiveSheetIndex(3);

        if($sh == 0)
        {   
            $sheet = $workbook->createSheet($sh);
            $sheet = $workbook->setActiveSheetIndex($sh);
            $workbook->getActiveSheet()->setTitle('worksheet1');
            $sheet_name = $sheet->getTitle();
        }
        elseif($sh == 1)
        {   //$sheet = $workbook->createSheet($sh);
            $sheet = $workbook->setActiveSheetIndex($sh);
            $workbook->getActiveSheet()->setTitle('worksheet2');
            $sheet_name = $sheet->getTitle();
        }
        //echo "<pre/>"; print_r($sheet_name); exit;
        $workbook->getActiveSheet()->getTabColor()->setRGB('CC000#'); 
        $styleHeadings = array(
                                'borders' => array(
                                    'allborders' => array(
                                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                                            'color' => array('argb' => '#DBDBB7'),
                                        ),
                                    ),
                                'font' => array(
                                    'bold' => true,
                                    'size' => 20 
                                    ),
                                'fill' => array(
                                    'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
                                    'startcolor' => array(
                                        'argb' => 'E9E9E9',
                                    ),
                                    'endcolor' => array(
                                        'argb' => 'E9E9E9',
                                    ),
                                ),
                                'alignment' => array(
                                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
                                ),   
                    );
        if($this->mng_auth->get_language()=='en')
        {
            $sheet->setRightToLeft(false); 
        }
        else
        {  
            $sheet->setRightToLeft(true); 
        }
        $sheet->getStyle('G1:O2')->applyFromArray($styleHeadings);
        if($sh == 0)
        {
            $sheet->setCellValue('G1','ÑÇæÑ æÑÓ åÇ Èå ÇÓÇÓ ÑÊÈå ÔÇÑÏ');
        }
        else
        {
            $sheet->setCellValue('G1','ÑÇæÑ æÑÓ åÇ Èå ÇÓÇÓ ãÍá æÙíÝå ÔÇÑÏ');
        }
        $sheet->getStyle('G1:O2')->getAlignment()->setWrapText(true);
        //$sheet->setCellValue('H11','ãÌãæÚ ÔÇÑÏÇä'); 
        $sheet->mergeCellsByColumnAndRow(6,1,14,2);
        if($sh == 0)
        {
            $sheet->fromArray(  
               $course_rep,
               NULL,
               'A'.$startRow_num 
            );
        }
        else
        {
            $sheet->fromArray(  
               $course_jobLocBasedrep,
               NULL,
               'A'.$startRow_num  
            );

        }
        $sheet->fromArray(  
               $chart_total_stdOfCourse,
               NULL,
               'Q'.$startRow_num  
            );
        $labels = array(
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$B$'.$startRow_num, null, 1),
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$C$'.$startRow_num, null, 1), 
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$D$'.$startRow_num, null, 1), 
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$E$'.$startRow_num, null, 1), 
        );

        // find the end row number of data source because we write from A2 so A3 is the start of main data 

        $categories = array(
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$A$'.$category_start.':$A$'.$total_rows, null, 4),   
        );
        $values = array(
          new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$B$'.$category_start.':$B$'.$total_rows, null, 4),
          new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$C$'.$category_start.':$C$'.$total_rows, null, 4),  
          new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$D$'.$category_start.':$D$'.$total_rows, null, 4),  
          new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$E$'.$category_start.':$E$'.$total_rows, 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);

        // Total student chart 
        $total_labels = array(
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$Q$'.$startRow_num, null, 1),
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$R$'.$startRow_num, null, 1), 
        );

        // find the end row number of data source because we write from A2 so A3 is the start of main data 

        $total_categories = array(
          new PHPExcel_Chart_DataSeriesValues('String', $sheet_name.'!$Q$'.$category_start.':$Q$'.$total_rows, null, 4),   
        );
        $total_values = array(
          new PHPExcel_Chart_DataSeriesValues('Number', $sheet_name.'!$R$'.$category_start.':$R$'.$total_rows, null, 4),  
        );     
        $total_series = new PHPExcel_Chart_DataSeries(
          PHPExcel_Chart_DataSeries::TYPE_BARCHART_3D,     // plotType
          PHPExcel_Chart_DataSeries::GROUPING_STACKED,  // plotGrouping
          array(0,1),                                     // plotOrder
          $total_labels,                                        // plotLabel
          $total_categories,                                    // plotCategory
          $total_values                                         // plotValues
        );  

        $total_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);                                              
        //$layout1->setShowPercent(TRUE);
        //$layout1->setShowCatName(TRUE);
        //$layout1->setShowSerName(TRUE);
        //$layout1->setShowBubbleSize(TRUE);
        //$layout1->setShowLeaderLines(TRUE);
        //$layout1->setShowLegendKey(TRUE);
        //$image = base_url().'image/app_req.jpeg';
        //$header1 = new PHPExcel_Worksheet();

        //$header1->setHeaderFooter(PHPExcel_Worksheet_HeaderFooter::addImage($image,'A12'));
        $plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series,$total_series));
        $title    = new PHPExcel_Chart_Title($this->lang->line('co_report'), null);  
        //$title = new PHPExcel_Chart_Title(new PHPExcel_RichText(new PHPExcel_Cell('A','1','Ttile Chart',PHPExcel_Cell_DataType::TYPE_STRING,$sheet)));
        $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('B'.$start_ofChart);
        $chart->setBottomRightPosition('J25');
        //echo "<pre/>";print_r($chart); exit;  
        //echo "<pre/>"; print_r($workbook); exit; 
        $sheet->addChart($chart); 
    }   
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="training report.xlsx"');
    header('Cache-Control: max-age=0'); 
    $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
    $writer->setIncludeCharts(TRUE);
    ob_start();
    ob_end_clean(); 
    $writer->save('php://output');

我应该在每个工作表中绘制两个图表,每个图表应该使用自己的数据源,但上面的代码不能绘制图表,只是它将数据源值写入工作表内的指定列行。但是,如果我在工作表中绘制一个图表,则上面的代码运行为真。

0 个答案:

没有答案