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; |
||
0 ignored issues
–
show
|
|||
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 | ['Jan', 47, 45, 71], |
||
21 | ['Feb', 56, 73, 86], |
||
22 | ['Mar', 52, 61, 69], |
||
23 | ['Apr', 40, 52, 60], |
||
24 | ['May', 42, 55, 71], |
||
25 | ['Jun', 58, 63, 76], |
||
26 | ['Jul', 53, 61, 89], |
||
27 | ['Aug', 46, 69, 85], |
||
28 | ['Sep', 62, 75, 81], |
||
29 | ['Oct', 51, 70, 96], |
||
30 | ['Nov', 55, 66, 89], |
||
31 | ['Dec', 68, 62, 0], |
||
32 | ] |
||
33 | ); |
||
34 | |||
35 | // Set the Labels for each data series we want to plot |
||
36 | // Datatype |
||
37 | // Cell reference for data |
||
38 | // Format Code |
||
39 | // Number of datapoints in series |
||
40 | // Data values |
||
41 | // Data Marker |
||
42 | $dataSeriesLabels = [ |
||
43 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011 |
||
44 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012 |
||
45 | ]; |
||
46 | // Set the X-Axis Labels |
||
47 | // Datatype |
||
48 | // Cell reference for data |
||
49 | // Format Code |
||
50 | // Number of datapoints in series |
||
51 | // Data values |
||
52 | // Data Marker |
||
53 | $xAxisTickValues = [ |
||
54 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec |
||
55 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec |
||
56 | ]; |
||
57 | // Set the Data values for each data series we want to plot |
||
58 | // Datatype |
||
59 | // Cell reference for data |
||
60 | // Format Code |
||
61 | // Number of datapoints in series |
||
62 | // Data values |
||
63 | // Data Marker |
||
64 | $dataSeriesValues = [ |
||
65 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$13', null, 12), |
||
66 | new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$13', null, 12), |
||
67 | ]; |
||
68 | |||
69 | // Build the dataseries |
||
70 | $series = new DataSeries( |
||
71 | DataSeries::TYPE_RADARCHART, // plotType |
||
72 | null, // plotGrouping (Radar charts don't have any grouping) |
||
73 | range(0, count($dataSeriesValues) - 1), // plotOrder |
||
74 | $dataSeriesLabels, // plotLabel |
||
75 | $xAxisTickValues, // plotCategory |
||
76 | $dataSeriesValues, // plotValues |
||
77 | null, // plotDirection |
||
78 | null, // smooth line |
||
79 | DataSeries::STYLE_MARKER // plotStyle |
||
80 | ); |
||
81 | |||
82 | // Set up a layout object for the Pie chart |
||
83 | $layout = new Layout(); |
||
84 | |||
85 | // Set the series in the plot area |
||
86 | $plotArea = new PlotArea($layout, [$series]); |
||
87 | // Set the chart legend |
||
88 | $legend = new Legend(Legend::POSITION_RIGHT, null, false); |
||
89 | |||
90 | $title = new Title('Test Radar Chart'); |
||
91 | |||
92 | // Create the chart |
||
93 | $chart = new Chart( |
||
94 | 'chart1', // name |
||
95 | $title, // title |
||
96 | $legend, // legend |
||
97 | $plotArea, // plotArea |
||
98 | true, // plotVisibleOnly |
||
99 | 0, // displayBlanksAs |
||
100 | null, // xAxisLabel |
||
101 | null // yAxisLabel - Radar charts don't have a Y-Axis |
||
102 | ); |
||
103 | |||
104 | // Set the position where the chart should appear in the worksheet |
||
105 | $chart->setTopLeftPosition('F2'); |
||
106 | $chart->setBottomRightPosition('M15'); |
||
107 | |||
108 | // Add the chart to the worksheet |
||
109 | $worksheet->addChart($chart); |
||
110 | |||
111 | // Save Excel 2007 file |
||
112 | $filename = $helper->getFilename(__FILE__); |
||
113 | $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
||
114 | $writer->setIncludeCharts(true); |
||
115 | $callStartTime = microtime(true); |
||
116 | $writer->save($filename); |
||
117 | $helper->logWrite($writer, $filename, $callStartTime); |
||
118 |
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: