plotjuliasliderinteractionmakie.jl

How to interact with plot using keyboard arros?


My interactive plot (topoplot) reacts to mouse signals, but how to make it reacting to keyboard signals?

Here is my code:

f = Figure()
xs = 1:1:193 #range(-30, 120, length = size(dat_e, 2))
sg = SliderGrid(f[2, 1],
(label="time", range=xs, format = "{:d} ms", startvalue = 100),
)
time = sg.sliders[1].value
str = lift(t -> "[$t ms]", time)
topo_slice = lift((t, data) -> mean(data[1:30, t, :], dims=2)[:,1], time, dat_e)
topo_axis = Axis(f[1, 1], aspect = DataAspect())
topo = eeg_topoplot!(topo_axis, topo_slice, 
    raw.ch_names[1:30]; 
    positions=pos, # produced  automatically from ch_names
    label_text=true) # aspect ratio, correlation of height and width

text!(topo_axis, 1, 1, text = str,  align = (:center, :center))
#topo_slice = lift((t, data) -> data[:, :, t], time, topo)

xlims!(-0.2, 1.1)
ylims!(-0.2, 1.2)
hidedecorations!(topo_axis)
hidespines!(topo_axis) 
f

There is an official instruction https://docs.juliahub.com/AbstractPlotting/6fydZ/0.12.11/interaction.html, but as usual with Julia documentations, there is no example and I have no idea how implement it in my code.

How my plot looks like: enter image description here


Solution

  • Expanding on the answer from before:

    T = 10
    
    pts = range(-1, 1, length=100)
    ts = reshape(1:T, 1, 1, :)
    topo = cos.(pts) .+ cos.(ts .* pts')
    
    fig = Figure()
    ax = Axis(fig[1, 1])
    
    sg = SliderGrid(fig[2,1],
        (label="time", range=1:T))
    
    time = sg.sliders[1].value
    
    str = lift(t -> "[$t ms]", time)
    text!(ax, str)
    
    topo_slice = lift((t, data) -> data[:, :, t], time, topo)
    
    # decrement/increment slider with left/right keys
    on(events(fig).keyboardbutton) do btn
        if btn.action in (Keyboard.press, Keyboard.repeat)
            if btn.key == Keyboard.left
                set_close_to!(sg.sliders[1], time[] - 1)
            elseif btn.key == Keyboard.right
                set_close_to!(sg.sliders[1], time[] + 1)
            end
        end
    end
    
    contour!(ax, topo_slice)
    
    hidedecorations!(ax)
    hidespines!(ax) 
    fig