|
| 1 | +<?php |
| 2 | + |
| 3 | +use PhpOffice\PhpSpreadsheet\Chart\Axis; |
| 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 | +$worksheet = $spreadsheet->getActiveSheet(); |
| 19 | +// changed data to simulate a trend chart - Xaxis are dates; Yaxis are 3 meausurements from each date |
| 20 | +$worksheet->fromArray( |
| 21 | + [ |
| 22 | + ['', 'metric1', 'metric2', 'metric3'], |
| 23 | + ['=DATEVALUE("2021-01-01")', 12.1, 15.1, 21.1], |
| 24 | + ['=DATEVALUE("2021-01-04")', 56.2, 73.2, 86.2], |
| 25 | + ['=DATEVALUE("2021-01-07")', 52.2, 61.2, 69.2], |
| 26 | + ['=DATEVALUE("2021-01-10")', 30.2, 32.2, 0.2], |
| 27 | + ] |
| 28 | +); |
| 29 | +$worksheet->getStyle('A2:A5')->getNumberFormat()->setFormatCode(Properties::FORMAT_CODE_DATE_ISO8601); |
| 30 | +$worksheet->getColumnDimension('A')->setAutoSize(true); |
| 31 | +$worksheet->setSelectedCells('A1'); |
| 32 | + |
| 33 | +// Set the Labels for each data series we want to plot |
| 34 | +// Datatype |
| 35 | +// Cell reference for data |
| 36 | +// Format Code |
| 37 | +// Number of datapoints in series |
| 38 | +// Data values |
| 39 | +// Data Marker |
| 40 | +$dataSeriesLabels = [ |
| 41 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // was 2010 |
| 42 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // was 2011 |
| 43 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // was 2012 |
| 44 | +]; |
| 45 | +// Set the X-Axis Labels |
| 46 | +// changed from STRING to NUMBER |
| 47 | +// added 2 additional x-axis values associated with each of the 3 metrics |
| 48 | +// added FORMATE_CODE_NUMBER |
| 49 | +$xAxisTickValues = [ |
| 50 | + //new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
| 51 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), |
| 52 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), |
| 53 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), |
| 54 | +]; |
| 55 | +// Set the Data values for each data series we want to plot |
| 56 | +// Datatype |
| 57 | +// Cell reference for data |
| 58 | +// Format Code |
| 59 | +// Number of datapoints in series |
| 60 | +// Data values |
| 61 | +// Data Marker |
| 62 | +// added FORMAT_CODE_NUMBER |
| 63 | +$dataSeriesValues = [ |
| 64 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', Properties::FORMAT_CODE_NUMBER, 4), |
| 65 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', Properties::FORMAT_CODE_NUMBER, 4), |
| 66 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', Properties::FORMAT_CODE_NUMBER, 4), |
| 67 | +]; |
| 68 | + |
| 69 | +// series 1 |
| 70 | +// marker details |
| 71 | +$dataSeriesValues[0] |
| 72 | + ->setPointMarker('diamond') |
| 73 | + ->setPointSize(5) |
| 74 | + ->getMarkerFillColor() |
| 75 | + ->setColorProperties('0070C0', null, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 76 | +$dataSeriesValues[0] |
| 77 | + ->getMarkerBorderColor() |
| 78 | + ->setColorProperties('002060', null, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 79 | + |
| 80 | +// line details - smooth line, connected |
| 81 | +$dataSeriesValues[0] |
| 82 | + ->setScatterLines(true) |
| 83 | + ->setSmoothLine(true) |
| 84 | + ->setLineColorProperties('accent1', 40, ChartColor::EXCEL_COLOR_TYPE_SCHEME); // value, alpha, type |
| 85 | +$dataSeriesValues[0]->setLineStyleProperties( |
| 86 | + 2.5, // width in points |
| 87 | + Properties::LINE_STYLE_COMPOUND_TRIPLE, // compound |
| 88 | + Properties::LINE_STYLE_DASH_SQUARE_DOT, // dash |
| 89 | + Properties::LINE_STYLE_CAP_SQUARE, // cap |
| 90 | + Properties::LINE_STYLE_JOIN_MITER, // join |
| 91 | + Properties::LINE_STYLE_ARROW_TYPE_OPEN, // head type |
| 92 | + Properties::LINE_STYLE_ARROW_SIZE_4, // head size preset index |
| 93 | + Properties::LINE_STYLE_ARROW_TYPE_ARROW, // end type |
| 94 | + Properties::LINE_STYLE_ARROW_SIZE_6 // end size preset index |
| 95 | +); |
| 96 | + |
| 97 | +// series 2 - straight line - no special effects, connected, straight line |
| 98 | +$dataSeriesValues[1] // square fill |
| 99 | + ->setPointMarker('square') |
| 100 | + ->setPointSize(6) |
| 101 | + ->getMarkerBorderColor() |
| 102 | + ->setColorProperties('accent6', 3, ChartColor::EXCEL_COLOR_TYPE_SCHEME); |
| 103 | +$dataSeriesValues[1] // square border |
| 104 | + ->getMarkerFillColor() |
| 105 | + ->setColorProperties('0FFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 106 | +$dataSeriesValues[1] |
| 107 | + ->setScatterLines(true) |
| 108 | + ->setSmoothLine(false) |
| 109 | + ->setLineColorProperties('FF0000', 80, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 110 | +$dataSeriesValues[1]->setLineWidth(2.0); |
| 111 | + |
| 112 | +// series 3 - markers, no line |
| 113 | +$dataSeriesValues[2] // triangle fill |
| 114 | + //->setPointMarker('triangle') // let Excel choose shape |
| 115 | + ->setPointSize(7) |
| 116 | + ->getMarkerFillColor() |
| 117 | + ->setColorProperties('FFFF00', null, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 118 | +$dataSeriesValues[2] // triangle border |
| 119 | + ->getMarkerBorderColor() |
| 120 | + ->setColorProperties('accent4', null, ChartColor::EXCEL_COLOR_TYPE_SCHEME); |
| 121 | +$dataSeriesValues[2]->setScatterLines(false); // points not connected |
| 122 | + |
| 123 | + // Added so that Xaxis shows dates instead of Excel-equivalent-year1900-numbers |
| 124 | +$xAxis = new Axis(); |
| 125 | +//$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE ); |
| 126 | +$xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE_ISO8601, true); |
| 127 | +$xAxis->setAxisOption('textRotation', '45'); |
| 128 | +$xAxis->setAxisOption('hidden', '1'); |
| 129 | + |
| 130 | +$yAxis = new Axis(); |
| 131 | +$yAxis->setLineStyleProperties( |
| 132 | + 2.5, // width in points |
| 133 | + Properties::LINE_STYLE_COMPOUND_SIMPLE, |
| 134 | + Properties::LINE_STYLE_DASH_DASH_DOT, |
| 135 | + Properties::LINE_STYLE_CAP_FLAT, |
| 136 | + Properties::LINE_STYLE_JOIN_BEVEL |
| 137 | +); |
| 138 | +$yAxis->setLineColorProperties('ffc000', null, ChartColor::EXCEL_COLOR_TYPE_RGB); |
| 139 | +$yAxis->setAxisOption('hidden', '1'); |
| 140 | + |
| 141 | +// Build the dataseries |
| 142 | +$series = new DataSeries( |
| 143 | + DataSeries::TYPE_SCATTERCHART, // plotType |
| 144 | + null, // plotGrouping (Scatter charts don't have any grouping) |
| 145 | + range(0, count($dataSeriesValues) - 1), // plotOrder |
| 146 | + $dataSeriesLabels, // plotLabel |
| 147 | + $xAxisTickValues, // plotCategory |
| 148 | + $dataSeriesValues, // plotValues |
| 149 | + null, // plotDirection |
| 150 | + false, // smooth line |
| 151 | + DataSeries::STYLE_SMOOTHMARKER // plotStyle |
| 152 | +); |
| 153 | + |
| 154 | +// Set the series in the plot area |
| 155 | +$plotArea = new PlotArea(null, [$series]); |
| 156 | +$plotArea->setNoFill(true); |
| 157 | +// Set the chart legend |
| 158 | +$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false); |
| 159 | + |
| 160 | +$title = new Title('Test Scatter Trend Chart'); |
| 161 | +//$yAxisLabel = new Title('Value ($k)'); |
| 162 | + |
| 163 | +// Create the chart |
| 164 | +$chart = new Chart( |
| 165 | + 'chart1', // name |
| 166 | + $title, // title |
| 167 | + $legend, // legend |
| 168 | + $plotArea, // plotArea |
| 169 | + true, // plotVisibleOnly |
| 170 | + DataSeries::EMPTY_AS_GAP, // displayBlanksAs |
| 171 | + null, // xAxisLabel |
| 172 | + null, //$yAxisLabel, // yAxisLabel |
| 173 | + // added xAxis for correct date display |
| 174 | + $xAxis, // xAxis |
| 175 | + $yAxis, // yAxis |
| 176 | +); |
| 177 | +$chart->setNoFill(true); |
| 178 | + |
| 179 | +// Set the position where the chart should appear in the worksheet |
| 180 | +$chart->setTopLeftPosition('A7'); |
| 181 | +$chart->setBottomRightPosition('P20'); |
| 182 | +// Add the chart to the worksheet |
| 183 | +$worksheet->addChart($chart); |
| 184 | + |
| 185 | +// Save Excel 2007 file |
| 186 | +$filename = $helper->getFilename(__FILE__); |
| 187 | +$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
| 188 | +$writer->setIncludeCharts(true); |
| 189 | +$callStartTime = microtime(true); |
| 190 | +$writer->save($filename); |
| 191 | +$spreadsheet->disconnectWorksheets(); |
| 192 | +$helper->logWrite($writer, $filename, $callStartTime); |
0 commit comments