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

samples/Chart/33_Chart_create_column_2.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\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...
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
            ['', '', 'Budget', 'Forecast', 'Actual'],
19
            ['2010', 'Q1', 47, 44, 43],
20
            ['', 'Q2', 56, 53, 50],
21
            ['', 'Q3', 52, 46, 45],
22
            ['', 'Q4', 45, 40, 40],
23
            ['2011', 'Q1', 51, 42, 46],
24
            ['', 'Q2', 53, 58, 56],
25
            ['', 'Q3', 64, 66, 69],
26
            ['', 'Q4', 54, 55, 56],
27
            ['2012', 'Q1', 49, 52, 58],
28
            ['', 'Q2', 68, 73, 86],
29
            ['', 'Q3', 72, 78, 0],
30
            ['', 'Q4', 50, 60, 0],
31
        ]
32
);
33
34
// Set the Labels for each data series we want to plot
35
//     Datatype
36
//     Cell reference for data
37
//     Format Code
38
//     Number of datapoints in series
39
//     Data values
40
//     Data Marker
41
$dataSeriesLabels = [
42
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 'Budget'
43
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 'Forecast'
44
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$E$1', null, 1), // 'Actual'
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:$B$13', null, 12), // Q1 to Q4 for 2010 to 2012
55
];
56
// Set the Data values for each data series we want to plot
57
//     Datatype
58
//     Cell reference for data
59
//     Format Code
60
//     Number of datapoints in series
61
//     Data values
62
//     Data Marker
63
$dataSeriesValues = [
64
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$13', null, 12),
65
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$13', null, 12),
66
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$E$2:$E$13', null, 12),
67
];
68
69
// Build the dataseries
70
$series = new DataSeries(
71
    DataSeries::TYPE_BARCHART, // plotType
72
    DataSeries::GROUPING_CLUSTERED, // plotGrouping
73
    range(0, count($dataSeriesValues) - 1), // plotOrder
74
    $dataSeriesLabels, // plotLabel
75
    $xAxisTickValues, // plotCategory
76
    $dataSeriesValues        // plotValues
77
);
78
// Set additional dataseries parameters
79
//     Make it a vertical column rather than a horizontal bar graph
80
$series->setPlotDirection(DataSeries::DIRECTION_COL);
81
82
// Set the series in the plot area
83
$plotArea = new PlotArea(null, [$series]);
84
// Set the chart legend
85
$legend = new Legend(Legend::POSITION_BOTTOM, null, false);
86
87
$title = new Title('Test Grouped Column Chart');
88
$xAxisLabel = new Title('Financial Period');
89
$yAxisLabel = new Title('Value ($k)');
90
91
// Create the chart
92
$chart = new Chart(
93
    'chart1', // name
94
    $title, // title
95
    $legend, // legend
96
    $plotArea, // plotArea
97
    true, // plotVisibleOnly
98
    0, // displayBlanksAs
99
    $xAxisLabel, // xAxisLabel
100
    $yAxisLabel  // yAxisLabel
101
);
102
103
// Set the position where the chart should appear in the worksheet
104
$chart->setTopLeftPosition('G2');
105
$chart->setBottomRightPosition('P20');
106
107
// Add the chart to the worksheet
108
$worksheet->addChart($chart);
109
110
// Save Excel 2007 file
111
$filename = $helper->getFilename(__FILE__);
112
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
113
$writer->setIncludeCharts(true);
114
$callStartTime = microtime(true);
115
$writer->save($filename);
116
$helper->logWrite($writer, $filename, $callStartTime);
117