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

Possible to display pre-existing plot in Gtk4 ? #549

Open
jonathanBieler opened this issue Aug 30, 2024 · 2 comments
Open

Possible to display pre-existing plot in Gtk4 ? #549

jonathanBieler opened this issue Aug 30, 2024 · 2 comments

Comments

@jonathanBieler
Copy link

jonathanBieler commented Aug 30, 2024

Hi, I'm trying to use Plots with GR in Gtk4, the example provided works well but I'd like to be able to take any plot and display it on the canvas. Is there a way to do that ? I couldn't figure it out. Calling display do show the plot initially but it doesn't redraw when the size change. Thanks!

using Gtk4, Serialization
using Plots
using Printf

c = GtkCanvas()

p = scatter(rand(100))

function _plot(ctx, w, h)
    ENV["GKS_WSTYPE"] = "142"
    ENV["GKSconid"] = @sprintf("%lu", UInt64(ctx.ptr))
    #plot(p, size=(w, h))
    
    display(p)
end

@guarded draw(c) do widget
    ctx = getgc(c)
    w = width(c)
    h = height(c)
    @show w, h
    rectangle(ctx, 0, 0, w, h)
    set_source_rgb(ctx, 1, 1, 1)
    fill(ctx)
    _plot(ctx, w, h)
end

win = GtkWindow("Gtk4 example", 600, 450)
win[] = c

e = GtkEventControllerMotion(c)

function on_motion(controller, x, y)
    win.title = @sprintf("(%g, %g)", x, y)
    reveal(c) # triggers a redraw
end

signal_connect(on_motion, e, "motion")
@jheinen
Copy link
Owner

jheinen commented Aug 30, 2024

I just added a Gtk4 Plots example:

using Gtk4
using Plots
using Printf

c = GtkCanvas()

function _plot(ctx, w, h)
    ENV["GKS_WSTYPE"] = "142"
    ENV["GKSconid"] = @sprintf("%lu", UInt64(ctx.ptr))
    gr(show=true)
    plot(randn(10, 3), size=(w, h))
end

@guarded draw(c) do widget
    ctx = getgc(c)
    w = width(c)
    h = height(c)
    @show w, h
    rectangle(ctx, 0, 0, w, h)
    set_source_rgb(ctx, 1, 1, 1)
    fill(ctx)
    _plot(ctx, w, h)
end

win = GtkWindow("Gtk4 example", 600, 450)
win[] = c

e = GtkEventControllerMotion(c)

function on_motion(controller, x, y)
    win.title = @sprintf("(%g, %g)", x, y)
    reveal(c) # triggers a redraw
end

signal_connect(on_motion, e, "motion")

As you can see, for Plots you will have to add the gr(show=true) command.

@jonathanBieler
Copy link
Author

Thanks for the quick answer. What I'm trying to do is to have a Canvas opened and then display a plot on it, then plot another one, etc. but I your example the plot is "hardcoded" in the _plot function and redone at each update.

Something like that but this doesn't redraw when resizing :

p = plot(randn(10, 3))

function _plot(ctx, w, h)
    ENV["GKS_WSTYPE"] = "142"
    ENV["GKSconid"] = @sprintf("%lu", UInt64(ctx.ptr))
    gr(show=true)
    display(p)
end

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