From 782b4e4faee4c9592283d66262e41b1dc0864882 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 29 Oct 2017 01:58:42 +0900 Subject: [PATCH] Upgrade chart rendering support to be composer based This allow to get rid of manual class loading and have simpler usage of the library. --- composer.json | 3 +- composer.lock | 42 ++++- docs/topics/migration-from-PHPExcel.md | 34 ++++ samples/Chart/32_Chart_read_write_HTML.php | 13 +- samples/Chart/32_Chart_read_write_PDF.php | 13 +- samples/Chart/35_Chart_render.php | 14 +- src/PhpSpreadsheet/Chart.php | 34 ++-- .../Chart/Renderer/IRenderer.php | 24 +++ src/PhpSpreadsheet/Chart/Renderer/JpGraph.php | 158 +++++++++--------- .../Chart/Renderer/Polyfill.php | 9 + src/PhpSpreadsheet/Helper/Sample.php | 4 +- src/PhpSpreadsheet/Settings.php | 94 ++--------- src/PhpSpreadsheet/Writer/IWriter.php | 9 + tests/PhpSpreadsheetTests/SampleTest.php | 13 +- 14 files changed, 246 insertions(+), 218 deletions(-) create mode 100644 src/PhpSpreadsheet/Chart/Renderer/IRenderer.php create mode 100644 src/PhpSpreadsheet/Chart/Renderer/Polyfill.php diff --git a/composer.json b/composer.json index 3a5085fc..f30c4648 100644 --- a/composer.json +++ b/composer.json @@ -54,7 +54,8 @@ "phpunit/phpunit": "^5.7", "dompdf/dompdf": "^0.8.0", "mpdf/mpdf": "^7.0.0", - "friendsofphp/php-cs-fixer": "^2.7" + "friendsofphp/php-cs-fixer": "^2.7", + "jpgraph/jpgraph": "^4.0" }, "suggest": { "ext-gd": "Required for exact column width autocalculation", diff --git a/composer.lock b/composer.lock index ea208633..b7319bd8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "eb3d1cabb8bf94da9858c9fb0c24ed6a", + "content-hash": "804bbe054403d6532308f8a65f19adc2", "packages": [ { "name": "psr/simple-cache", @@ -527,6 +527,46 @@ ], "time": "2014-11-20T16:49:30+00:00" }, + { + "name": "jpgraph/jpgraph", + "version": "4.0.2", + "source": { + "type": "git", + "url": "https://github.com/ztec/JpGraph.git", + "reference": "e82db7da6a546d3926c24c9a346226da7aa49094" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ztec/JpGraph/zipball/e82db7da6a546d3926c24c9a346226da7aa49094", + "reference": "e82db7da6a546d3926c24c9a346226da7aa49094", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "lib/JpGraph.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "QPL 1.0" + ], + "authors": [ + { + "name": "JpGraph team" + } + ], + "description": "jpGraph, library to make graphs and charts", + "homepage": "http://jpgraph.net/", + "keywords": [ + "chart", + "data", + "graph", + "jpgraph", + "pie" + ], + "time": "2017-02-23T09:44:15+00:00" + }, { "name": "mpdf/mpdf", "version": "v7.0.0", diff --git a/docs/topics/migration-from-PHPExcel.md b/docs/topics/migration-from-PHPExcel.md index 495c3313..55063c03 100644 --- a/docs/topics/migration-from-PHPExcel.md +++ b/docs/topics/migration-from-PHPExcel.md @@ -199,6 +199,40 @@ $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf') $writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet); ``` +### Rendering charts + +When rendering charts for HTML or PDF outputs, the process was also simplified. And while +JpGraph support is still available, it is unfortunately not up to date for latest PHP versions +and it will generate various warnings. + +If you rely on this feature, please consider +contributing either patches to JpGraph or another `IRenderer` implementation (a good +candidate might be [CpChart](https://github.com/szymach/c-pchart)). + +Before: + +```php +$rendererName = \PHPExcel_Settings::CHART_RENDERER_JPGRAPH; +$rendererLibrary = 'jpgraph3.5.0b1/src/'; +$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary; + +\PHPExcel_Settings::setChartRenderer($rendererName, $rendererLibraryPath); +``` + +After: + +Require the dependency via composer: + +```sh +composer require jpgraph/jpgraph +``` + +And then: + +```php +Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class); +``` + ### PclZip and ZipArchive Support for PclZip were dropped in favor of the more complete and modern diff --git a/samples/Chart/32_Chart_read_write_HTML.php b/samples/Chart/32_Chart_read_write_HTML.php index fb2ead68..8071edd2 100644 --- a/samples/Chart/32_Chart_read_write_HTML.php +++ b/samples/Chart/32_Chart_read_write_HTML.php @@ -6,16 +6,7 @@ use PhpOffice\PhpSpreadsheet\Settings; require __DIR__ . '/../Header.php'; // Change these values to select the Rendering library that you wish to use -// and its directory location on your server -$rendererName = Settings::CHART_RENDERER_JPGRAPH; -$rendererLibrary = 'jpgraph3.5.0b1/src/'; -$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary; - -if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) { - $helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure'); - - return; -} +Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class); $inputFileType = 'Xlsx'; $inputFileNames = __DIR__ . '/../templates/36write*.xlsx'; @@ -86,7 +77,7 @@ foreach ($inputFileNames as $inputFileName) { } // Save - $filename = $helper->getFilename($inputFileName); + $filename = $helper->getFilename($inputFileName, 'html'); $writer = IOFactory::createWriter($spreadsheet, 'Html'); $writer->setIncludeCharts(true); $callStartTime = microtime(true); diff --git a/samples/Chart/32_Chart_read_write_PDF.php b/samples/Chart/32_Chart_read_write_PDF.php index 6400ab49..7cc51565 100644 --- a/samples/Chart/32_Chart_read_write_PDF.php +++ b/samples/Chart/32_Chart_read_write_PDF.php @@ -8,16 +8,7 @@ require __DIR__ . '/../Header.php'; IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class); // Change these values to select the Rendering library that you wish to use -// for Chart images, and its directory location on your server -$rendererName = Settings::CHART_RENDERER_JPGRAPH; -$rendererLibrary = 'jpgraph3.5.0b1/src/'; -$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary; - -if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) { - $helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure'); - - return; -} +Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class); $inputFileType = 'Xlsx'; $inputFileNames = __DIR__ . '/../templates/36write*.xlsx'; @@ -88,7 +79,7 @@ foreach ($inputFileNames as $inputFileName) { } // Save - $filename = $helper->getFilename($inputFileName); + $filename = $helper->getFilename($inputFileName, 'pdf'); $writer = IOFactory::createWriter($spreadsheet, 'Pdf'); $writer->setIncludeCharts(true); $callStartTime = microtime(true); diff --git a/samples/Chart/35_Chart_render.php b/samples/Chart/35_Chart_render.php index 4671f21b..9638c679 100644 --- a/samples/Chart/35_Chart_render.php +++ b/samples/Chart/35_Chart_render.php @@ -6,16 +6,7 @@ use PhpOffice\PhpSpreadsheet\Settings; require __DIR__ . '/../Header.php'; // Change these values to select the Rendering library that you wish to use -// and its directory location on your server -$rendererName = Settings::CHART_RENDERER_JPGRAPH; -$rendererLibrary = 'jpgraph3.5.0b1/src/'; -$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary; - -if (!Settings::setChartRenderer($rendererName, $rendererLibraryPath)) { - $helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure'); - - return; -} +Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class); $inputFileType = 'Xlsx'; $inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx'; @@ -62,13 +53,14 @@ foreach ($inputFileNames as $inputFileName) { } $helper->log(' ' . $chartName . ' - ' . $caption); - $jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'jpg'); + $jpegFile = $helper->getFilename('35-' . $inputFileNameShort, 'png'); if (file_exists($jpegFile)) { unlink($jpegFile); } try { $chart->render($jpegFile); + $helper->log('Rendered image: ' . $jpegFile); } catch (Exception $e) { $helper->log('Error rendering chart: ' . $e->getMessage()); } diff --git a/src/PhpSpreadsheet/Chart.php b/src/PhpSpreadsheet/Chart.php index 01323642..9ea037ee 100644 --- a/src/PhpSpreadsheet/Chart.php +++ b/src/PhpSpreadsheet/Chart.php @@ -640,29 +640,29 @@ class Chart } } + /** + * Render the chart to given file (or stream). + * + * @param string $outputDestination Name of the file render to + * + * @return bool true on success + */ public function render($outputDestination = null) { - $libraryName = Settings::getChartRendererName(); - if ($libraryName === null) { - return false; - } - // Ensure that data series values are up-to-date before we render - $this->refresh(); - - $libraryPath = Settings::getChartRendererPath(); - $includePath = str_replace('\\', '/', get_include_path()); - $rendererPath = str_replace('\\', '/', $libraryPath); - if (strpos($rendererPath, $includePath) === false) { - set_include_path(get_include_path() . PATH_SEPARATOR . $libraryPath); - } - - $rendererName = '\\PhpOffice\\PhpSpreadsheet\\Chart\\Renderer\\' . $libraryName; - $renderer = new $rendererName($this); - if ($outputDestination == 'php://output') { $outputDestination = null; } + $libraryName = Settings::getChartRenderer(); + if ($libraryName === null) { + return false; + } + + // Ensure that data series values are up-to-date before we render + $this->refresh(); + + $renderer = new $libraryName($this); + return $renderer->render($outputDestination); } } diff --git a/src/PhpSpreadsheet/Chart/Renderer/IRenderer.php b/src/PhpSpreadsheet/Chart/Renderer/IRenderer.php new file mode 100644 index 00000000..b1336fa5 --- /dev/null +++ b/src/PhpSpreadsheet/Chart/Renderer/IRenderer.php @@ -0,0 +1,24 @@ + MARK_DIAMOND, - 'square' => MARK_SQUARE, - 'triangle' => MARK_UTRIANGLE, - 'x' => MARK_X, - 'star' => MARK_STAR, - 'dot' => MARK_FILLEDCIRCLE, - 'dash' => MARK_DTRIANGLE, - 'circle' => MARK_CIRCLE, - 'plus' => MARK_CROSS, - ]; + private static $markSet; private $chart; @@ -40,6 +31,51 @@ class JpGraph private static $plotMark = 0; + /** + * Create a new jpgraph. + * + * @param Chart $chart + */ + public function __construct(Chart $chart) + { + self::init(); + $this->graph = null; + $this->chart = $chart; + } + + private static function init() + { + static $loaded = false; + if ($loaded) { + return; + } + + \JpGraph\JpGraph::load(); + \JpGraph\JpGraph::module('bar'); + \JpGraph\JpGraph::module('contour'); + \JpGraph\JpGraph::module('line'); + \JpGraph\JpGraph::module('pie'); + \JpGraph\JpGraph::module('pie3d'); + \JpGraph\JpGraph::module('radar'); + \JpGraph\JpGraph::module('regstat'); + \JpGraph\JpGraph::module('scatter'); + \JpGraph\JpGraph::module('stock'); + + self::$markSet = [ + 'diamond' => MARK_DIAMOND, + 'square' => MARK_SQUARE, + 'triangle' => MARK_UTRIANGLE, + 'x' => MARK_X, + 'star' => MARK_STAR, + 'dot' => MARK_FILLEDCIRCLE, + 'dash' => MARK_DTRIANGLE, + 'circle' => MARK_CIRCLE, + 'plus' => MARK_CROSS, + ]; + + $loaded = true; + } + private function formatPointMarker($seriesPlot, $markerID) { $plotMarkKeys = array_keys(self::$markSet); @@ -186,7 +222,7 @@ class JpGraph private function renderCartesianPlotArea($type = 'textlin') { - $this->graph = new Graph(self::$width, self::$height); + $this->graph = new \Graph(self::$width, self::$height); $this->graph->SetScale($type); $this->renderTitle(); @@ -223,14 +259,14 @@ class JpGraph private function renderPiePlotArea($doughnut = false) { - $this->graph = new PieGraph(self::$width, self::$height); + $this->graph = new \PieGraph(self::$width, self::$height); $this->renderTitle(); } private function renderRadarPlotArea() { - $this->graph = new RadarGraph(self::$width, self::$height); + $this->graph = new \RadarGraph(self::$width, self::$height); $this->graph->SetScale('lin'); $this->renderTitle(); @@ -272,7 +308,7 @@ class JpGraph ++$testCurrentIndex; } - $seriesPlot = new LinePlot($dataValues); + $seriesPlot = new \LinePlot($dataValues); if ($combination) { $seriesPlot->SetBarCenter(); } @@ -294,7 +330,7 @@ class JpGraph if ($grouping == 'standard') { $groupPlot = $seriesPlots; } else { - $groupPlot = new AccLinePlot($seriesPlots); + $groupPlot = new \AccLinePlot($seriesPlots); } $this->graph->Add($groupPlot); } @@ -349,7 +385,7 @@ class JpGraph if ($rotation == 'bar') { $dataValues = array_reverse($dataValues); } - $seriesPlot = new BarPlot($dataValues); + $seriesPlot = new \BarPlot($dataValues); $seriesPlot->SetColor('black'); $seriesPlot->SetFillColor(self::$colourSet[self::$plotColour++]); if ($dimensions == '3d') { @@ -370,11 +406,11 @@ class JpGraph } if ($grouping == 'clustered') { - $groupPlot = new GroupBarPlot($seriesPlots); + $groupPlot = new \GroupBarPlot($seriesPlots); } elseif ($grouping == 'standard') { - $groupPlot = new GroupBarPlot($seriesPlots); + $groupPlot = new \GroupBarPlot($seriesPlots); } else { - $groupPlot = new AccBarPlot($seriesPlots); + $groupPlot = new \AccBarPlot($seriesPlots); if ($dimensions == '3d') { $groupPlot->SetShadow(); } @@ -400,14 +436,14 @@ class JpGraph $dataValuesY[$k] = $k; } - $seriesPlot = new ScatterPlot($dataValuesX, $dataValuesY); + $seriesPlot = new \ScatterPlot($dataValuesX, $dataValuesY); if ($scatterStyle == 'lineMarker') { $seriesPlot->SetLinkPoints(); $seriesPlot->link->SetColor(self::$colourSet[self::$plotColour]); } elseif ($scatterStyle == 'smoothMarker') { - $spline = new Spline($dataValuesY, $dataValuesX); + $spline = new \Spline($dataValuesY, $dataValuesX); list($splineDataY, $splineDataX) = $spline->Get(count($dataValuesX) * self::$width / 20); - $lplot = new LinePlot($splineDataX, $splineDataY); + $lplot = new \LinePlot($splineDataX, $splineDataY); $lplot->SetColor(self::$colourSet[self::$plotColour]); $this->graph->Add($lplot); @@ -452,7 +488,7 @@ class JpGraph $this->graph->SetTitles(array_reverse($dataValues)); - $seriesPlot = new RadarPlot(array_reverse($dataValuesX)); + $seriesPlot = new \RadarPlot(array_reverse($dataValuesX)); $dataLabel = $this->chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotLabelByIndex($i)->getDataValue(); $seriesPlot->SetColor(self::$colourSet[self::$plotColour++]); @@ -481,7 +517,7 @@ class JpGraph $dataValues[$i] = $dataValuesX; } - $seriesPlot = new ContourPlot($dataValues); + $seriesPlot = new \ContourPlot($dataValues); $this->graph->Add($seriesPlot); } @@ -519,7 +555,7 @@ class JpGraph $this->graph->xaxis->SetTickLabels($datasetLabels); } - $seriesPlot = new StockPlot($dataValuesPlot); + $seriesPlot = new \StockPlot($dataValuesPlot); $seriesPlot->SetWidth(20); $this->graph->Add($seriesPlot); @@ -527,8 +563,6 @@ class JpGraph private function renderAreaChart($groupCount, $dimensions = '2d') { - require_once Settings::getChartRendererPath() . 'jpgraph_line.php'; - $this->renderCartesianPlotArea(); for ($i = 0; $i < $groupCount; ++$i) { @@ -538,8 +572,6 @@ class JpGraph private function renderLineChart($groupCount, $dimensions = '2d') { - require_once Settings::getChartRendererPath() . 'jpgraph_line.php'; - $this->renderCartesianPlotArea(); for ($i = 0; $i < $groupCount; ++$i) { @@ -549,8 +581,6 @@ class JpGraph private function renderBarChart($groupCount, $dimensions = '2d') { - require_once Settings::getChartRendererPath() . 'jpgraph_bar.php'; - $this->renderCartesianPlotArea(); for ($i = 0; $i < $groupCount; ++$i) { @@ -560,10 +590,6 @@ class JpGraph private function renderScatterChart($groupCount) { - require_once Settings::getChartRendererPath() . 'jpgraph_scatter.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_regstat.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_line.php'; - $this->renderCartesianPlotArea('linlin'); for ($i = 0; $i < $groupCount; ++$i) { @@ -573,8 +599,6 @@ class JpGraph private function renderBubbleChart($groupCount) { - require_once Settings::getChartRendererPath() . 'jpgraph_scatter.php'; - $this->renderCartesianPlotArea('linlin'); for ($i = 0; $i < $groupCount; ++$i) { @@ -584,11 +608,6 @@ class JpGraph private function renderPieChart($groupCount, $dimensions = '2d', $doughnut = false, $multiplePlots = false) { - require_once Settings::getChartRendererPath() . 'jpgraph_pie.php'; - if ($dimensions == '3d') { - require_once Settings::getChartRendererPath() . 'jpgraph_pie3d.php'; - } - $this->renderPiePlotArea($doughnut); $iLimit = ($multiplePlots) ? $groupCount : 1; @@ -622,12 +641,12 @@ class JpGraph } if ($dimensions == '3d') { - $seriesPlot = new PiePlot3D($dataValues); + $seriesPlot = new \PiePlot3D($dataValues); } else { if ($doughnut) { - $seriesPlot = new PiePlotC($dataValues); + $seriesPlot = new \PiePlotC($dataValues); } else { - $seriesPlot = new PiePlot($dataValues); + $seriesPlot = new \PiePlot($dataValues); } } @@ -660,8 +679,6 @@ class JpGraph private function renderRadarChart($groupCount) { - require_once Settings::getChartRendererPath() . 'jpgraph_radar.php'; - $this->renderRadarPlotArea(); for ($groupID = 0; $groupID < $groupCount; ++$groupID) { @@ -671,8 +688,6 @@ class JpGraph private function renderStockChart($groupCount) { - require_once Settings::getChartRendererPath() . 'jpgraph_stock.php'; - $this->renderCartesianPlotArea('intint'); for ($groupID = 0; $groupID < $groupCount; ++$groupID) { @@ -682,8 +697,6 @@ class JpGraph private function renderContourChart($groupCount, $dimensions) { - require_once Settings::getChartRendererPath() . 'jpgraph_contour.php'; - $this->renderCartesianPlotArea('intint'); for ($i = 0; $i < $groupCount; ++$i) { @@ -693,12 +706,6 @@ class JpGraph private function renderCombinationChart($groupCount, $dimensions, $outputDestination) { - require_once Settings::getChartRendererPath() . 'jpgraph_line.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_bar.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_scatter.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_regstat.php'; - require_once Settings::getChartRendererPath() . 'jpgraph_line.php'; - $this->renderCartesianPlotArea(); for ($i = 0; $i < $groupCount; ++$i) { @@ -707,21 +714,21 @@ class JpGraph switch ($chartType) { case 'area3DChart': $dimensions = '3d'; - // no break + // no break case 'areaChart': $this->renderPlotLine($i, true, true, $dimensions); break; case 'bar3DChart': $dimensions = '3d'; - // no break + // no break case 'barChart': $this->renderPlotBar($i, $dimensions); break; case 'line3DChart': $dimensions = '3d'; - // no break + // no break case 'lineChart': $this->renderPlotLine($i, false, true, $dimensions); @@ -777,35 +784,35 @@ class JpGraph switch ($chartType) { case 'area3DChart': $dimensions = '3d'; - // no break + // no break case 'areaChart': $this->renderAreaChart($groupCount, $dimensions); break; case 'bar3DChart': $dimensions = '3d'; - // no break + // no break case 'barChart': $this->renderBarChart($groupCount, $dimensions); break; case 'line3DChart': $dimensions = '3d'; - // no break + // no break case 'lineChart': $this->renderLineChart($groupCount, $dimensions); break; case 'pie3DChart': $dimensions = '3d'; - // no break + // no break case 'pieChart': $this->renderPieChart($groupCount, $dimensions, false, false); break; case 'doughnut3DChart': $dimensions = '3d'; - // no break + // no break case 'doughnutChart': $this->renderPieChart($groupCount, $dimensions, true, true); @@ -824,7 +831,7 @@ class JpGraph break; case 'surface3DChart': $dimensions = '3d'; - // no break + // no break case 'surfaceChart': $this->renderContourChart($groupCount, $dimensions); @@ -844,17 +851,4 @@ class JpGraph return true; } - - /** - * Create a new jpgraph. - * - * @param Chart $chart - */ - public function __construct(Chart $chart) - { - $this->graph = null; - $this->chart = $chart; - - require_once Settings::getChartRendererPath() . '/jpgraph.php'; - } } diff --git a/src/PhpSpreadsheet/Chart/Renderer/Polyfill.php b/src/PhpSpreadsheet/Chart/Renderer/Polyfill.php new file mode 100644 index 00000000..7fa38394 --- /dev/null +++ b/src/PhpSpreadsheet/Chart/Renderer/Polyfill.php @@ -0,0 +1,9 @@ +getTemporaryFolder() . '/' . str_replace('.php', '.' . $extension, basename($filename)); + $originalExtension = pathinfo($filename, PATHINFO_EXTENSION); + + return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename)); } /** diff --git a/src/PhpSpreadsheet/Settings.php b/src/PhpSpreadsheet/Settings.php index 66cbfcca..faea0501 100644 --- a/src/PhpSpreadsheet/Settings.php +++ b/src/PhpSpreadsheet/Settings.php @@ -2,33 +2,19 @@ namespace PhpOffice\PhpSpreadsheet; +use PhpOffice\PhpSpreadsheet\Chart\Renderer\IRenderer; use PhpOffice\PhpSpreadsheet\Collection\Memory; use Psr\SimpleCache\CacheInterface; class Settings { - /** Optional Chart Rendering libraries */ - const CHART_RENDERER_JPGRAPH = 'JpGraph'; - - private static $chartRenderers = [ - self::CHART_RENDERER_JPGRAPH, - ]; - /** - * Name of the external Library used for rendering charts - * e.g. - * jpgraph. + * Class name of the chart renderer used for rendering charts + * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph. * * @var string */ - private static $chartRendererName; - - /** - * Directory Path to the external Library used for rendering charts. - * - * @var string - */ - private static $chartRendererPath; + private static $chartRenderer; /** * Default options for libxml loader. @@ -56,80 +42,32 @@ class Settings return Calculation::getInstance()->setLocale($locale); } - /** - * Set details of the external library that PhpSpreadsheet should use for rendering charts. - * - * @param string $libraryName Internal reference name of the library - * e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH - * @param string $libraryBaseDir Directory path to the library's base folder - * - * @return bool Success or failure - */ - public static function setChartRenderer($libraryName, $libraryBaseDir) - { - if (!self::setChartRendererName($libraryName)) { - return false; - } - - return self::setChartRendererPath($libraryBaseDir); - } - /** * Identify to PhpSpreadsheet the external library to use for rendering charts. * - * @param string $libraryName Internal reference name of the library - * e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH + * @param string $rendererClass Class name of the chart renderer + * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph * - * @return bool Success or failure + * @throws Exception */ - public static function setChartRendererName($libraryName) + public static function setChartRenderer($rendererClass) { - if (!in_array($libraryName, self::$chartRenderers)) { - return false; + if (!is_a($rendererClass, IRenderer::class, true)) { + throw new Exception('Chart renderer must implement ' . IRenderer::class); } - self::$chartRendererName = $libraryName; - return true; + self::$chartRenderer = $rendererClass; } /** - * Tell PhpSpreadsheet where to find the external library to use for rendering charts. + * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use. * - * @param string $libraryBaseDir Directory path to the library's base folder - * - * @return bool Success or failure + * @return null|string Class name of the chart renderer + * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph */ - public static function setChartRendererPath($libraryBaseDir) + public static function getChartRenderer() { - if ((file_exists($libraryBaseDir) === false) || (is_readable($libraryBaseDir) === false)) { - return false; - } - self::$chartRendererPath = $libraryBaseDir; - - return true; - } - - /** - * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use (e.g. jpgraph). - * - * @return null|string Internal reference name of the Chart Rendering Library that PhpSpreadsheet is - * currently configured to use - * e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH - */ - public static function getChartRendererName() - { - return self::$chartRendererName; - } - - /** - * Return the directory path to the Chart Rendering Library that PhpSpreadsheet is currently configured to use. - * - * @return null|string Directory Path to the Chart Rendering Library that PhpSpreadsheet is - * currently configured to use - */ - public static function getChartRendererPath() - { - return self::$chartRendererPath; + return self::$chartRenderer; } /** diff --git a/src/PhpSpreadsheet/Writer/IWriter.php b/src/PhpSpreadsheet/Writer/IWriter.php index 7f3bf00d..9ce45a19 100644 --- a/src/PhpSpreadsheet/Writer/IWriter.php +++ b/src/PhpSpreadsheet/Writer/IWriter.php @@ -2,8 +2,17 @@ namespace PhpOffice\PhpSpreadsheet\Writer; +use PhpOffice\PhpSpreadsheet\Spreadsheet; + interface IWriter { + /** + * IWriter constructor. + * + * @param Spreadsheet $spreadsheet + */ + public function __construct(Spreadsheet $spreadsheet); + /** * Save PhpSpreadsheet to file. * diff --git a/tests/PhpSpreadsheetTests/SampleTest.php b/tests/PhpSpreadsheetTests/SampleTest.php index c12ef16d..19e6fea9 100644 --- a/tests/PhpSpreadsheetTests/SampleTest.php +++ b/tests/PhpSpreadsheetTests/SampleTest.php @@ -25,15 +25,18 @@ class SampleTest extends PHPUnit_Framework_TestCase public function providerSample() { - $skipped = []; + $skipped = [ + 'Chart/32_Chart_read_write_PDF.php', // Unfortunately JpGraph is not up to date for latest PHP and raise many warnings + 'Chart/32_Chart_read_write_HTML.php', // idem + ]; // Unfortunately some tests are too long be ran with code-coverage // analysis on Travis, so we need to exclude them global $argv; if (in_array('--coverage-clover', $argv)) { $tooLongToBeCovered = [ - '06 Largescale', - '13 CalculationCyclicFormulae', + 'Basic/06_Largescale.php', + 'Basic/13_CalculationCyclicFormulae.php', ]; $skipped = array_merge($skipped, $tooLongToBeCovered); } @@ -41,8 +44,8 @@ class SampleTest extends PHPUnit_Framework_TestCase $helper = new Sample(); $result = []; foreach ($helper->getSamples() as $samples) { - foreach ($samples as $name => $sample) { - if (!in_array($name, $skipped)) { + foreach ($samples as $sample) { + if (!in_array($sample, $skipped)) { $file = '../samples/' . $sample; $result[] = [$file]; }