Richard Wheeler
Richard Wheeler

Reputation: 637

Is there a way to zoom and then do a selection interval without further zooming with Altair

Have a scatter plot, I know you use bind on the scales for panning and using the wheel to zoom which is great. However once zoomed need a way to then do a selection interval without further zooming effects. Need a way to pause or escape via a shift key for example. In the vega-lite question it uses mouse events. There is no example at all of how to do this in Altair documentation. The API is there however the altair.EventType does not expose the mouse events.

Very much appreciate how to get this to work in Altair.

Upvotes: 3

Views: 1543

Answers (1)

jakevdp
jakevdp

Reputation: 86463

You can do this using an event modifier in the selection definition (there are a few examples in Altair's docs here).

For example, here is a chart where the zoom action is triggered when the shift key is not held, and the selection action is triggered when the shift key is held:

import altair as alt
from vega_datasets import data

source = data.cars()

zoom = alt.selection_interval(
    bind='scales',
    on="[mousedown[!event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[!event.shiftKey], mouseup] > mousemove!",
)

selection = alt.selection_interval(
    on="[mousedown[event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[event.shiftKey], mouseup] > mousemove!",
)

alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
).add_selection(zoom, selection)

The syntax for these event modifiers can be found in vega's eventStream selector documentation.

Upvotes: 6

Related Questions