PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.5.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.5.1
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / phpoffice / phpspreadsheet / samples / templates / chartSpreadsheet.php

chartSpreadsheet.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.5.1, at vendor/phpoffice/phpspreadsheet/samples/templates/chartSpreadsheet.php

99 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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;
7 use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
8 use PhpOffice\PhpSpreadsheet\Chart\Title;
9 use PhpOffice\PhpSpreadsheet\Spreadsheet;
10
11 $spreadsheet = new Spreadsheet();
12 $worksheet = $spreadsheet->getActiveSheet();
13 $worksheet->fromArray(
14 [
15 ['', 2010, 2011, 2012],
16 ['Q1', 12, 15, 21],
17 ['Q2', 56, 73, 86],
18 ['Q3', 52, 61, 69],
19 ['Q4', 30, 32, 0],
20 ]
21 );
22
23 // Set the Labels for each data series we want to plot
24 // Datatype
25 // Cell reference for data
26 // Format Code
27 // Number of datapoints in series
28 // Data values
29 // Data Marker
30 $dataSeriesLabels = [
31 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
32 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
33 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
34 ];
35 // Set the X-Axis Labels
36 // Datatype
37 // Cell reference for data
38 // Format Code
39 // Number of datapoints in series
40 // Data values
41 // Data Marker
42 $xAxisTickValues = [
43 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
44 ];
45 // Set the Data values for each data series we want to plot
46 // Datatype
47 // Cell reference for data
48 // Format Code
49 // Number of datapoints in series
50 // Data values
51 // Data Marker
52 $dataSeriesValues = [
53 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
54 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
55 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
56 ];
57
58 // Build the dataseries
59 $series = new DataSeries(
60 DataSeries::TYPE_BARCHART, // plotType
61 DataSeries::GROUPING_CLUSTERED, // plotGrouping
62 range(0, count($dataSeriesValues) - 1), // plotOrder
63 $dataSeriesLabels, // plotLabel
64 $xAxisTickValues, // plotCategory
65 $dataSeriesValues // plotValues
66 );
67 // Set additional dataseries parameters
68 // Make it a horizontal bar rather than a vertical column graph
69 $series->setPlotDirection(DataSeries::DIRECTION_BAR);
70
71 // Set the series in the plot area
72 $plotArea = new PlotArea(null, [$series]);
73 // Set the chart legend
74 $legend = new Legend(Legend::POSITION_RIGHT, null, false);
75
76 $title = new Title('Test Bar Chart');
77 $yAxisLabel = new Title('Value ($k)');
78
79 // Create the chart
80 $chart = new Chart(
81 'chart1', // name
82 $title, // title
83 $legend, // legend
84 $plotArea, // plotArea
85 true, // plotVisibleOnly
86 0, // displayBlanksAs
87 null, // xAxisLabel
88 $yAxisLabel // yAxisLabel
89 );
90
91 // Set the position where the chart should appear in the worksheet
92 $chart->setTopLeftPosition('A7');
93 $chart->setBottomRightPosition('H20');
94
95 // Add the chart to the worksheet
96 $worksheet->addChart($chart);
97
98 return $spreadsheet;
99