Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altair - filter range by slider binding

I am trying to achieve something similar to this example, except instead of using the slider to select discrete values, I would like it to serve as the cutoff point in a transform_filter (i.e. values < slider value).

Right now I have something like this:

import altair as alt
from altair.expr import datum

slider = alt.binding_range(min=data.refill_time.min().value,
                           max=data.refill_time.max().value,
                           step = 1)
select_date = alt.selection_single(name='refill_time',
                                   fields=['refill_time'],
                                   bind=slider)

refills = alt.Chart(data).mark_bar(
).transform_filter(
    datum.refill_time < select_date.ref()
).encode(
    alt.X('refill_count:Q',
    alt.Y('group:N')
).add_selection(
    select_date
)

display(refills)

I tried looking at the data from slider but I couldn't find any fields that return it's current value (.ref() attempt). I also tried changing the selection_single to _multi and _interval but I just got invalid specification errors - I couldn't find any way to define the binding_range that would make sense to the selection.

Is this possible in altair?

like image 749
bergst15 Avatar asked Mar 25 '26 08:03

bergst15


1 Answers

Try the following filter expression:

transform_filter(
    "datum.refill_time < refill_time_refill_time"
)

where the strange looking "refill_time_refill_time" is build up by the selection name "refill_time" and the field name "refill_time". Although I am not 100% sure if thats correct, but in any case it should be the "slider text", so in the linked example it would be "year_year".

like image 169
Corvince Avatar answered Mar 27 '26 20:03

Corvince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!