2015-08-17 99 views
0

PHPExcel很棒,我非常喜歡將圖表添加到工作表的可能性。我玩過很多,但有一件事我無法完成。這是關於顯示在圖表的圖例中的數據系列的標籤: enter image description here 我只得到這樣的結果: (請忽略不同的款式/顏色)PHPExcel DataSeries標籤沒有顯示

在這裏,我做了一個「小」的例子,也有人看什麼做錯了?

<?php 
require_once '../vendor/autoload.php'; 
// header: download as xlxs file 
header("HTTP/1.1 200 OK"); 
header("Pragma: public"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
header('Content-Disposition: attachment; filename="data.xlsx"'); 
// create some sheet 
$PHPExcel = new PHPExcel(); 
$sheet = $PHPExcel->getActiveSheet(); 
$sheet->fromArray([ 
    // A   B  C   D 
    ['Month' ,'Data A', 'Data B', 'Data C'], // 1 
    ['2015-08', 36.24, 65.23, 43.34], // 2 
    ['2015-07', 54.15, 23.12, 28.53], // 3 
    ['2015-06', 43.35, 56.43,  3.30], // 4 
    ['2015-05', 62.56, 43.53, 32.63], // 5 
    ['2015-04', 23.38,  3.34,  5.23] // 6 
]); 

$dataSeriesLabels = [ 
    new \PHPExcel_Chart_DataSeriesValues('String','Data!$B$1',NULL,1), // B1 
    new \PHPExcel_Chart_DataSeriesValues('String','Data!$C$1',NULL,1), // C1 
    new \PHPExcel_Chart_DataSeriesValues('String','Data!$D$1',NULL,1) // D1 
]; 

$dataSeriesValues = [ 
    new \PHPExcel_Chart_DataSeriesValues('Number','Data!$B$2:$B$6',NULL,5), // B2-B6 
    new \PHPExcel_Chart_DataSeriesValues('Number','Data!$C$2:$C$6',NULL,5), // C2-C6 
    new \PHPExcel_Chart_DataSeriesValues('Number','Data!$D$2:$D$6',NULL,5), // D2-D6 
]; 

$xAxisLabels = [ 
    new \PHPExcel_Chart_DataSeriesValues('String','Data!$A$2:$A$6',NULL,5), // A2-A6 
]; 

$dataSeries = new \PHPExcel_Chart_DataSeries(
    \PHPExcel_Chart_DataSeries::TYPE_LINECHART, 
    \PHPExcel_Chart_DataSeries::GROUPING_STANDARD, 
    range(0, count($dataSeriesValues)-1), 
    $dataSeriesLabels, 
    $xAxisLabels, 
    $dataSeriesValues 
); 

$pa = new \PHPExcel_Chart_PlotArea(null, array($dataSeries)); 
$legend = new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false); 

$chartTitle = new \PHPExcel_Chart_Title('Test'); 
$chart = new \PHPExcel_Chart('chart_'.uniqid(),$chartTitle,$legend,$pa,true,0,null,null); 

$chart->setTopLeftPosition('A8'); 
$chart->setBottomRightPosition('K26'); 
$sheet->addChart($chart); 

$objWriter = new PHPExcel_Writer_Excel2007($PHPExcel); 
$objWriter->setPreCalculateFormulas(false); 
$objWriter->setIncludeCharts(true); 
$objWriter->save("php://output"); 

正如可以看到$dataSeriesLabels被設置並連接到$dataSeries,這是在$chart$pa(PlotArea)的一部分。我真的不明白爲什麼這不起作用,我很樂意提供任何建議。

回答

0

我發現我自己的問題,所以在這裏,如果有人有同樣的問題的答案:

我總是在「區域的字符串」 DataData!$B$1。現在我將其更改爲Wooksheet/Wooksheet!$B$1,因爲Wooksheet是我的工作表的名稱。似乎這是必需的。