Makie.jl: barplot bar index

Created on 3 Jun 2020  Â·  3Comments  Â·  Source: JuliaPlots/Makie.jl

How can I get the index of a bar in a bar plot when I click on it?
I tried using mouse_selection, but I get more than one index per bar.
MWE:

function setup_click(scene, idx=1)
    selection = Observable{Any}(0)
    on(scene.events.mousebuttons) do buttons
        if ispressed(scene, Mouse.left) && AbstractPlotting.is_mouseinside(scene)
            plt, click_idx = mouse_selection(scene)
            selection[] = (plt, click_idx)[idx]
        end
    end
    return selection
end
​
cs = Observable(to_colormap(:viridis, 4))
sc = barplot(rand(Point2f0, 4), color=cs)
​
ci=setup_click(sc, 2)
​
on(ci) do i
    println(i)
end

If I click on the lower part of the bar I get a number that is different than if I click on the upper part of the bar. It seems that always the upper one is the lower one - 1

Most helpful comment

I messed around with shaders a bit to display the ids that are picked. For barplot, each bar is plotted using a rectangle mesh, which consists of two triangular faces. Each face has it's own index.
Screenshot from 2020-06-03 21-17-10

To be a bit more precise, the index you get is the index of the last vertex that's used to draw a face. So pattern will go (3, 4), (7, 8), (11, 12), etc. With that div(click_idx+1, 4) should give you the bar index from the index mouse_selection returns.

All 3 comments

I messed around with shaders a bit to display the ids that are picked. For barplot, each bar is plotted using a rectangle mesh, which consists of two triangular faces. Each face has it's own index.
Screenshot from 2020-06-03 21-17-10

To be a bit more precise, the index you get is the index of the last vertex that's used to draw a face. So pattern will go (3, 4), (7, 8), (11, 12), etc. With that div(click_idx+1, 4) should give you the bar index from the index mouse_selection returns.

Thanks a lot for explaining the behavior!
I wonder if it would be of interest to have an API for selecting plots and elements form plots that abstracts away this details.

Closing as this is the intended behavior. I moved the discussion about the selection API over in #240

Was this page helpful?
0 / 5 - 0 ratings