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

Figure now resizes to fit the window #14

Merged
merged 5 commits into from Sep 10, 2021
Merged

Figure now resizes to fit the window #14

merged 5 commits into from Sep 10, 2021

Conversation

wesley5040
Copy link

@wesley5040 wesley5040 commented Aug 23, 2021

  • Added on_resize to init so it can be used by the user
  • Updated example to automagically resize the plot to the window size (it's a little laggy, but way better than having it be a fixed size)

I was going to add something to make it deal with resizing the figure on it's own, but I didn't see a clean way to do it. So if you see one I can add it.

Let me know if you want anything changed.

f = Figure(figsize=(5, 4))
ax = f.add_subplot(1, 1, 1)
dpi = 100 # as of writing, 100 is also the default DPI for matplotlib.figure.Figure
f = Figure(figsize=(self.chart.layout.content_width / dpi, self.chart.layout.content_height / dpi), dpi=dpi)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly a fan of how this accesses something that doesn't really seem to be documented (or at least somewhere I thought to look), but it's how it's done in the Canvas example for Toga: https://github.com/beeware/toga/blob/e1650326e77d09a27f7e916072bb3379b334977f/examples/canvas/canvas/app.py#L282

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - it's not formally documented, but I'd interpret the layout attribute as fair game for read access.

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay in reviewing this. It looks great!

I've added to your code to take these improvements a step further. Old code should continue to work as it did before; but I've modified the API (and the example) to provide what I hope is a more natural API that is focussed on what the user is actually trying to do: draw a chart.

The major benefit:

  • The chart automatically resizes when the canvas resizes. This seems like a natural and obvious behaviour to me, so I can't see any reason it shouldn't be the default.
  • The end user doesn't need to interrogate the layout width/height attributes - they get a pre-built Figure that is the right size.
  • The end user doesn't need to import any of matplotlib for simple cases; they just manipulate a Figure that they've been given.

If the user doesn't want the resize behavior, they can define their own "no-op" resize, and fall back to manually drawing as before.

@freakboy3742 freakboy3742 merged commit 64ba814 into beeware:master Sep 10, 2021
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.

None yet

3 participants