35_Chart_render.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $inputFileType = 'Xlsx';
  8. $inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
  9. if ((isset($argc)) && ($argc > 1)) {
  10. $inputFileNames = [];
  11. for ($i = 1; $i < $argc; ++$i) {
  12. $inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
  13. }
  14. } else {
  15. $inputFileNames = glob($inputFileNames);
  16. }
  17. foreach ($inputFileNames as $inputFileName) {
  18. $inputFileNameShort = basename($inputFileName);
  19. if (!file_exists($inputFileName)) {
  20. $helper->log('File ' . $inputFileNameShort . ' does not exist');
  21. continue;
  22. }
  23. $helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
  24. $reader = IOFactory::createReader($inputFileType);
  25. $reader->setIncludeCharts(true);
  26. $spreadsheet = $reader->load($inputFileName);
  27. $helper->log('Iterate worksheets looking at the charts');
  28. foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
  29. $sheetName = $worksheet->getTitle();
  30. $helper->log('Worksheet: ' . $sheetName);
  31. $chartNames = $worksheet->getChartNames();
  32. if (empty($chartNames)) {
  33. $helper->log(' There are no charts in this worksheet');
  34. } else {
  35. natsort($chartNames);
  36. foreach ($chartNames as $i => $chartName) {
  37. $chart = $worksheet->getChartByName($chartName);
  38. if ($chart->getTitle() !== null) {
  39. $caption = '"' . implode(' ', $chart->getTitle()->getCaption()) . '"';
  40. } else {
  41. $caption = 'Untitled';
  42. }
  43. $helper->log(' ' . $chartName . ' - ' . $caption);
  44. $jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'png');
  45. if (file_exists($jpegFile)) {
  46. unlink($jpegFile);
  47. }
  48. try {
  49. $chart->render($jpegFile);
  50. $helper->log('Rendered image: ' . $jpegFile);
  51. } catch (Exception $e) {
  52. $helper->log('Error rendering chart: ' . $e->getMessage());
  53. }
  54. }
  55. }
  56. }
  57. $spreadsheet->disconnectWorksheets();
  58. unset($spreadsheet);
  59. }
  60. $helper->log('Done rendering charts as images');