123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- use PhpOffice\PhpSpreadsheet\Chart\Chart;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
- use PhpOffice\PhpSpreadsheet\Chart\Layout;
- 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],
- ]
- );
- $dataSeriesLabels1 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1),
- ];
- $xAxisTickValues1 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4),
- ];
- $dataSeriesValues1 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
- ];
- $series1 = new DataSeries(
- DataSeries::TYPE_PIECHART,
- null,
- range(0, count($dataSeriesValues1) - 1),
- $dataSeriesLabels1,
- $xAxisTickValues1,
- $dataSeriesValues1
- );
- $layout1 = new Layout();
- $layout1->setShowVal(true);
- $layout1->setShowPercent(true);
- $plotArea1 = new PlotArea($layout1, [$series1]);
- $legend1 = new Legend(Legend::POSITION_RIGHT, null, false);
- $title1 = new Title('Test Pie Chart');
- $chart1 = new Chart(
- 'chart1',
- $title1,
- $legend1,
- $plotArea1,
- true,
- 0,
- null,
- null
- );
- $chart1->setTopLeftPosition('A7');
- $chart1->setBottomRightPosition('H20');
- $worksheet->addChart($chart1);
- $dataSeriesLabels2 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1),
- ];
- $xAxisTickValues2 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4),
- ];
- $dataSeriesValues2 = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
- ];
- $series2 = new DataSeries(
- DataSeries::TYPE_DONUTCHART,
- null,
- range(0, count($dataSeriesValues2) - 1),
- $dataSeriesLabels2,
- $xAxisTickValues2,
- $dataSeriesValues2
- );
- $layout2 = new Layout();
- $layout2->setShowVal(true);
- $layout2->setShowCatName(true);
- $plotArea2 = new PlotArea($layout2, [$series2]);
- $title2 = new Title('Test Donut Chart');
- $chart2 = new Chart(
- 'chart2',
- $title2,
- null,
- $plotArea2,
- true,
- 0,
- null,
- null
- );
- $chart2->setTopLeftPosition('I7');
- $chart2->setBottomRightPosition('P20');
- $worksheet->addChart($chart2);
- $filename = $helper->getFilename(__FILE__);
- $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->setIncludeCharts(true);
- $callStartTime = microtime(true);
- $writer->save($filename);
- $helper->logWrite($writer, $filename, $callStartTime);
|