【发布时间】:2015-08-17 10:06:47
【问题描述】:
PHPExcel 很棒,我真的很喜欢将图表添加到工作表中的可能性。我玩了很多,但有一件事我无法完成。这是关于在图表的图例中显示数据系列标签: 我只得到这个结果: (请忽略不同的款式/颜色)
在这里我做了一个“小”的例子,有人看到什么做错了吗?
<?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) 的一部分。我真的不明白为什么这不起作用,我很高兴有任何建议。
【问题讨论】: