mitoteam / jpgraph

JpGraph library composer package with PHP 8.4 support
https://packagist.org/packages/mitoteam/jpgraph
Other
36 stars 5 forks source link

using with phpspreadsheet #7

Closed rolinger closed 2 years ago

rolinger commented 2 years ago

phpoffice / phpspreadsheet documentation shows using jpgraph with it, but the old composer version is no longer supported and the built in jpgraph is quite old (no longer supported either).

So I installed this mitoteam/jpgraph package via composer thinking this would do the trick. In my php code I added the phpspreadsheet line:

use mitoteam\jpgraph\MtJpGraph
Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraph::class);

But I am getting a Fatal Error on the Settings::setChartRenderer line - does anyone have any idea on how to get the two to work together? I am assuming I am not calling it correctly in the Settings command.

Please don't flame me on this. I know this isn't a mitoteam issue, but I am desperate to get this working as I have a deliverable to a client and I have been spinning my wheels on this for two days.

f1mishutka commented 2 years ago

Hi,

It seems the we need custom IRenderer implementation for this to work. There two possible ways:

First one is obviously faster. But second one is more architecturally correct.

I'll do some experiments to understand a better way.

Could you please provide a simple code example of creating spreadsheet with a chart for us to be able to check this? Thank you!

f1mishutka commented 2 years ago

I've created for of PHPSpreadsheet to play with it a bit. I've implemented chart renderer to use MtJpGraph instead of original JpGraph renderer. Now I need your help to test it.

Could you please do flollowing:

If this helps I will create PR to main PhpSpreadsheet repository for them to be able to include this functionality in next release.

rolinger commented 2 years ago

Hey, wow, that was fast. I will try to get to this later today or tomorrow. I am quite certain the PhpSpreadsheet team would appreciate this effort. As of now, there seems to be no other wrapper available in PhpSpreadsheet except the one that comes installed with it. And as in my case, my services run on Linux cPanel with easyApache4 managing multiple installs of PHP - which makes the manual install JpGraph a real hassle, but having a composer version (MtJpGraph) with a new wrapper added to PhpSpreadsheet I think will make a lot of peoples lives easier.

rolinger commented 2 years ago

composer require mitoteam/phpspreadsheet not found. Which is odd, because I see it there.

Specifically:

[InvalidArgumentException]
Could not find a matching version of package mitoteam/phpspreadsheet. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (stable).

Anyway, gotta run out with the family today. Thought I could give it a quick go without diving to deep into it, will do so later.

f1mishutka commented 2 years ago

mitoteam/phpspreadsheet is not composer package, you can not add it by composer command.

You need to install usual PHPSpreadsheet library and then manuall download https://github.com/mitoteam/PhpSpreadsheet sources and replace it in vendor folder.

Anyway, gotta run out with the family today.

Sure! Nothing urgent at all.

rolinger commented 2 years ago

ah, ok. Sorry about that. I just tried real quick not giving it much thought. Will get to that now. Thanks

rolinger commented 2 years ago

Morning. Got everything copied over and got this access level error:

PHP Fatal error:  Access level to PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::init() must be protected (as in class PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraphRendererBase) or we
aker in /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Chart/Renderer/MtJpGraphRenderer.php on line 15

I changed MtJpGraphRenderer.php : line 15 to public - not certain if thats the correct action, it got past that error but then that generated the next error:

 PHP Fatal error:  Uncaught Error: Cannot call abstract method PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraphRendererBase::init() in /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSprea
dsheet/Chart/Renderer/JpGraphRendererBase.php:56

Stack trace:
#0 /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Chart/Chart.php(677): PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraphRendererBase->__construct(Object(PhpOffice\PhpSpreadsheet\Chart\Chart))
#1 /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Html.php(745): PhpOffice\PhpSpreadsheet\Chart\Chart->render('/tmp/62e7d77a51...')
#2 /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Html.php(1385): PhpOffice\PhpSpreadsheet\Writer\Html->writeChartInCell(Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), 'A8')
#3 /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Html.php(1401): PhpOffice\PhpSpreadsheet\Writer\Html->generateRowIncludeCharts(Object(PhpOffice\PhpSpreadsheet\W in /home/myDir/php/vendor/phpoffice/php
spreadsheet/src/PhpSpreadsheet/Chart/Renderer/JpGraphRendererBase.php on line 56
f1mishutka commented 2 years ago

Yes, my fault.

I've changed methods visibility with latest commit. Could you please try it again?

rolinger commented 2 years ago

Getting closer:

PHP Fatal error: Uncaught Error: Class 'PhpOffice\PhpSpreadsheet\Chart\Renderer\mitoteam\jpgraph\MtJpGraph' not found in /home/myDir/php/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Char t/Renderer/MtJpGraphRenderer.php:24

Also, probably should just call the main file: MtJpGraph.php vs MtJpGraphRenderer.php

rolinger commented 2 years ago

The JpGraph.php INIT function is different than the `MtJpGraphRendererr.php INIT function. I suspect they would need to be the same....no?

JpGraph:

    protected static function init(): void
    {
        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');

        $loaded = true;
    }

vs MtJpGraphRenderer:

    protected static function init(): void
    {
        static $loaded = false;
        if ($loaded) {
            return;
        }

        mitoteam\jpgraph\MtJpGraph::load([
            'bar',
            'contour',
            'line',
            'pie',
            'pie3d',
            'radar',
            'regstat',
            'scatter',
            'stock',
          ]);

        $loaded = true;
    }
rolinger commented 2 years ago

Ok, think I got it work. Line 24 on MtJpGraphRendererr.php needs to be changed from:

mitoteam\jpgraph\MtJpGraph::load([

to

\mitoteam\jpgraph\MtJpGraph::load([

I got a bunch of these errors related to a specific pie chart in the excel template I am using, but I am assuming this is related to my data set:

PHP Warning: A non-numeric value encountered in /home/myDir/php/vendor/mitoteam/jpgraph/src/lib/jpgraph_pie3d.php on line 656

Verifying all my output files now.

rolinger commented 2 years ago

Hey! Progress. I have charts printing to my PDF files now so it looks like your fork is now working with the one following change:

MtJpGraphRendererr.php Line 24: FROM: mitoteam\jpgraph\MtJpGraph::load([ TO: \mitoteam\jpgraph\MtJpGraph::load([

I need to validate my piechart data and clean up excel template files but I am finally on the right track now.

Thanks for your help and effort to make MtJpGraph composer compatible with PhpSpreadsheet - its going to make a big difference for many users.

f1mishutka commented 2 years ago

Great news!

Created PR for PhpSpreadsheet https://github.com/PHPOffice/PhpSpreadsheet/pull/2979 Hope it will be accepted soon.

Thank you for your efforts for testing this!

f1mishutka commented 2 years ago

Created PR for PhpSpreadsheet PHPOffice/PhpSpreadsheet#2979

PR was accepted and merged to master. So it will be released with next PhpSpreadsheet version.