How do I select a line by clicking in Makie?

Does this help:

using GLMakie

function picklines()
    f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), resolution = (1500, 700))
    ax = Axis(f[1, 1], xlabel = "Time [s]", ylabel = "Voltage amplitude [µV]")

    #N = 1:length(pos)
    positions = Observable(rand(Point2f, 10))

    xs = 0:0.01:10
    ys = 0.5 .* sin.(xs)

    lns = lines!(xs, ys)
    lns2 = lines!(xs, ys * 2)

    hidedecorations!(ax, label = false, ticks = false, ticklabels = false)
    hidespines!(ax, :t, :r)
    hlines!(0, color = :gray, linewidth = 1)
    vlines!(0, color = :gray, linewidth = 1)

    i = Observable((0.0,0.0))
    str = lift(i -> "$(i)", i)
    text!(ax, 1, -0.5, text = str,  align = (:center, :center))
    on(events(f).mousebutton, priority = 2) do event
        if event.button == Mouse.left && event.action == Mouse.press
            plt, p = pick(f)
            if plt == lns
                i[] = (xs[p], ys[p])
            elseif plt == lns2
                i[] = (xs[p], 2*ys[p])
            end
        end
    end
    f
end
1 Like