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; |
||
0 ignored issues
–
show
|
|||
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 | ['', 2010, 2011, 2012], |
||
19 | ['Q1', 12, 15, 21], |
||
20 | ['Q2', 56, 73, 86], |
||
21 | ['Q3', 52, 61, 69], |
||
22 | ['Q4', 30, 32, 0], |
||
23 | ] |
||
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 | $dataSeriesLabels1 = [ |
||
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 | // Datatype |
||
40 | // Cell reference for data |
||
41 | // Format Code |
||
42 | // Number of datapoints in series |
||
43 | // Data values |
||
44 | // Data Marker |
||
45 | $xAxisTickValues1 = [ |
||
46 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
||
47 | ]; |
||
48 | // Set the Data values for each data series we want to plot |
||
49 | // Datatype |
||
50 | // Cell reference for data |
||
51 | // Format Code |
||
52 | // Number of datapoints in series |
||
53 | // Data values |
||
54 | // Data Marker |
||
55 | $dataSeriesValues1 = [ |
||
56 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4), |
||
57 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), |
||
58 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), |
||
59 | ]; |
||
60 | |||
61 | // Build the dataseries |
||
62 | $series1 = new DataSeries( |
||
63 | DataSeries::TYPE_AREACHART, // plotType |
||
64 | DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping |
||
65 | range(0, count($dataSeriesValues1) - 1), // plotOrder |
||
66 | $dataSeriesLabels1, // plotLabel |
||
67 | $xAxisTickValues1, // plotCategory |
||
68 | $dataSeriesValues1 // plotValues |
||
69 | ); |
||
70 | |||
71 | // Set the series in the plot area |
||
72 | $plotArea1 = new PlotArea(null, [$series1]); |
||
73 | // Set the chart legend |
||
74 | $legend1 = new Legend(Legend::POSITION_TOPRIGHT, null, false); |
||
75 | |||
76 | $title1 = new Title('Test %age-Stacked Area Chart'); |
||
77 | $yAxisLabel1 = new Title('Value ($k)'); |
||
78 | |||
79 | // Create the chart |
||
80 | $chart1 = new Chart( |
||
81 | 'chart1', // name |
||
82 | $title1, // title |
||
83 | $legend1, // legend |
||
84 | $plotArea1, // plotArea |
||
85 | true, // plotVisibleOnly |
||
86 | 0, // displayBlanksAs |
||
87 | null, // xAxisLabel |
||
88 | $yAxisLabel1 // yAxisLabel |
||
89 | ); |
||
90 | |||
91 | // Set the position where the chart should appear in the worksheet |
||
92 | $chart1->setTopLeftPosition('A7'); |
||
93 | $chart1->setBottomRightPosition('H20'); |
||
94 | |||
95 | // Add the chart to the worksheet |
||
96 | $worksheet->addChart($chart1); |
||
97 | |||
98 | // Set the Labels for each data series we want to plot |
||
99 | // Datatype |
||
100 | // Cell reference for data |
||
101 | // Format Code |
||
102 | // Number of datapoints in series |
||
103 | // Data values |
||
104 | // Data Marker |
||
105 | $dataSeriesLabels2 = [ |
||
106 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010 |
||
107 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 |
||
108 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012 |
||
109 | ]; |
||
110 | // Set the X-Axis Labels |
||
111 | // Datatype |
||
112 | // Cell reference for data |
||
113 | // Format Code |
||
114 | // Number of datapoints in series |
||
115 | // Data values |
||
116 | // Data Marker |
||
117 | $xAxisTickValues2 = [ |
||
118 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 |
||
119 | ]; |
||
120 | // Set the Data values for each data series we want to plot |
||
121 | // Datatype |
||
122 | // Cell reference for data |
||
123 | // Format Code |
||
124 | // Number of datapoints in series |
||
125 | // Data values |
||
126 | // Data Marker |
||
127 | $dataSeriesValues2 = [ |
||
128 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4), |
||
129 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), |
||
130 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), |
||
131 | ]; |
||
132 | |||
133 | // Build the dataseries |
||
134 | $series2 = new DataSeries( |
||
135 | DataSeries::TYPE_BARCHART, // plotType |
||
136 | DataSeries::GROUPING_STANDARD, // plotGrouping |
||
137 | range(0, count($dataSeriesValues2) - 1), // plotOrder |
||
138 | $dataSeriesLabels2, // plotLabel |
||
139 | $xAxisTickValues2, // plotCategory |
||
140 | $dataSeriesValues2 // plotValues |
||
141 | ); |
||
142 | // Set additional dataseries parameters |
||
143 | // Make it a vertical column rather than a horizontal bar graph |
||
144 | $series2->setPlotDirection(DataSeries::DIRECTION_COL); |
||
145 | |||
146 | // Set the series in the plot area |
||
147 | $plotArea2 = new PlotArea(null, [$series2]); |
||
148 | // Set the chart legend |
||
149 | $legend2 = new Legend(Legend::POSITION_RIGHT, null, false); |
||
150 | |||
151 | $title2 = new Title('Test Column Chart'); |
||
152 | $yAxisLabel2 = new Title('Value ($k)'); |
||
153 | |||
154 | // Create the chart |
||
155 | $chart2 = new Chart( |
||
156 | 'chart2', // name |
||
157 | $title2, // title |
||
158 | $legend2, // legend |
||
159 | $plotArea2, // plotArea |
||
160 | true, // plotVisibleOnly |
||
161 | 0, // displayBlanksAs |
||
162 | null, // xAxisLabel |
||
163 | $yAxisLabel2 // yAxisLabel |
||
164 | ); |
||
165 | |||
166 | // Set the position where the chart should appear in the worksheet |
||
167 | $chart2->setTopLeftPosition('I7'); |
||
168 | $chart2->setBottomRightPosition('P20'); |
||
169 | |||
170 | // Add the chart to the worksheet |
||
171 | $worksheet->addChart($chart2); |
||
172 | |||
173 | // Save Excel 2007 file |
||
174 | $filename = $helper->getFilename(__FILE__); |
||
175 | $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
||
176 | $writer->setIncludeCharts(true); |
||
177 | $callStartTime = microtime(true); |
||
178 | $writer->save($filename); |
||
179 | $helper->logWrite($writer, $filename, $callStartTime); |
||
180 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: