| 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 |
['', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'], |
| 19 |
['Jan', 78, 52, 61], |
| 20 |
['Feb', 64, 54, 62], |
| 21 |
['Mar', 62, 57, 63], |
| 22 |
['Apr', 21, 62, 59], |
| 23 |
['May', 11, 75, 60], |
| 24 |
['Jun', 1, 75, 57], |
| 25 |
['Jul', 1, 79, 56], |
| 26 |
['Aug', 1, 79, 59], |
| 27 |
['Sep', 10, 75, 60], |
| 28 |
['Oct', 40, 68, 63], |
| 29 |
['Nov', 69, 62, 64], |
| 30 |
['Dec', 89, 57, 66], |
| 31 |
] |
| 32 |
); |
| 33 |
|
| 34 |
// Set the Labels for each data series we want to plot |
| 35 |
// Datatype |
| 36 |
// Cell reference for data |
| 37 |
// Format Code |
| 38 |
// Number of datapoints in series |
| 39 |
// Data values |
| 40 |
// Data Marker |
| 41 |
$dataSeriesLabels1 = [ |
| 42 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // Temperature |
| 43 |
]; |
| 44 |
$dataSeriesLabels2 = [ |
| 45 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // Rainfall |
| 46 |
]; |
| 47 |
$dataSeriesLabels3 = [ |
| 48 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // Humidity |
| 49 |
]; |
| 50 |
|
| 51 |
// Set the X-Axis Labels |
| 52 |
// Datatype |
| 53 |
// Cell reference for data |
| 54 |
// Format Code |
| 55 |
// Number of datapoints in series |
| 56 |
// Data values |
| 57 |
// Data Marker |
| 58 |
$xAxisTickValues = [ |
| 59 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec |
| 60 |
]; |
| 61 |
|
| 62 |
// Set the Data values for each data series we want to plot |
| 63 |
// Datatype |
| 64 |
// Cell reference for data |
| 65 |
// Format Code |
| 66 |
// Number of datapoints in series |
| 67 |
// Data values |
| 68 |
// Data Marker |
| 69 |
$dataSeriesValues1 = [ |
| 70 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$13', null, 12), |
| 71 |
]; |
| 72 |
|
| 73 |
// Build the dataseries |
| 74 |
$series1 = new DataSeries( |
| 75 |
DataSeries::TYPE_BARCHART, // plotType |
| 76 |
DataSeries::GROUPING_CLUSTERED, // plotGrouping |
| 77 |
range(0, count($dataSeriesValues1) - 1), // plotOrder |
| 78 |
$dataSeriesLabels1, // plotLabel |
| 79 |
$xAxisTickValues, // plotCategory |
| 80 |
$dataSeriesValues1 // plotValues |
| 81 |
); |
| 82 |
// Set additional dataseries parameters |
| 83 |
// Make it a vertical column rather than a horizontal bar graph |
| 84 |
$series1->setPlotDirection(DataSeries::DIRECTION_COL); |
| 85 |
|
| 86 |
// Set the Data values for each data series we want to plot |
| 87 |
// Datatype |
| 88 |
// Cell reference for data |
| 89 |
// Format Code |
| 90 |
// Number of datapoints in series |
| 91 |
// Data values |
| 92 |
// Data Marker |
| 93 |
$dataSeriesValues2 = [ |
| 94 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$13', null, 12), |
| 95 |
]; |
| 96 |
|
| 97 |
// Build the dataseries |
| 98 |
$series2 = new DataSeries( |
| 99 |
DataSeries::TYPE_LINECHART, // plotType |
| 100 |
DataSeries::GROUPING_STANDARD, // plotGrouping |
| 101 |
range(0, count($dataSeriesValues2) - 1), // plotOrder |
| 102 |
$dataSeriesLabels2, // plotLabel |
| 103 |
[], // plotCategory |
| 104 |
$dataSeriesValues2 // plotValues |
| 105 |
); |
| 106 |
|
| 107 |
// Set the Data values for each data series we want to plot |
| 108 |
// Datatype |
| 109 |
// Cell reference for data |
| 110 |
// Format Code |
| 111 |
// Number of datapoints in series |
| 112 |
// Data values |
| 113 |
// Data Marker |
| 114 |
$dataSeriesValues3 = [ |
| 115 |
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$13', null, 12), |
| 116 |
]; |
| 117 |
|
| 118 |
// Build the dataseries |
| 119 |
$series3 = new DataSeries( |
| 120 |
DataSeries::TYPE_AREACHART, // plotType |
| 121 |
DataSeries::GROUPING_STANDARD, // plotGrouping |
| 122 |
range(0, count($dataSeriesValues2) - 1), // plotOrder |
| 123 |
$dataSeriesLabels3, // plotLabel |
| 124 |
[], // plotCategory |
| 125 |
$dataSeriesValues3 // plotValues |
| 126 |
); |
| 127 |
|
| 128 |
// Set the series in the plot area |
| 129 |
$plotArea = new PlotArea(null, [$series1, $series2, $series3]); |
| 130 |
// Set the chart legend |
| 131 |
$legend = new Legend(Legend::POSITION_RIGHT, null, false); |
| 132 |
|
| 133 |
$title = new Title('Average Weather Chart for Crete'); |
| 134 |
|
| 135 |
// Create the chart |
| 136 |
$chart = new Chart( |
| 137 |
'chart1', // name |
| 138 |
$title, // title |
| 139 |
$legend, // legend |
| 140 |
$plotArea, // plotArea |
| 141 |
true, // plotVisibleOnly |
| 142 |
0, // displayBlanksAs |
| 143 |
null, // xAxisLabel |
| 144 |
null // yAxisLabel |
| 145 |
); |
| 146 |
|
| 147 |
// Set the position where the chart should appear in the worksheet |
| 148 |
$chart->setTopLeftPosition('F2'); |
| 149 |
$chart->setBottomRightPosition('O16'); |
| 150 |
|
| 151 |
// Add the chart to the worksheet |
| 152 |
$worksheet->addChart($chart); |
| 153 |
|
| 154 |
// Save Excel 2007 file |
| 155 |
$filename = $helper->getFilename(__FILE__); |
| 156 |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
| 157 |
$writer->setIncludeCharts(true); |
| 158 |
$callStartTime = microtime(true); |
| 159 |
$writer->save($filename); |
| 160 |
$helper->logWrite($writer, $filename, $callStartTime); |
| 161 |
|