如何使用phpSpreadsheet设置图表宽度和高度



我正在尝试设置由phpSpreadsheet创建的Excel图表的大小。

我使用了以下代码,但布局设置似乎没有任何效果:

// Define chart position and size
$layout = new Layout();
$layout->setXPosition(10);
$layout->setYPosition(50);
$layout->setWidth(400);
$layout->setHeight(300);
//  Set the series in the plot area
$plotArea = new PlotArea($layout, [$series]);
//  Set the chart legend
$legend = new Legend(Legend::POSITION_RIGHT, null, false);
$title = new Title('Test');
$yAxisLabel = new Title('y');
//  Create the chart
$chart = new Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
$yAxisLabel  // yAxisLabel
);

没有明确的图表宽度和高度设置。相反,请为图表区定义左上和右下单元格,如下所示:

$chart->setTopLeftPosition('A1');
$chart->setBottomRightPosition('M9');
$worksheet->addChart($chart);

请参阅图表示例,例如此处。

最新更新