Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendered charts from .xlsx file dont look like original charts. There is even a chart missing. #3783

Open
6 tasks
flyke opened this issue Nov 7, 2023 · 1 comment
Labels

Comments

@flyke
Copy link

flyke commented Nov 7, 2023

This is:

- [* ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

When I have a spreadsheet with charts, and I write it to html or PDF, I expect the the charts in the html or PDF to be like the charts in the source spreadsheet

What is the current behavior?

In the rendered html/pdf:

What are the steps to reproduce?

  • Use the attached xlsx file
    demo-excel.xlsx

  • Use this code to create PDF from the xlsx file.

<?php
use PhpOffice\PhpSpreadsheet\IOFactory as SpreadsheetIOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf as ExcelMPDF;

      // Set a class which will render the charts.
      \PhpOffice\PhpSpreadsheet\Settings::setChartRenderer(
        \PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::class
      );
      // Load the document.
      $reader = SpreadsheetIOFactory::createReader('Xlsx');
      $reader->setIncludeCharts(true);
      $document = $reader->load('demo-excel.xlsx');
      // Set up the writer
      $writer = new ExcelMPDF($document);
      $writer->setIncludeCharts(true);
      //$writer->writeAllSheets();
      // Create PDF file.
      $writer->save('demo-excel.pdf');

Screenshots attached.
result-pdf-file
source-xlsx-file

What features do you think are causing the issue

  • Reader
  • [* ] Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • [* ] Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

This issue is now only for XLSX files. I have a very comparable XLS file, but that does not render any chart, so thats a different issue altogether.

Which versions of PhpSpreadsheet and PHP are affected?

  • PhpSpreadsheet 1.29.0
  • I'm using PHP 8.1.23
@oleibman
Copy link
Collaborator

oleibman commented Nov 7, 2023

Thank you for the sample file and the screenshots.
The missing chart is a bug in PhpSpreadsheet which I will investigate.
The renderer uses its own palette. We do not pass any color information to it, and, even if we did, it could not use it.
I believe the rotation falls into the same category. I will ask the vendor.
My html output has a different problem than yours, which is a bit strange. My charts are cut off because the renderer creates the charts a little bigger than the spreadsheet, so they overlay each other in the html. I am not sure how I can tell what size the charts are in the spreadsheet, and, even if I knew that, I am not sure if I can pass that information to the renderer. I will ask the vendor.

f1mishutka added a commit to f1mishutka/PhpSpreadsheet that referenced this issue Nov 8, 2023
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 10, 2023
Several problems are noted in PHPOffice#3783. This PR addresses those problems which make the rendering unsatisfactory (see following paragraphs). It does not address some items where the rendering is IMO satisfactory although it doesn't match Excel. In particular, the use of a different color palette and the rotation of charts are not addressed. I will leave the issue open for now because of those.

As for the items which are addressed, in some cases the Html was omitting a chart altogether. This is because it had been extending the column range for charts only when it decided that extending the row range was needed. The code is changed to now extend the column range whenever the chart begins beyond the current column range of the sheet.

Also, the rendering always produced a fixed-size image, so saving as Html could result in charts overlaying each other or other parts of the spreadsheet. New properties `renderedWidth` and `renderedHeight` are added to Chart, along with setters and getters. Writer/Html is changed to set these values using the chart's top left and bottom right cells to try to determine the actual size that is needed. Users can also set these properties outside of Writer/Html if they wish. Thanks to @f1mishutka for determining the source of this problem and suggesting an approach to resolving it.

Because the size of the rendered image in Html/Pdf is changed, this could be considered a breaking change. To restore the prior behavior, do the following for all charts before saving as Html:
```php
$chart->setRenderedWidth(640.0);
$chart->setRenderedHeight(480.0);
```
oleibman added a commit that referenced this issue Nov 13, 2023
* Two Problems with Html Chart Rendering - Minor Break

Several problems are noted in #3783. This PR addresses those problems which make the rendering unsatisfactory (see following paragraphs). It does not address some items where the rendering is IMO satisfactory although it doesn't match Excel. In particular, the use of a different color palette and the rotation of charts are not addressed. I will leave the issue open for now because of those.

As for the items which are addressed, in some cases the Html was omitting a chart altogether. This is because it had been extending the column range for charts only when it decided that extending the row range was needed. The code is changed to now extend the column range whenever the chart begins beyond the current column range of the sheet.

Also, the rendering always produced a fixed-size image, so saving as Html could result in charts overlaying each other or other parts of the spreadsheet. New properties `renderedWidth` and `renderedHeight` are added to Chart, along with setters and getters. Writer/Html is changed to set these values using the chart's top left and bottom right cells to try to determine the actual size that is needed. Users can also set these properties outside of Writer/Html if they wish. Thanks to @f1mishutka for determining the source of this problem and suggesting an approach to resolving it.

Because the size of the rendered image in Html/Pdf is changed, this could be considered a breaking change. To restore the prior behavior, do the following for all charts before saving as Html:
```php
$chart->setRenderedWidth(640.0);
$chart->setRenderedHeight(480.0);
```

* Update CHANGELOG.md
kpn13 pushed a commit to kpn13/PhpSpreadsheet that referenced this issue Nov 13, 2023
* Two Problems with Html Chart Rendering - Minor Break

Several problems are noted in PHPOffice#3783. This PR addresses those problems which make the rendering unsatisfactory (see following paragraphs). It does not address some items where the rendering is IMO satisfactory although it doesn't match Excel. In particular, the use of a different color palette and the rotation of charts are not addressed. I will leave the issue open for now because of those.

As for the items which are addressed, in some cases the Html was omitting a chart altogether. This is because it had been extending the column range for charts only when it decided that extending the row range was needed. The code is changed to now extend the column range whenever the chart begins beyond the current column range of the sheet.

Also, the rendering always produced a fixed-size image, so saving as Html could result in charts overlaying each other or other parts of the spreadsheet. New properties `renderedWidth` and `renderedHeight` are added to Chart, along with setters and getters. Writer/Html is changed to set these values using the chart's top left and bottom right cells to try to determine the actual size that is needed. Users can also set these properties outside of Writer/Html if they wish. Thanks to @f1mishutka for determining the source of this problem and suggesting an approach to resolving it.

Because the size of the rendered image in Html/Pdf is changed, this could be considered a breaking change. To restore the prior behavior, do the following for all charts before saving as Html:
```php
$chart->setRenderedWidth(640.0);
$chart->setRenderedHeight(480.0);
```

* Update CHANGELOG.md
kpn13 pushed a commit to kpn13/PhpSpreadsheet that referenced this issue Nov 14, 2023
* Two Problems with Html Chart Rendering - Minor Break

Several problems are noted in PHPOffice#3783. This PR addresses those problems which make the rendering unsatisfactory (see following paragraphs). It does not address some items where the rendering is IMO satisfactory although it doesn't match Excel. In particular, the use of a different color palette and the rotation of charts are not addressed. I will leave the issue open for now because of those.

As for the items which are addressed, in some cases the Html was omitting a chart altogether. This is because it had been extending the column range for charts only when it decided that extending the row range was needed. The code is changed to now extend the column range whenever the chart begins beyond the current column range of the sheet.

Also, the rendering always produced a fixed-size image, so saving as Html could result in charts overlaying each other or other parts of the spreadsheet. New properties `renderedWidth` and `renderedHeight` are added to Chart, along with setters and getters. Writer/Html is changed to set these values using the chart's top left and bottom right cells to try to determine the actual size that is needed. Users can also set these properties outside of Writer/Html if they wish. Thanks to @f1mishutka for determining the source of this problem and suggesting an approach to resolving it.

Because the size of the rendered image in Html/Pdf is changed, this could be considered a breaking change. To restore the prior behavior, do the following for all charts before saving as Html:
```php
$chart->setRenderedWidth(640.0);
$chart->setRenderedHeight(480.0);
```

* Update CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants