|
| 1 | +<?php |
| 2 | + |
| 3 | +use PhpOffice\PhpSpreadsheet\Chart\Axis as ChartAxis; |
| 4 | +use PhpOffice\PhpSpreadsheet\Chart\Chart; |
| 5 | +use PhpOffice\PhpSpreadsheet\Chart\ChartColor; |
| 6 | +use PhpOffice\PhpSpreadsheet\Chart\DataSeries; |
| 7 | +use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; |
| 8 | +use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend; |
| 9 | +use PhpOffice\PhpSpreadsheet\Chart\PlotArea; |
| 10 | +use PhpOffice\PhpSpreadsheet\Chart\Properties; |
| 11 | +use PhpOffice\PhpSpreadsheet\Chart\Title; |
| 12 | +use PhpOffice\PhpSpreadsheet\IOFactory; |
| 13 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 14 | + |
| 15 | +require __DIR__ . '/../Header.php'; |
| 16 | + |
| 17 | +$spreadsheet = new Spreadsheet(); |
| 18 | +$dataSheet = $spreadsheet->getActiveSheet(); |
| 19 | +$dataSheet->setTitle('Data'); |
| 20 | + |
| 21 | +$results = [ |
| 22 | + ['Station 1', 'Score'], |
| 23 | + [13.25, 3], |
| 24 | + [16.25, 4], |
| 25 | + [18.5, 4], |
| 26 | + [15.5, 3], |
| 27 | + [15.75, 5], |
| 28 | + [17.25, 4], |
| 29 | + [10.5, 2], |
| 30 | +]; |
| 31 | + |
| 32 | +$dataSheet->fromArray($results); |
| 33 | + |
| 34 | +$spreadsheet->createSheet(); |
| 35 | + |
| 36 | +$chartSheet = $spreadsheet->getSheet(1); |
| 37 | +$chartSheet->setTitle('Appendix'); |
| 38 | + |
| 39 | +$dataSeriesLabels = [ |
| 40 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Data!$A$1', null, 1), |
| 41 | +]; |
| 42 | + |
| 43 | +$dataSeriesValues = [ |
| 44 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Data!$A$2:$A$' . count($results), Properties::FORMAT_CODE_NUMBER, 4, null, 'diamond', null, 7), |
| 45 | +]; |
| 46 | + |
| 47 | +$xAxisTickValues = [ |
| 48 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Data!$B$2:$B$' . count($results), Properties::FORMAT_CODE_NUMBER, 8), |
| 49 | +]; |
| 50 | + |
| 51 | +$dataSeriesValues[0]->setScatterLines(false); // Points not connected |
| 52 | + |
| 53 | +$dataSeriesValues[0]->getMarkerFillColor() |
| 54 | + ->setColorProperties('accent1', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME); |
| 55 | + |
| 56 | +// Build the dataseries |
| 57 | +$series = new DataSeries( |
| 58 | + DataSeries::TYPE_SCATTERCHART, // plotType |
| 59 | + null, // plotGrouping (Scatter charts don't have grouping) |
| 60 | + range(0, count($dataSeriesValues) - 1), // plotOrder |
| 61 | + $dataSeriesLabels, // plotLabel |
| 62 | + $xAxisTickValues, // plotCategory |
| 63 | + $dataSeriesValues, // plotValues |
| 64 | + null, // plotDirection |
| 65 | + null, // smooth line |
| 66 | + DataSeries::STYLE_LINEMARKER // plotStyle |
| 67 | +); |
| 68 | + |
| 69 | +// Set the series in the plot area |
| 70 | +$plotArea = new PlotArea(null, [$series]); |
| 71 | +// Set the chart legend |
| 72 | +$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false); |
| 73 | + |
| 74 | +$title = new Title($results[0][0]); |
| 75 | + |
| 76 | +$xAxis = new ChartAxis(); |
| 77 | + |
| 78 | +$xAxis->setAxisOptionsProperties( |
| 79 | + Properties::AXIS_LABELS_NEXT_TO, |
| 80 | + null, // horizontalCrossesValue |
| 81 | + null, // horizontalCrosses |
| 82 | + null, // axisOrientation |
| 83 | + null, // majorTmt |
| 84 | + Properties::TICK_MARK_OUTSIDE, // minorTmt |
| 85 | + 0, // minimum |
| 86 | + 6, // maximum |
| 87 | + null, // majorUnit |
| 88 | + 1, // minorUnit |
| 89 | +); |
| 90 | + |
| 91 | +$xAxis->setAxisType(ChartAxis::AXIS_TYPE_VALUE); |
| 92 | + |
| 93 | +$yAxis = new ChartAxis(); |
| 94 | + |
| 95 | +$yAxis->setAxisOptionsProperties( |
| 96 | + Properties::AXIS_LABELS_NEXT_TO, |
| 97 | + null, // horizontalCrossesValue |
| 98 | + null, // horizontalCrosses |
| 99 | + null, // axisOrientation |
| 100 | + null, // majorTmt |
| 101 | + Properties::TICK_MARK_OUTSIDE, // minorTmt |
| 102 | + 0, // minimum |
| 103 | + 25, // 30 // maximum |
| 104 | + null, // majorUnit |
| 105 | + 5, // minorUnit |
| 106 | +); |
| 107 | + |
| 108 | +// Create the chart |
| 109 | +$chart = new Chart( |
| 110 | + 'chart2', // name |
| 111 | + $title, // title |
| 112 | + $legend, // legend |
| 113 | + $plotArea, // plotArea |
| 114 | + true, // plotVisibleOnly |
| 115 | + DataSeries::EMPTY_AS_GAP, // displayBlanksAs |
| 116 | + null, // xAxisLabel |
| 117 | + null, // yAxisLabel |
| 118 | + // added xAxis for correct date display |
| 119 | + $xAxis, // xAxis |
| 120 | + $yAxis, // yAxis |
| 121 | +); |
| 122 | + |
| 123 | +// Set the position of the chart in the chart sheet below the first chart |
| 124 | +$chart->setTopLeftPosition('B2'); |
| 125 | +$chart->setBottomRightPosition('K22'); |
| 126 | + |
| 127 | +// Add the chart to the worksheet $chartSheet |
| 128 | +$chartSheet->addChart($chart); |
| 129 | +$spreadsheet->setActiveSheetIndex(1); |
| 130 | + |
| 131 | +// Save Excel 2007 file |
| 132 | +$filename = $helper->getFilename(__FILE__); |
| 133 | +$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
| 134 | +$writer->setIncludeCharts(true); |
| 135 | +$callStartTime = microtime(true); |
| 136 | +$writer->save($filename); |
| 137 | +$helper->logWrite($writer, $filename, $callStartTime); |
0 commit comments