Skip to content

Commit 782b4e4

Browse files
committed
Upgrade chart rendering support to be composer based
This allow to get rid of manual class loading and have simpler usage of the library.
1 parent dc9f432 commit 782b4e4

File tree

14 files changed

+244
-216
lines changed

14 files changed

+244
-216
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"phpunit/phpunit": "^5.7",
5555
"dompdf/dompdf": "^0.8.0",
5656
"mpdf/mpdf": "^7.0.0",
57-
"friendsofphp/php-cs-fixer": "^2.7"
57+
"friendsofphp/php-cs-fixer": "^2.7",
58+
"jpgraph/jpgraph": "^4.0"
5859
},
5960
"suggest": {
6061
"ext-gd": "Required for exact column width autocalculation",

composer.lock

Lines changed: 41 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/topics/migration-from-PHPExcel.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,40 @@ $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf')
199199
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
200200
```
201201

202+
### Rendering charts
203+
204+
When rendering charts for HTML or PDF outputs, the process was also simplified. And while
205+
JpGraph support is still available, it is unfortunately not up to date for latest PHP versions
206+
and it will generate various warnings.
207+
208+
If you rely on this feature, please consider
209+
contributing either patches to JpGraph or another `IRenderer` implementation (a good
210+
candidate might be [CpChart](https://github.com/szymach/c-pchart)).
211+
212+
Before:
213+
214+
```php
215+
$rendererName = \PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
216+
$rendererLibrary = 'jpgraph3.5.0b1/src/';
217+
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
218+
219+
\PHPExcel_Settings::setChartRenderer($rendererName, $rendererLibraryPath);
220+
```
221+
222+
After:
223+
224+
Require the dependency via composer:
225+
226+
```sh
227+
composer require jpgraph/jpgraph
228+
```
229+
230+
And then:
231+
232+
```php
233+
Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
234+
```
235+
202236
### PclZip and ZipArchive
203237

204238
Support for PclZip were dropped in favor of the more complete and modern

samples/Chart/32_Chart_read_write_HTML.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
require __DIR__ . '/../Header.php';
77

88
// Change these values to select the Rendering library that you wish to use
9-
// and its directory location on your server
10-
$rendererName = Settings::CHART_RENDERER_JPGRAPH;
11-
$rendererLibrary = 'jpgraph3.5.0b1/src/';
12-
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
13-
14-
if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) {
15-
$helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure');
16-
17-
return;
18-
}
9+
Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
1910

2011
$inputFileType = 'Xlsx';
2112
$inputFileNames = __DIR__ . '/../templates/36write*.xlsx';
@@ -86,7 +77,7 @@
8677
}
8778

8879
// Save
89-
$filename = $helper->getFilename($inputFileName);
80+
$filename = $helper->getFilename($inputFileName, 'html');
9081
$writer = IOFactory::createWriter($spreadsheet, 'Html');
9182
$writer->setIncludeCharts(true);
9283
$callStartTime = microtime(true);

samples/Chart/32_Chart_read_write_PDF.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@
88
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
99

1010
// Change these values to select the Rendering library that you wish to use
11-
// for Chart images, and its directory location on your server
12-
$rendererName = Settings::CHART_RENDERER_JPGRAPH;
13-
$rendererLibrary = 'jpgraph3.5.0b1/src/';
14-
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
15-
16-
if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) {
17-
$helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure');
18-
19-
return;
20-
}
11+
Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
2112

2213
$inputFileType = 'Xlsx';
2314
$inputFileNames = __DIR__ . '/../templates/36write*.xlsx';
@@ -88,7 +79,7 @@
8879
}
8980

9081
// Save
91-
$filename = $helper->getFilename($inputFileName);
82+
$filename = $helper->getFilename($inputFileName, 'pdf');
9283
$writer = IOFactory::createWriter($spreadsheet, 'Pdf');
9384
$writer->setIncludeCharts(true);
9485
$callStartTime = microtime(true);

samples/Chart/35_Chart_render.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
require __DIR__ . '/../Header.php';
77

88
// Change these values to select the Rendering library that you wish to use
9-
// and its directory location on your server
10-
$rendererName = Settings::CHART_RENDERER_JPGRAPH;
11-
$rendererLibrary = 'jpgraph3.5.0b1/src/';
12-
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
13-
14-
if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) {
15-
$helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure');
16-
17-
return;
18-
}
9+
Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
1910

2011
$inputFileType = 'Xlsx';
2112
$inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
@@ -62,13 +53,14 @@
6253
}
6354
$helper->log(' ' . $chartName . ' - ' . $caption);
6455

65-
$jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'jpg');
56+
$jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'png');
6657
if (file_exists($jpegFile)) {
6758
unlink($jpegFile);
6859
}
6960

7061
try {
7162
$chart->render($jpegFile);
63+
$helper->log('Rendered image: ' . $jpegFile);
7264
} catch (Exception $e) {
7365
$helper->log('Error rendering chart: ' . $e->getMessage());
7466
}

src/PhpSpreadsheet/Chart.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -640,28 +640,28 @@ public function refresh()
640640
}
641641
}
642642

643+
/**
644+
* Render the chart to given file (or stream).
645+
*
646+
* @param string $outputDestination Name of the file render to
647+
*
648+
* @return bool true on success
649+
*/
643650
public function render($outputDestination = null)
644651
{
645-
$libraryName = Settings::getChartRendererName();
646-
if ($libraryName === null) {
647-
return false;
652+
if ($outputDestination == 'php://output') {
653+
$outputDestination = null;
648654
}
649-
// Ensure that data series values are up-to-date before we render
650-
$this->refresh();
651655

652-
$libraryPath = Settings::getChartRendererPath();
653-
$includePath = str_replace('\\', '/', get_include_path());
654-
$rendererPath = str_replace('\\', '/', $libraryPath);
655-
if (strpos($rendererPath, $includePath) === false) {
656-
set_include_path(get_include_path() . PATH_SEPARATOR . $libraryPath);
656+
$libraryName = Settings::getChartRenderer();
657+
if ($libraryName === null) {
658+
return false;
657659
}
658660

659-
$rendererName = '\\PhpOffice\\PhpSpreadsheet\\Chart\\Renderer\\' . $libraryName;
660-
$renderer = new $rendererName($this);
661+
// Ensure that data series values are up-to-date before we render
662+
$this->refresh();
661663

662-
if ($outputDestination == 'php://output') {
663-
$outputDestination = null;
664-
}
664+
$renderer = new $libraryName($this);
665665

666666
return $renderer->render($outputDestination);
667667
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheet\Chart\Renderer;
4+
5+
use PhpOffice\PhpSpreadsheet\Chart;
6+
7+
interface IRenderer
8+
{
9+
/**
10+
* IRenderer constructor.
11+
*
12+
* @param Chart $chart
13+
*/
14+
public function __construct(Chart $chart);
15+
16+
/**
17+
* Render the chart to given file (or stream).
18+
*
19+
* @param string $filename Name of the file render to
20+
*
21+
* @return bool true on success
22+
*/
23+
public function render($filename);
24+
}

0 commit comments

Comments
 (0)