Laravel Excel 3.1
Appearance
Laravel Excel 3.1
Appearance
By using the WithCharts concern, you can add one or multiple charts to your worksheet.
You first have to instantiate a new PhpOffice\PhpSpreadsheet\Worksheet\Chart, passing the name, title, legend and plot in the constructor.
$label = [new DataSeriesValues('String', 'Worksheet!$B$1', null, 1)];
$categories = [new DataSeriesValues('String', 'Worksheet!$B$2:$B$5', null, 4)];
$values = [new DataSeriesValues('Number', 'Worksheet!$A$2:$A$5', null, 4)];
$series = new DataSeries(DataSeries::TYPE_PIECHART, DataSeries::GROUPING_STANDARD,
range(0, \count($values) - 1), $label, $categories, $values);
$plot = new PlotArea(null, [$series]);
$legend = new Legend();
$chart = new Chart('chart name', new Title('chart title'), $legend, $plot);
$chart->setTopLeftPosition('D1');
$chart->setBottomRightPosition('K12');You can view all available properties for Charts on the PhpSpreadsheet docs.
When you've instantiated the chart, you can add the WithCharts concern to your export class and return the Chart instance in the charts method.
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithCharts;
use PhpOffice\PhpSpreadsheet\Worksheet\Chart;
class InvoicesExport implements WithCharts
{
public function charts()
{
$label = [new DataSeriesValues('String', 'Worksheet!$B$1', null, 1)];
$categories = [new DataSeriesValues('String', 'Worksheet!$B$2:$B$5', null, 4)];
$values = [new DataSeriesValues('Number', 'Worksheet!$A$2:$A$5', null, 4)];
$series = new DataSeries(DataSeries::TYPE_PIECHART, DataSeries::GROUPING_STANDARD,
range(0, \count($values) - 1), $label, $categories, $values);
$plot = new PlotArea(null, [$series]);
$legend = new Legend();
$chart = new Chart('chart name', new Title('chart title'), $legend, $plot);
$chart->setTopLeftPosition('D1');
$chart->setBottomRightPosition('K12');
return $chart;
}
}You can add multiple charts to the worksheet by returning an array in the charts method.
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithCharts;
use PhpOffice\PhpSpreadsheet\Worksheet\Chart;
class InvoicesExport implements WithCharts
{
public function charts()
{
$label = [new DataSeriesValues('String', 'Worksheet!$B$1', null, 1)];
$categories = [new DataSeriesValues('String', 'Worksheet!$B$2:$B$5', null, 4)];
$values = [new DataSeriesValues('Number', 'Worksheet!$A$2:$A$5', null, 4)];
$series = new DataSeries(DataSeries::TYPE_PIECHART, DataSeries::GROUPING_STANDARD,
range(0, \count($values) - 1), $label, $categories, $values);
$plot = new PlotArea(null, [$series]);
$legend = new Legend();
$chart = new Chart('chart name', new Title('chart title'), $legend, $plot);
$chart->setTopLeftPosition('D1');
$chart->setBottomRightPosition('K12');
$chart2 = new Chart('chart 2 name', new Title('chart 2 title'), $legend, $plot);
$chart2->setTopLeftPosition('D13');
$chart2->setBottomRightPosition('K24');
return [$chart, $chart2];
}
}