PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.7.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.7.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 / Chart / 33_Chart_create_scatter.php

33_Chart_create_scatter.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.7.1, at vendor/phpoffice/phpspreadsheet/samples/Chart/33_Chart_create_scatter.php

102 lines 3.2 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\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 $dataSeriesLabels = [
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 $xAxisTickValues = [
40 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
41 ];
42 // Set the Data values for each data series we want to plot
43 // Datatype
44 // Cell reference for data
45 // Format Code
46 // Number of datapoints in series
47 // Data values
48 // Data Marker
49 $dataSeriesValues = [
50 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
51 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
52 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
53 ];
54
55 // Build the dataseries
56 $series = new DataSeries(
57 DataSeries::TYPE_SCATTERCHART, // plotType
58 null, // plotGrouping (Scatter charts don't have any grouping)
59 range(0, count($dataSeriesValues) - 1), // plotOrder
60 $dataSeriesLabels, // plotLabel
61 $xAxisTickValues, // plotCategory
62 $dataSeriesValues, // plotValues
63 null, // plotDirection
64 null, // smooth line
65 DataSeries::STYLE_LINEMARKER // plotStyle
66 );
67
68 // Set the series in the plot area
69 $plotArea = new PlotArea(null, [$series]);
70 // Set the chart legend
71 $legend = new Legend(Legend::POSITION_TOPRIGHT, null, false);
72
73 $title = new Title('Test Scatter Chart');
74 $yAxisLabel = new Title('Value ($k)');
75
76 // Create the chart
77 $chart = new Chart(
78 'chart1', // name
79 $title, // title
80 $legend, // legend
81 $plotArea, // plotArea
82 true, // plotVisibleOnly
83 0, // displayBlanksAs
84 null, // xAxisLabel
85 $yAxisLabel // yAxisLabel
86 );
87
88 // Set the position where the chart should appear in the worksheet
89 $chart->setTopLeftPosition('A7');
90 $chart->setBottomRightPosition('H20');
91
92 // Add the chart to the worksheet
93 $worksheet->addChart($chart);
94
95 // Save Excel 2007 file
96 $filename = $helper->getFilename(__FILE__);
97 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
98 $writer->setIncludeCharts(true);
99 $callStartTime = microtime(true);
100 $writer->save($filename);
101 $helper->logWrite($writer, $filename, $callStartTime);
102