123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- 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;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- require __DIR__ . '/../Header.php';
- $spreadsheet = new Spreadsheet();
- $worksheet = $spreadsheet->getActiveSheet();
- $worksheet->fromArray(
- [
- ['', 2010, 2011, 2012],
- ['Q1', 12, 15, 21],
- ['Q2', 56, 73, 86],
- ['Q3', 52, 61, 69],
- ['Q4', 30, 32, 0],
- ]
- );
- $dataSeriesLabels = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1),
- ];
- $xAxisTickValues = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4),
- ];
- $dataSeriesValues = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
- ];
- $series = new DataSeries(
- DataSeries::TYPE_BARCHART,
- DataSeries::GROUPING_STANDARD,
- range(0, count($dataSeriesValues) - 1),
- $dataSeriesLabels,
- $xAxisTickValues,
- $dataSeriesValues
- );
- $series->setPlotDirection(DataSeries::DIRECTION_COL);
- $plotArea = new PlotArea(null, [$series]);
- $legend = new Legend(Legend::POSITION_RIGHT, null, false);
- $title = new Title('Test Column Chart');
- $yAxisLabel = new Title('Value ($k)');
- $chart = new Chart(
- 'chart1',
- $title,
- $legend,
- $plotArea,
- true,
- 0,
- null,
- $yAxisLabel
- );
- $chart->setTopLeftPosition('A7');
- $chart->setBottomRightPosition('H20');
- $worksheet->addChart($chart);
- $filename = $helper->getFilename(__FILE__);
- $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->setIncludeCharts(true);
- $callStartTime = microtime(true);
- $writer->save($filename);
- $helper->logWrite($writer, $filename, $callStartTime);
|