| 1 |
<?php |
| 2 |
|
| 3 |
use PhpOffice\PhpSpreadsheet\Chart\Chart; |
| 4 |
use PhpOffice\PhpSpreadsheet\Chart\DataSeries; |
| 5 |
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; |
| 6 |
use PhpOffice\PhpSpreadsheet\Chart\Legend; |
| 7 |
use PhpOffice\PhpSpreadsheet\Chart\PlotArea; |
| 8 |
use PhpOffice\PhpSpreadsheet\Chart\Title; |
| 9 |
use PhpOffice\PhpSpreadsheet\IOFactory; |
| 10 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 11 |
|
| 12 |
require __DIR__ . '/../Header.php'; |
| 13 |
|
| 14 |
$spreadsheet = new Spreadsheet(); |
| 15 |
$worksheet = $spreadsheet->getActiveSheet(); |
| 16 |
$worksheet->fromArray( |
| 17 |
[ |
| 18 |
['', 2010, 2011, 2012], |
| 19 |
['Q1', 12, 15, 21], |
| 20 |
['Q2', 56, 73, 86], |
| 21 |
['Q3', 52, 61, 69], |
| 22 |
['Q4', 30, 32, 0], |
| 23 |
] |
| 24 |
); |
| 25 |
|
| 26 |
// Set the Labels for each data series we want to plot |
| 27 |
// Datatype |
| 28 |
// Cell reference for data |
| 29 |
// Format Code |
| 30 |
// Number of datapoints in series |
| 31 |
// Data values |
| 32 |
// Data Marker |
| 33 |
$dataSeriesLabels = [ |
| 34 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010 |
| 35 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 |
| 36 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012 |
| 37 |
]; |
| 38 |
// Set the X-Axis Labels |
| 39 |
// Datatype |
| 40 |
// Cell reference for data |
| 41 |
// Format Code |
| 42 |
// Number of datapoints in series |
| 43 |
// Data values |
| 44 |
// Data Marker |
| 45 |
$xAxisTickValues = [ |
| 46 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
| 47 |
]; |
| 48 |
// Set the Data values for each data series we want to plot |
| 49 |
// Datatype |
| 50 |
// Cell reference for data |
| 51 |
// Format Code |
| 52 |
// Number of datapoints in series |
| 53 |
// Data values |
| 54 |
// Data Marker |
| 55 |
$dataSeriesValues = [ |
| 56 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4), |
| 57 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), |
| 58 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), |
| 59 |
]; |
| 60 |
|
| 61 |
// Build the dataseries |
| 62 |
$series = new DataSeries( |
| 63 |
DataSeries::TYPE_BARCHART, // plotType |
| 64 |
DataSeries::GROUPING_STANDARD, // plotGrouping |
| 65 |
range(0, count($dataSeriesValues) - 1), // plotOrder |
| 66 |
$dataSeriesLabels, // plotLabel |
| 67 |
$xAxisTickValues, // plotCategory |
| 68 |
$dataSeriesValues // plotValues |
| 69 |
); |
| 70 |
// Set additional dataseries parameters |
| 71 |
// Make it a vertical column rather than a horizontal bar graph |
| 72 |
$series->setPlotDirection(DataSeries::DIRECTION_COL); |
| 73 |
|
| 74 |
// Set the series in the plot area |
| 75 |
$plotArea = new PlotArea(null, [$series]); |
| 76 |
// Set the chart legend |
| 77 |
$legend = new Legend(Legend::POSITION_RIGHT, null, false); |
| 78 |
|
| 79 |
$title = new Title('Test Column Chart'); |
| 80 |
$yAxisLabel = new Title('Value ($k)'); |
| 81 |
|
| 82 |
// Create the chart |
| 83 |
$chart = new Chart( |
| 84 |
'chart1', // name |
| 85 |
$title, // title |
| 86 |
$legend, // legend |
| 87 |
$plotArea, // plotArea |
| 88 |
true, // plotVisibleOnly |
| 89 |
0, // displayBlanksAs |
| 90 |
null, // xAxisLabel |
| 91 |
$yAxisLabel // yAxisLabel |
| 92 |
); |
| 93 |
|
| 94 |
// Set the position where the chart should appear in the worksheet |
| 95 |
$chart->setTopLeftPosition('A7'); |
| 96 |
$chart->setBottomRightPosition('H20'); |
| 97 |
|
| 98 |
// Add the chart to the worksheet |
| 99 |
$worksheet->addChart($chart); |
| 100 |
|
| 101 |
// Save Excel 2007 file |
| 102 |
$filename = $helper->getFilename(__FILE__); |
| 103 |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
| 104 |
$writer->setIncludeCharts(true); |
| 105 |
$callStartTime = microtime(true); |
| 106 |
$writer->save($filename); |
| 107 |
$helper->logWrite($writer, $filename, $callStartTime); |
| 108 |
|