-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy path35_Chart_render33.php
More file actions
100 lines (84 loc) · 3.38 KB
/
35_Chart_render33.php
File metadata and controls
100 lines (84 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Settings;
require __DIR__ . '/../Header.php';
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
// Change these values to select the Rendering library that you wish to use
//Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
Settings::setChartRenderer(PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::class);
$inputFileType = 'Xlsx';
$inputFileNamesString = $helper->getTemporaryFolder() . '/33_Chart_create_*.xlsx';
if ((isset($argc)) && ($argc > 1)) {
$inputFileNames = [];
for ($i = 1; $i < $argc; ++$i) {
$inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
}
} else {
$inputFileNames = glob($inputFileNamesString) ?: [];
}
if (count($inputFileNames) === 1) {
/** @var string[] */
$unresolvedErrors = [];
} else {
/** @var string[] */
$unresolvedErrors = [
//'33_Chart_create_bar_stacked.xlsx', // fixed with mitoteam/jpgraph 10.3
];
}
foreach ($inputFileNames as $inputFileName) {
$inputFileNameShort = basename($inputFileName);
if (!file_exists($inputFileName)) {
$helper->log('File ' . $inputFileNameShort . ' does not exist');
continue;
}
if (in_array($inputFileNameShort, $unresolvedErrors, true)) {
$helper->log('*****');
$helper->log('***** File ' . $inputFileNameShort . ' does not yet work with this script');
$helper->log('*****');
continue;
}
$helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
$reader = IOFactory::createReader($inputFileType);
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($inputFileName);
$helper->log('Iterate worksheets looking at the charts');
$renderedCharts = 0;
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$sheetName = $worksheet->getTitle();
$helper->log('Worksheet: ' . $sheetName);
$chartNames = $worksheet->getChartNames();
if (empty($chartNames)) {
$helper->log(' There are no charts in this worksheet');
} else {
natsort($chartNames);
foreach ($chartNames as $j => $chartName) {
$i = $renderedCharts + $j;
$chart = $worksheet->getChartByNameOrThrow($chartName);
if ($chart->getTitle() !== null) {
$caption = '"' . $chart->getTitle()->getCaptionText($spreadsheet) . '"';
} else {
$caption = 'Untitled';
}
$helper->log(' ' . $chartName . ' - ' . $caption);
$pngFile = $helper->getFilename('35-' . $inputFileNameShort, 'png');
if ($i !== 0) {
$pngFile = substr($pngFile, 0, -3) . "$i.png";
}
if (file_exists($pngFile)) {
unlink($pngFile);
}
try {
$chart->render($pngFile);
$helper->log('Rendered image: ' . $pngFile);
} catch (Exception $e) {
$helper->log('Error rendering chart: ' . $e->getMessage());
}
++$renderedCharts;
}
}
}
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
gc_collect_cycles();
}
$helper->log('Done rendering charts as images');