首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPSpreadsheet用图表生成无效文件

PHPSpreadsheet用图表生成无效文件
EN

Stack Overflow用户
提问于 2020-01-11 12:34:39
回答 1查看 1.3K关注 0票数 3

我正在将现有的/工作应用程序从PHPExcel (phpoffice/phpexcel: 1.8.0)迁移到PHPSpreadsheet,在生成包含、一个或多个图表文件时遇到了一个问题。

如果没有任何addChart调用,则生成的文件是有效的,但一旦添加图表,生成的文件就无效。Excel确实尝试恢复数据,该数据成功但没有图表。

Excel恢复工具显示:

代码语言:javascript
复制
Excel completed file level validation and repair. 
Some parts of this workbook may have been repaired or discarded.
Removed Part: /xl/drawings/drawing1.xml part.  (Drawing shape)

当前使用:PHP7.1和phpoffice/phpspreadsheet: "1.10.1"

我的代码非常类似于我试图添加的PieChart示例。

生成PieChart的代码的一部分:

  • $ageCounterRowIndex值为6
  • $expectedPointCount的值为4
  • 我的数据勾值位于D3:D6中。
  • 我的值位于E3:E6
代码语言:javascript
复制
$dataSeriesLabels = [];

$xAxisTickValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, "$quotedSheetTitle\$D\$3:\$D\$$ageCounterRowIndex", $expectedPointCount)
];

$dataSeriesValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, "$quotedSheetTitle\$E\$3:\$E\$$ageCounterRowIndex", $expectedPointCount),
];

$dataSeries = new DataSeries(
    DataSeries::TYPE_PIECHART,
    NULL, 
    range(0, count($dataSeriesValues) - 1), 
    $dataSeriesLabels, 
    $xAxisTickValues, 
    $dataSeriesValues 
);

$pieLayout = new Layout();
$pieLayout->setShowVal(TRUE)->setShowPercent(TRUE);

$plotArea = new PlotArea($pieLayout, [ $dataSeries ]);
$legend = new Legend(Legend::POSITION_RIGHT, NULL, FALSE);

$chart = new Chart(
    'piechart_' . ($sheetIndex - 1), 
    null,
    $legend, 
    $plotArea, 
    true, 
    0, 
    NULL, 
    NULL 
);

$chart->setTopLeftPosition('G2');
$chart->setBottomRightPosition('L15');
$sheet->addChart($chart);
EN

回答 1

Stack Overflow用户

发布于 2020-04-22 22:20:08

将新图表调用中的displayBlanksAs参数从0更改为'gap'解决了这个问题。

我在PhpSpreadsheet存储库上找到了一份关于它的错误报告

代码语言:javascript
复制
$chart = new Chart(
    'piechart_' . ($sheetIndex - 1), 
    null,
    $legend, 
    $plotArea, 
    true, 
    0, 
    NULL, 
    NULL 
);

变成了

代码语言:javascript
复制
$chart = new Chart(
    'piechart_' . ($sheetIndex - 1), 
    null,
    $legend, 
    $plotArea, 
    true, 
    'gap', 
    NULL, 
    NULL 
);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59694440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档