How do I select a line by clicking in Makie?

I want to be able to select a line on the plot by clicking on it. Ideally, when I click on any line, the number of that line will appear on the screen. I wrote my code based on the tutorial, but there is no example with lines, so I did what I did. Tutorial: Page Redirection

At the moment I have no idea what these numbers are telling me and why. They are not even coordinates of clicked points.

P.S. Actually it is just a starting point. I want to create event interaction on series and topoplots. But for now it would be great to find out the basics.

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)

lines!(xs, ys)
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)
on(events(f).mousebutton, priority = 2) do event
    if event.button == Mouse.left && event.action == Mouse.press
        plt, i[] = pick(f)
        str = lift(i -> "$(i)", i)
        text!(ax, 1, -0.5, text = str,  align = (:center, :center)) 
    end
end
f

Below are some examples of the interaction between clicking and the number displayed (the red dot is where I click).


From the docs you linked

 For lines and linesegments it's end position of the clicked line segment.

So to get the coordinates, you need to first ensure plt is one of the two lines objects you created at lines 10 and 11 of your script, and then index into the xs and ys arrays using i.

It is not clear to me what the end position of the clicked line segment is. What is the line segment, what is the end position?

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

Thank you! This helps to get coordinates for lines, but unfortunately doesn’t tell you which line was selected. But that’s helpful too, it perfectly works for topoplots!