PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.11
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.11
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_stock.php

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

114 lines 3.8 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 use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
12
13 require __DIR__ . '/../Header.php';
14
15 $spreadsheet = new Spreadsheet();
16 $worksheet = $spreadsheet->getActiveSheet();
17 $worksheet->fromArray(
18 [
19 ['Counts', 'Max', 'Min', 'Min Threshold', 'Max Threshold'],
20 [10, 10, 5, 0, 50],
21 [30, 20, 10, 0, 50],
22 [20, 30, 15, 0, 50],
23 [40, 10, 0, 0, 50],
24 [100, 40, 5, 0, 50],
25 ],
26 null,
27 'A1',
28 true
29 );
30 $worksheet->getStyle('B2:E6')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER_00);
31
32 // Set the Labels for each data series we want to plot
33 // Datatype
34 // Cell reference for data
35 // Format Code
36 // Number of datapoints in series
37 // Data values
38 // Data Marker
39 $dataSeriesLabels = [
40 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), //Max / Open
41 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), //Min / Close
42 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), //Min Threshold / Min
43 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$E$1', null, 1), //Max Threshold / Max
44 ];
45 // Set the X-Axis Labels
46 // Datatype
47 // Cell reference for data
48 // Format Code
49 // Number of datapoints in series
50 // Data values
51 // Data Marker
52 $xAxisTickValues = [
53 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', null, 5), // Counts
54 ];
55 // Set the Data values for each data series we want to plot
56 // Datatype
57 // Cell reference for data
58 // Format Code
59 // Number of datapoints in series
60 // Data values
61 // Data Marker
62 $dataSeriesValues = [
63 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$6', null, 5),
64 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', null, 5),
65 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$6', null, 5),
66 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$E$2:$E$6', null, 5),
67 ];
68
69 // Build the dataseries
70 $series = new DataSeries(
71 DataSeries::TYPE_STOCKCHART, // plotType
72 null, // plotGrouping - if we set this to not null, then xlsx throws error
73 range(0, count($dataSeriesValues) - 1), // plotOrder
74 $dataSeriesLabels, // plotLabel
75 $xAxisTickValues, // plotCategory
76 $dataSeriesValues // plotValues
77 );
78
79 // Set the series in the plot area
80 $plotArea = new PlotArea(null, [$series]);
81 // Set the chart legend
82 $legend = new Legend(Legend::POSITION_RIGHT, null, false);
83
84 $title = new Title('Test Stock Chart');
85 $xAxisLabel = new Title('Counts');
86 $yAxisLabel = new Title('Values');
87
88 // Create the chart
89 $chart = new Chart(
90 'stock-chart', // name
91 $title, // title
92 $legend, // legend
93 $plotArea, // plotArea
94 true, // plotVisibleOnly
95 0, // displayBlanksAs
96 $xAxisLabel, // xAxisLabel
97 $yAxisLabel // yAxisLabel
98 );
99
100 // Set the position where the chart should appear in the worksheet
101 $chart->setTopLeftPosition('A7');
102 $chart->setBottomRightPosition('H20');
103
104 // Add the chart to the worksheet
105 $worksheet->addChart($chart);
106
107 // Save Excel 2007 file
108 $filename = $helper->getFilename(__FILE__);
109 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
110 $writer->setIncludeCharts(true);
111 $callStartTime = microtime(true);
112 $writer->save($filename);
113 $helper->logWrite($writer, $filename, $callStartTime);
114