使用PHPSpreadsheet创建自定义折线图。我已将数据加载到电子表格中,并且正在构建图表。但是,我无法调整图表中线条的颜色。当我尝试将颜色应用于我的系列时,电子表格会打开,但存在某种损坏并且图形无法加载。 Excel告诉我它已删除图表,以便它可以打开电子表格:'Removed Part:/xl/drawings/drawing1.xml part。 (绘图形状)'
我无法在文档中找到有关自定义图表颜色的任何信息,但它已在今年早些时候提交并合并到项目中。
我已经检查了我的PHPSpreadsheet版本的源代码,它确实有更新的文件/类,允许自定义图表着色。
<?php
require_once '/opt/sites/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Title;
$phpsheet = new Spreadsheet();
$phpsheet->setActiveSheetIndex(0);
$phpsheet->getActiveSheet()->setTitle('Worksheet');
$file = "report.xlsx";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=\"".$file."\"");
header('Cache-Control: max-age=0');
$phpsheet->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$phpsheet->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$phpsheet->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$r = 1;
$phpsheet->getActiveSheet()->setCellValue('A' . $r, "Date");
$phpsheet->getActiveSheet()->setCellValue('B' . $r, "Val One");
$phpsheet->getActiveSheet()->setCellValue('C' . $r, "Val Two");
$r++;
while($r<=20)
{
$randNumOne = rand(1,100);
$randNumTwo = rand(1,100);
$phpsheet->getActiveSheet()->setCellValue('A' . $r, "2018-04-".$r);
$phpsheet->getActiveSheet()->setCellValue('B' . $r, $randNumOne);
$phpsheet->getActiveSheet()->setCellValue('C' . $r, $randNumTwo);
$r++;
}
/////////////////////////////////////////////////////////
//////////////////////////Error Here/////////////////////
///If I change the two hex codes to NULL, there is no issue but of course then there is no change in color either.
/////////////////////////////////////////////////////////
//////////////////////////Error Here/////////////////////
$dsl=array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', NULL, 1, [], NULL, "d914e0"), //Cause of the error
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', NULL, 1, [], NULL, "2ce81b"), //Cause of the error
);
$x = $r - 1;
$xal=array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$'.$r, NULL, $x),
);
$dsv = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$'.$r, NULL, $x),
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$'.$r, NULL, $x),
);
$ds = new DataSeries(
DataSeries::TYPE_LINECHART,
DataSeries::GROUPING_STANDARD,
range(0, count($dsv)-1),
$dsl,
$xal,
$dsv
);
$pa = new PlotArea(NULL, array($ds));
$legend = new Legend(Legend::POSITION_RIGHT, NULL, false);
$title = new Title('Sample Chart');
$chart = new Chart(
'chart1',
$title,
$legend,
$pa,
0,
0,
NULL,
NULL
);
$chart->setTopLeftPosition('E1');
$chart->setBottomRightPosition('V20');
$phpsheet->getActiveSheet()->addChart($chart);
$Excel_writer = new Xlsx($phpsheet);
ob_end_clean();
$Excel_writer->setIncludeCharts(true);
$Excel_writer->save('php://output');
我在服务器端没有任何错误/警告/问题。一切都按预期执行,除了'绘图损坏'之外,我得到的excel文件没有问题。
答案 0 :(得分:2)
通过用'gap'替换0来解决我的问题,似乎最近的一次推送将预期的int'0'更改为字符串'gap':
$chart = new Chart(
'chart1',
$title,
$legend,
$pa,
0,
'gap', //change here from 0 to gap
NULL,
NULL
);