beeware / toga-chart

A matplotlib charting widget for Toga.
BSD 3-Clause "New" or "Revised" License
25 stars 11 forks source link

Interactive matplotlib charts #72

Open kaylai opened 8 months ago

kaylai commented 8 months ago

What is the problem or limitation you are having?

It would be great to have intractability within a toga_chart.Chart() object. I understand Chart() is calling matplotlib. Does that mean any matplotlib styling/functionality can be used within a Chart() object? Currently, a Chart() is plotted as a static thing. I'd like to enable the standard matplotlib controls (pan, zoom, save) and also add a DraggableLine element. Is intractability currently possible, or will this feature be added?

Describe the solution you'd like

If this is not already possible given Chart() is using matplotlib, I would love to see this added as a feature and/or have someway to call any matplotlib stuff (e.g., pyplot.ion).

Describe alternatives you've considered

I tried just calling pyplot.ion, but that does not enable intractability.

Additional context

No response

freakboy3742 commented 8 months ago

Thanks for the suggestion.

It's not currently possible, but it's certainly in the realm of the possible. Toga's Canvas element (which is what Chart is built on) has handlers to capture mouse clicks and movements; presumably there is some interface that can be be implemented in a matplotlib backend that will convert canvas-level mouse clicks into the relevant Matplotlib commands.

I don't have any immediate plans to work on this, but if you're interested, I'd encourage you to take a look yourself. Although it might seem daunting, the code for toga-chart is a relatively straightforward mapping from Matplotlib's backend API to Toga's Canvas API. It is implemented as a matplotlib backend, the core of which is a subclass of matplotlib.backend_bases.RendererBase. From a quick look at the docs, there is a button_press_handler that is likely at the core of how interactive charts work. You might be able to learn more by poking around the source code for one of the existing interactive backends (Tk, GTK or Qt).

kaylai commented 8 months ago

Hi there, thanks for the information and resources! I think I will take a peak at the matplotlib backend and at how Tk handles that integration. I doubt I'll make much progress, but if I do, I'll get in touch about contributing via a PR.