35_Chart_render.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. use PhpOffice\PhpSpreadsheet\Settings;
  4. require __DIR__ . '/../Header.php';
  5. // Change these values to select the Rendering library that you wish to use
  6. //Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
  7. Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::class);
  8. $inputFileType = 'Xlsx';
  9. $inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
  10. //$inputFileNames = __DIR__ . '/../templates/32readwriteStockChart5.xlsx';
  11. if ((isset($argc)) && ($argc > 1)) {
  12. $inputFileNames = [];
  13. for ($i = 1; $i < $argc; ++$i) {
  14. $inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
  15. }
  16. } else {
  17. $inputFileNames = glob($inputFileNames);
  18. }
  19. if (count($inputFileNames) === 1) {
  20. $unresolvedErrors = [];
  21. } else {
  22. $unresolvedErrors = [
  23. // The following spreadsheet was created by 3rd party software,
  24. // and doesn't include the data that usually accompanies a chart.
  25. // That is good enough for Excel, but not for JpGraph.
  26. '32readwriteBubbleChart2.xlsx',
  27. ];
  28. }
  29. foreach ($inputFileNames as $inputFileName) {
  30. $inputFileNameShort = basename($inputFileName);
  31. if (!file_exists($inputFileName)) {
  32. $helper->log('File ' . $inputFileNameShort . ' does not exist');
  33. continue;
  34. }
  35. if (in_array($inputFileNameShort, $unresolvedErrors, true)) {
  36. $helper->log('*****');
  37. $helper->log('***** File ' . $inputFileNameShort . ' does not yet work with this script');
  38. $helper->log('*****');
  39. continue;
  40. }
  41. $helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
  42. $reader = IOFactory::createReader($inputFileType);
  43. $reader->setIncludeCharts(true);
  44. $spreadsheet = $reader->load($inputFileName);
  45. $helper->log('Iterate worksheets looking at the charts');
  46. foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
  47. $sheetName = $worksheet->getTitle();
  48. $helper->log('Worksheet: ' . $sheetName);
  49. $chartNames = $worksheet->getChartNames();
  50. if (empty($chartNames)) {
  51. $helper->log(' There are no charts in this worksheet');
  52. } else {
  53. natsort($chartNames);
  54. foreach ($chartNames as $i => $chartName) {
  55. $chart = $worksheet->getChartByName($chartName);
  56. if ($chart->getTitle() !== null) {
  57. $caption = '"' . implode(' ', $chart->getTitle()->getCaption()) . '"';
  58. } else {
  59. $caption = 'Untitled';
  60. }
  61. $helper->log(' ' . $chartName . ' - ' . $caption);
  62. $jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'png');
  63. if ($i !== 0) {
  64. $jpegFile = substr($jpegFile, 0, -3) . "$i.png";
  65. }
  66. if (file_exists($jpegFile)) {
  67. unlink($jpegFile);
  68. }
  69. try {
  70. $chart->render($jpegFile);
  71. $helper->log('Rendered image: ' . $jpegFile);
  72. } catch (Exception $e) {
  73. $helper->log('Error rendering chart: ' . $e->getMessage());
  74. }
  75. }
  76. }
  77. }
  78. $spreadsheet->disconnectWorksheets();
  79. unset($spreadsheet);
  80. gc_collect_cycles();
  81. }
  82. $helper->log('Done rendering charts as images');