Help with a new routine to add labels to the tail of each plot

Thanks for that!

It seems like the pixel coordinates seem to transfer between the two scenes so that this works.


using CairoMakie

function get_last_point_in_pixels(plt, plot)
  pts = plt[1][] # get the points 
  # find the last point
  p = argmax(x->x[1],pts)
  # translate the point to pixel space
  pp = Makie.plot_to_screen(plot, p)
  return pp 
end 
function last_point(plot, ax)
  obs = Observable{Point2f}(get_last_point_in_pixels(plot, plot))
  onany(ax.scene.camera, ax.scene.camera.pixel_space, ax.scene.camera.projectionview) do _, _, _
    obs[] = get_last_point_in_pixels(plot, plot)
  end
  return obs 
end 

f = Figure()
ax = Axis(f[1, 1])
Axis(f[2, 1])
Axis(f[2, 2])
Axis(f[1, 2])
leg = LScene(f[1, 1, Right()], scenekw = (; camera = campixel!), show_axis = false)
padding = 10
y = map(1:5) do i
    Observable(Point2f(0, i*33))
end

for i in 1:5
    l = lines!(ax, (1:10) .* i*randn())
    y[i] = last_point(l, ax)
    y2 = @lift $(y[i])[2]
    textpos = @lift Point2f(padding, $(y[i])[2])
    text!(leg, padding, y2, text = "Label $i", color = l.color, align = (:left, :center), space = :pixel)
    
    linepts = @lift [(Point2f(padding-20, $(y2)), 
                      Point2f(padding-5, $(y2))
          )]
    @show linepts
    linesegments!(leg, linepts, color = :lightgrey)
end
textbb = mapreduce(x -> Makie.boundingbox(x, :pixel), union, leg.scene.plots)
leg.width = textbb.widths[1] + 5 * padding
hidespines!(ax)
f

I can think of some scenarios where you might want the labeling lines to “go into” the axis. Is there any way to make that work?

I’ll have to hook this up to the auto placement option next. (That just needs a little rejiggering…)