Skip to content

For SVG exports, shift PlotCurveItems to be about 0,0#2728

Merged
j9ac9k merged 6 commits intopyqtgraph:masterfrom
j9ac9k:fix-2661-svg
Jun 6, 2023
Merged

For SVG exports, shift PlotCurveItems to be about 0,0#2728
j9ac9k merged 6 commits intopyqtgraph:masterfrom
j9ac9k:fix-2661-svg

Conversation

@j9ac9k
Copy link
Copy Markdown
Member

@j9ac9k j9ac9k commented May 19, 2023

Fixes #2661
Fixes #2018
Fixes #1849

This one is a doozy...

In the SVG exporter; there is a QPainter draw operation that occurs to a QDataBuffer that is interacting w/ a QSvgGenerator. The issue is that the positional information in the view (x and y coordinates for line to, move to, etc), are relatively high (> 1E8) and small differences between values (in the case of #2661, differences in increments of 10) are lost.

The way this PR attempts to get around this is by shifting PlotCurveItem's such that they are centered about (0, 0), and compute the transform for that shift, then apply the transform to the QPainter; so the curve appears to be in the correct place on the SVG output.

There are some more todo's with this PR

  • Don't only work with PlotCurveItems (should handle everything with setData, and dataBounds Using viewRect())
  • Don't import pg.PlotCurveItem in the exporter, use relative imports instead
  • Don't just handle translation, attempt to handle scaling

This PR also removed an unused lengthy PlotItem method that was left over after its usages were removed in 2012. For now I am commenting out the method as it looks to do that similar kind of translation and scaling we want, so I may need to borrow some code from it. PlotItem.writeSvgCurves should be removed before this PR is merged.

While testing, I attempted to get scatter plots to work in a similar fashion but could not get it working; so I decided to give up on that effort since I'm not sure if anyone cares about scatter plots that are placed in positions with larger absolute values.

@j9ac9k j9ac9k force-pushed the fix-2661-svg branch 2 times, most recently from 6b5e51e to 86a6243 Compare May 21, 2023 04:40
@j9ac9k j9ac9k marked this pull request as ready for review May 21, 2023 04:46
@j9ac9k
Copy link
Copy Markdown
Member Author

j9ac9k commented May 24, 2023

This PR is working again, but I'm still puzzled by two things.

  1. Why am I not having to call item.setData(xOriginalData, yOriginalData) after the export occurs? How is the curve still in the correct place in the view?
  2. Why do I have to call item.setData(xModified, yModified) to begin with instead of using a QTransform on the curve to shift and scale it over (0, 0)?

Ideally, I would be able to apply the QTransform that shifts the GraphicsItem to the center, if that method works we can use that same method for scatter plots and other graphics items without a bunch of if/elif statements.

@j9ac9k j9ac9k force-pushed the fix-2661-svg branch 2 times, most recently from b93c03b to 98ac81c Compare June 4, 2023 17:27
profiler = debug.Profiler()

if options is None:
options = {}

Check notice

Code scanning / CodeQL

Unused local variable

Variable options is not used.
Comment on lines +250 to +287
# if hasattr(item, 'setExportMode'):
# item.setExportMode(False)

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
@j9ac9k
Copy link
Copy Markdown
Member Author

j9ac9k commented Jun 4, 2023

Looks like this type of annotation needs python 3.9+ ...while I could use the 3.8 compatible variant, we should have dropped python 3.8 already per NEP-29.... will let this PR sit idle until we actually drop python 3.8.

@j9ac9k j9ac9k force-pushed the fix-2661-svg branch 4 times, most recently from 2a14aad to 6419c0d Compare June 5, 2023 04:49
@j9ac9k
Copy link
Copy Markdown
Member Author

j9ac9k commented Jun 5, 2023

...and somewhere along the way I broke the exporter, it's no longer showing the curve in the SVG 😆

Oh, exiting prematurely in the case of PlotDataItem was apparently not a good idea in hindsight (even tho it saved me from the console log/spam).

@j9ac9k
Copy link
Copy Markdown
Member Author

j9ac9k commented Jun 5, 2023

Since I'm now looking at the SVG exporter and I hope I don't have to look at it for a while in the future, decided to investigate #2018

c996598 addresses the background issue reported there, but the size (height/width) parameters are still not being accepted correctly.

@j9ac9k j9ac9k merged commit e60f33a into pyqtgraph:master Jun 6, 2023
@j9ac9k j9ac9k deleted the fix-2661-svg branch June 6, 2023 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVG exporter output has errors when data values are very large SVGExporter: Background color SVG export: linearGradient not exported correctly

1 participant