【问题标题】:PHPExcel DataSeriesLabels not shownPHPExcel DataSeriesLabels 未显示
【发布时间】: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) 的一部分。我真的不明白为什么这不起作用,我很高兴有任何建议。

【问题讨论】:

    标签: php phpexcel


    【解决方案1】:

    我自己发现了问题,所以如果有人有同样的问题,请在这里回答:

    我总是在“区域字符串”中使用Data,例如Data!$B$1。现在我将其更改为Wooksheet/Wooksheet!$B$1,因为Wooksheet 是我的工作表的名称。看来这是必须的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2012-02-12
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多