Completed
Push — master ( dfd9c5...ccebf0 )
by Mark
161:27 queued 155:49
created

samples/Chart/33_Chart_create_pie.php (1 issue)

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
This use statement conflicts with another class in this namespace, Legend. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

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