0% found this document useful (0 votes)
226 views1 page

Grafico 1

This PHP code creates a bar chart in an Excel spreadsheet using the PhpSpreadsheet library. It sets cell values, defines the chart data series and plot area, inserts the chart on the worksheet, saves the file with the chart included, then reloads the template file and updates a cell value while losing the embedded chart.

Uploaded by

Ricardo David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views1 page

Grafico 1

This PHP code creates a bar chart in an Excel spreadsheet using the PhpSpreadsheet library. It sets cell values, defines the chart data series and plot area, inserts the chart on the worksheet, saves the file with the chart included, then reloads the template file and updates a cell value while losing the embedded chart.

Uploaded by

Ricardo David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php

use PhpOffice\PhpSpreadsheet\Spreadsheet;

//$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();


//$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 1);
$sheet->setCellValue('A2', 2);
$sheet->setCellValue('A3', 3);

$area = 'Worksheet!$A$1:$A$3';
$dataSeriesValues = $dataseriesLabels = $xAxisTickValues = [
new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', $area),
];

$series = new \PhpOffice\PhpSpreadsheet\Chart\DataSeries(


\PhpOffice\PhpSpreadsheet\Chart\DataSeries::TYPE_BARCHART,
\PhpOffice\PhpSpreadsheet\Chart\DataSeries::GROUPING_STANDARD,
range(0, count($dataSeriesValues) - 1),
$dataseriesLabels,
$xAxisTickValues,
$dataSeriesValues
);

$plotarea = new \PhpOffice\PhpSpreadsheet\Chart\PlotArea(null, [$series]);


$chart = new \PhpOffice\PhpSpreadsheet\Chart(
'chart1', // name
null, // title
null, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);

$chart->setTopLeftPosition('A5');
$chart->setBottomRightPosition('H20');
$sheet->addChart($chart);

// save XLSX
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save('template.xlsx'); // saved with the chart

// re-use template
$spreadsheetTpl = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
$worksheetTpl = $spreadsheetTpl->getActiveSheet();
$worksheetTpl->getCell('A1')->setValue(123);
$writerTpl = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheetTpl,
'Xlsx');
$writerTpl->setIncludeCharts(true); // useless since no charts were loaded
$writerTpl->save('template2.xlsx'); // cell A1 has value 123, but the chart is gone

You might also like