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

Is it currently possible to draw and updating plots from any backend? #19

Open
Boxylmer opened this issue Jan 4, 2022 · 2 comments
Open

Comments

@Boxylmer
Copy link

Boxylmer commented Jan 4, 2022

I'm trying to build a GTK application in glade, I know it's possible to push!(widget, container) and can access object made in Glade with dict-like accessors i.e.,

gladefile = GtkBuilder(filename=joinpath(@__DIR__, "testfile.glade"))
win = gladefile["main_window"]
button1 = gladefile["button1"]

But once I'm here, how can I use GtkObservables.jl to link a Plots.jl (or any other backend) plot to a GtkCanvas or similar widget, and insert it into my GUI? I'm starting with something simple here:

using Gtk, Plots, GtkObservables

gladefile = GtkBuilder(filename=joinpath(@__DIR__, "testfile.glade"))
win = gladefile["main_window"]
button1 = gladefile["button1"]
plot_frame = gladefile["plot_frame"]
canv = GtkCanvas()

showall(win)

function button_clicked_callback(widget)
    println(get_gtk_property(widget, :name, String), " was clicked!")
    h = Gtk.height(plot_frame)
    w = Gtk.width(plot_frame)
    x = randn(1000)
    y = randn(1000)
    # myplot = scatter(x, y)
    # push!(plot_frame, myplot)  # unsafe_convert doesn't know how to deal with this
end
signal_connect(button_clicked_callback, button1, "clicked")
@BioTurboNick
Copy link
Collaborator

What I ended up doing was to generate the plot normally, save it, and then reload and copy the image into the Canvas. I needed it saved anyway, but if you don't, maybe this isn't the right path. But I was also having trouble with the rendering.

using ImageCore, ImageIO

imgcanvas = canvas(UserUnit, 500, 500)
selectedimg = Observable{Union{Nothing, Matrix{RGB{N0f8}}}}(nothing)

function draw_canvas(c, img)
    img !== nothing || return
    copy!(c, img)
end

draw(draw_canvas, imgcanvas, selectedimg)

From what I was looking at for more direct rendering, you might need to try GR directly instead of using Plots, and you'll need to set some environment variables for GR to render correctly. Though there also may be some cross-platform issues with that right now.

@Boxylmer
Copy link
Author

Boxylmer commented Jan 4, 2022

Originally I was hesitant to do this based on speed requirements, but I bit the bulled and actually found that Plots.jl is surprisingly fast in creating saved images, but when your plots reach 10000+ data points (at least using scatter) things start to slow down a tad. I'm sure there's a workaround/set of trick to optimize for this, but for now this is a viable solution. Thanks!

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

No branches or pull requests

2 participants