|
| 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 as ChartLegend; |
| 7 | +use PhpOffice\PhpSpreadsheet\Chart\PlotArea; |
| 8 | +use PhpOffice\PhpSpreadsheet\Chart\Title; |
| 9 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 10 | + |
| 11 | +require __DIR__ . '/../Header.php'; |
| 12 | + |
| 13 | +$spreadsheet = new Spreadsheet(); |
| 14 | +$worksheet = $spreadsheet->getActiveSheet(); |
| 15 | +$worksheet->fromArray( |
| 16 | + [ |
| 17 | + ['', 2010, 2011, 2012], |
| 18 | + ['Q1', 12, 15, 21], |
| 19 | + ['Q2', 56, null, 86], |
| 20 | + ['Q3', 52, 61, 69], |
| 21 | + ['Q4', 30, 32, 0], |
| 22 | + ], |
| 23 | + strictNullComparison: true |
| 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 | +$xAxisTickValues = [ |
| 40 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
| 41 | +]; |
| 42 | +// Set the Data values for each data series we want to plot |
| 43 | +// Datatype |
| 44 | +// Cell reference for data |
| 45 | +// Format Code |
| 46 | +// Number of datapoints in series |
| 47 | +// Data values |
| 48 | +// Data Marker |
| 49 | +$dataSeriesValues = [ |
| 50 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4), |
| 51 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), |
| 52 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), |
| 53 | +]; |
| 54 | + |
| 55 | +// Build the dataseries |
| 56 | +$series = new DataSeries( |
| 57 | + DataSeries::TYPE_SCATTERCHART, // plotType |
| 58 | + null, // plotGrouping (Scatter charts don't have any grouping) |
| 59 | + range(0, count($dataSeriesValues) - 1), // plotOrder |
| 60 | + $dataSeriesLabels, // plotLabel |
| 61 | + $xAxisTickValues, // plotCategory |
| 62 | + $dataSeriesValues, // plotValues |
| 63 | + null, // plotDirection |
| 64 | + false, // smooth line |
| 65 | + DataSeries::STYLE_LINEMARKER // plotStyle |
| 66 | +); |
| 67 | + |
| 68 | +// Set the series in the plot area |
| 69 | +$plotArea = new PlotArea(null, [$series]); |
| 70 | +// Set the chart legend |
| 71 | +$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false); |
| 72 | + |
| 73 | +$title1 = new Title('Test Scatter Chart Gap'); |
| 74 | +$yAxisLabel1 = new Title('Value ($k)'); |
| 75 | +// Create the chart |
| 76 | +$chart1 = new Chart( |
| 77 | + 'chart1', // name |
| 78 | + $title1, // title |
| 79 | + $legend, // legend |
| 80 | + $plotArea, // plotArea |
| 81 | + true, // plotVisibleOnly |
| 82 | + DataSeries::EMPTY_AS_GAP, // displayBlanksAs |
| 83 | + null, // xAxisLabel |
| 84 | + $yAxisLabel1 // yAxisLabel |
| 85 | +); |
| 86 | + |
| 87 | +// Set the position where the chart should appear in the worksheet |
| 88 | +$chart1->setTopLeftPosition('A7'); |
| 89 | +$chart1->setBottomRightPosition('H20'); |
| 90 | + |
| 91 | +// Add the chart to the worksheet |
| 92 | +$worksheet->addChart($chart1); |
| 93 | + |
| 94 | +$helper->renderChart($chart1, __FILE__); |
| 95 | + |
| 96 | +$title2 = new Title('Test Scatter Chart Zero'); |
| 97 | +$yAxisLabel2 = new Title('Value ($k)'); |
| 98 | +// Create the chart |
| 99 | +$chart2 = new Chart( |
| 100 | + 'chart2', // name |
| 101 | + $title2, // title |
| 102 | + $legend, // legend |
| 103 | + $plotArea, // plotArea |
| 104 | + true, // plotVisibleOnly |
| 105 | + DataSeries::EMPTY_AS_ZERO, // displayBlanksAs |
| 106 | + null, // xAxisLabel |
| 107 | + $yAxisLabel2 // yAxisLabel |
| 108 | +); |
| 109 | + |
| 110 | +// Set the position where the chart should appear in the worksheet |
| 111 | +$chart2->setTopLeftPosition('A22'); |
| 112 | +$chart2->setBottomRightPosition('H35'); |
| 113 | + |
| 114 | +// Add the chart to the worksheet |
| 115 | +$worksheet->addChart($chart2); |
| 116 | + |
| 117 | +$helper->renderChart($chart2, __FILE__); |
| 118 | + |
| 119 | +$title3 = new Title('Test Scatter Chart Span'); |
| 120 | +$yAxisLabel3 = new Title('Value ($k)'); |
| 121 | + |
| 122 | +// Create the chart |
| 123 | +$chart3 = new Chart( |
| 124 | + 'chart3', // name |
| 125 | + $title3, // title |
| 126 | + $legend, // legend |
| 127 | + $plotArea, // plotArea |
| 128 | + true, // plotVisibleOnly |
| 129 | + DataSeries::EMPTY_AS_SPAN, // displayBlanksAs |
| 130 | + null, // xAxisLabel |
| 131 | + $yAxisLabel3 // yAxisLabel |
| 132 | +); |
| 133 | + |
| 134 | +// Set the position where the chart should appear in the worksheet |
| 135 | +$chart3->setTopLeftPosition('A37'); |
| 136 | +$chart3->setBottomRightPosition('H50'); |
| 137 | + |
| 138 | +// Add the chart to the worksheet |
| 139 | +$worksheet->addChart($chart3); |
| 140 | + |
| 141 | +$helper->renderChart($chart3, __FILE__); |
| 142 | + |
| 143 | +// Save Excel 2007 file |
| 144 | +$helper->write($spreadsheet, __FILE__, ['Xlsx'], true); |
0 commit comments