|
| 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\Layout; |
| 7 | +use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend; |
| 8 | +use PhpOffice\PhpSpreadsheet\Chart\PlotArea; |
| 9 | +use PhpOffice\PhpSpreadsheet\Chart\Title; |
| 10 | +use PhpOffice\PhpSpreadsheet\IOFactory; |
| 11 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 12 | + |
| 13 | +require __DIR__ . '/../Header.php'; |
| 14 | + |
| 15 | +$spreadsheet = new Spreadsheet(); |
| 16 | +$worksheet = $spreadsheet->getActiveSheet(); |
| 17 | +$worksheet->fromArray( |
| 18 | + [ |
| 19 | + ['', 2010, 2011, 2012], |
| 20 | + ['Q1', 12, 15, 21], |
| 21 | + ['Q2', 56, 73, 86], |
| 22 | + ['Q3', 52, 61, 69], |
| 23 | + ['Q4', 30, 32, 0], |
| 24 | + ] |
| 25 | +); |
| 26 | + |
| 27 | +// Custom colors for dataSeries (gray, blue, red, orange) |
| 28 | +$colors = [ |
| 29 | + 'cccccc', '00abb8', 'b8292f', 'eb8500', |
| 30 | +]; |
| 31 | + |
| 32 | +// Set the Labels for each data series we want to plot |
| 33 | +// Datatype |
| 34 | +// Cell reference for data |
| 35 | +// Format Code |
| 36 | +// Number of datapoints in series |
| 37 | +// Data values |
| 38 | +// Data Marker |
| 39 | +$dataSeriesLabels1 = [ |
| 40 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 |
| 41 | +]; |
| 42 | +// Set the X-Axis Labels |
| 43 | +// Datatype |
| 44 | +// Cell reference for data |
| 45 | +// Format Code |
| 46 | +// Number of datapoints in series |
| 47 | +// Data values |
| 48 | +// Data Marker |
| 49 | +$xAxisTickValues1 = [ |
| 50 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
| 51 | +]; |
| 52 | +// Set the Data values for each data series we want to plot |
| 53 | +// Datatype |
| 54 | +// Cell reference for data |
| 55 | +// Format Code |
| 56 | +// Number of datapoints in series |
| 57 | +// Data values |
| 58 | +// Data Marker |
| 59 | +// Custom colors |
| 60 | +$dataSeriesValues1 = [ |
| 61 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4, [], null, $colors), |
| 62 | +]; |
| 63 | + |
| 64 | +// Build the dataseries |
| 65 | +$series1 = new DataSeries( |
| 66 | + DataSeries::TYPE_BARCHART, // plotType |
| 67 | + null, // plotGrouping (Pie charts don't have any grouping) |
| 68 | + range(0, count($dataSeriesValues1) - 1), // plotOrder |
| 69 | + $dataSeriesLabels1, // plotLabel |
| 70 | + $xAxisTickValues1, // plotCategory |
| 71 | + $dataSeriesValues1 // plotValues |
| 72 | +); |
| 73 | + |
| 74 | +// Set up a layout object for the Pie chart |
| 75 | +$layout1 = new Layout(); |
| 76 | +$layout1->setShowVal(true); |
| 77 | +$layout1->setShowPercent(true); |
| 78 | + |
| 79 | +// Set the series in the plot area |
| 80 | +$plotArea1 = new PlotArea($layout1, [$series1]); |
| 81 | +// Set the chart legend |
| 82 | +$legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false); |
| 83 | + |
| 84 | +$title1 = new Title('Test Bar Chart'); |
| 85 | + |
| 86 | +// Create the chart |
| 87 | +$chart1 = new Chart( |
| 88 | + 'chart1', // name |
| 89 | + $title1, // title |
| 90 | + $legend1, // legend |
| 91 | + $plotArea1, // plotArea |
| 92 | + true, // plotVisibleOnly |
| 93 | + DataSeries::EMPTY_AS_GAP, // displayBlanksAs |
| 94 | + null, // xAxisLabel |
| 95 | + null // yAxisLabel - Pie charts don't have a Y-Axis |
| 96 | +); |
| 97 | + |
| 98 | +// Set the position where the chart should appear in the worksheet |
| 99 | +$chart1->setTopLeftPosition('A7'); |
| 100 | +$chart1->setBottomRightPosition('H20'); |
| 101 | + |
| 102 | +// Add the chart to the worksheet |
| 103 | +$worksheet->addChart($chart1); |
| 104 | + |
| 105 | +// Set the Labels for each data series we want to plot |
| 106 | +// Datatype |
| 107 | +// Cell reference for data |
| 108 | +// Format Code |
| 109 | +// Number of datapoints in series |
| 110 | +// Data values |
| 111 | +// Data Marker |
| 112 | +$dataSeriesLabels2 = [ |
| 113 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 |
| 114 | +]; |
| 115 | +// Set the X-Axis Labels |
| 116 | +// Datatype |
| 117 | +// Cell reference for data |
| 118 | +// Format Code |
| 119 | +// Number of datapoints in series |
| 120 | +// Data values |
| 121 | +// Data Marker |
| 122 | +$xAxisTickValues2 = [ |
| 123 | + new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
| 124 | +]; |
| 125 | +// Set the Data values for each data series we want to plot |
| 126 | +// Datatype |
| 127 | +// Cell reference for data |
| 128 | +// Format Code |
| 129 | +// Number of datapoints in series |
| 130 | +// Data values |
| 131 | +// Data Marker |
| 132 | +// Custom colors |
| 133 | +$dataSeriesValues2 = [ |
| 134 | + $dataSeriesValues2Element = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), |
| 135 | +]; |
| 136 | +$dataSeriesValues2Element->setFillColor($colors); |
| 137 | + |
| 138 | +// Build the dataseries |
| 139 | +$series2 = new DataSeries( |
| 140 | + DataSeries::TYPE_DONUTCHART, // plotType |
| 141 | + null, // plotGrouping (Donut charts don't have any grouping) |
| 142 | + range(0, count($dataSeriesValues2) - 1), // plotOrder |
| 143 | + $dataSeriesLabels2, // plotLabel |
| 144 | + $xAxisTickValues2, // plotCategory |
| 145 | + $dataSeriesValues2 // plotValues |
| 146 | +); |
| 147 | + |
| 148 | +// Set up a layout object for the Pie chart |
| 149 | +$layout2 = new Layout(); |
| 150 | +$layout2->setShowVal(true); |
| 151 | +$layout2->setShowCatName(true); |
| 152 | + |
| 153 | +// Set the series in the plot area |
| 154 | +$plotArea2 = new PlotArea($layout2, [$series2]); |
| 155 | + |
| 156 | +$title2 = new Title('Test Donut Chart'); |
| 157 | + |
| 158 | +// Create the chart |
| 159 | +$chart2 = new Chart( |
| 160 | + 'chart2', // name |
| 161 | + $title2, // title |
| 162 | + null, // legend |
| 163 | + $plotArea2, // plotArea |
| 164 | + true, // plotVisibleOnly |
| 165 | + DataSeries::EMPTY_AS_GAP, // displayBlanksAs |
| 166 | + null, // xAxisLabel |
| 167 | + null // yAxisLabel - Like Pie charts, Donut charts don't have a Y-Axis |
| 168 | +); |
| 169 | + |
| 170 | +// Set the position where the chart should appear in the worksheet |
| 171 | +$chart2->setTopLeftPosition('I7'); |
| 172 | +$chart2->setBottomRightPosition('P20'); |
| 173 | + |
| 174 | +// Add the chart to the worksheet |
| 175 | +$worksheet->addChart($chart2); |
| 176 | + |
| 177 | +// Save Excel 2007 file |
| 178 | +$filename = $helper->getFilename(__FILE__); |
| 179 | +$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
| 180 | +$writer->setIncludeCharts(true); |
| 181 | +$callStartTime = microtime(true); |
| 182 | +$writer->save($filename); |
| 183 | +$helper->logWrite($writer, $filename, $callStartTime); |
0 commit comments