Altair: Possible to use the slider values in transform_calculate?

Created on 11 Jul 2018  路  7Comments  路  Source: altair-viz/altair

Forum would be a better place for this question, sorry, but I couldn't find one yet :)

From what I can see, the slider can only control selection, is that correct?

I want to be able to give my users the option to scale the chart within a certain range, but would prefer not to have to pre-calculate the results for each point in that range.

If I could get the slider position, I could use that in a transform_calculate to do exactly what I need and reduce the size of the html by 50 times!

documentation enhancement question

Most helpful comment

Interesting... didn't know this was possible!

It might be useful to think about adding a high-level syntax for doing this; e.g.

chart.transform_filter(
   alt.datum.field_name < slider.value
)

or something along those lines, so that users don't have to worry about internal details surrounding names & formats.

All 7 comments

No, I don't think that is possible. Unless I'm mistaken, Vega-Lite can only bind slider values to selections, which cannot be used to set arbitrary chart properties.

I believe you can do something along those lines with Vega, though. It might be worth asking on the vega slack channel.

Along the same lines - is it possible to use a slider's values to select subsets of data?

E.g. I'd like to make a plot like this, but instead of only selecting the year selected in the slider, select all years >= that value:

https://altair-viz.github.io/gallery/us_population_over_time.html?highlight=slider

Did you managed to do that @breadbaron?

@adrielvieira no I don't believe so

I managed to change the color based on the value of the slider, following your mention of value comparison:

slider = altair.binding_range(min=0, max=100, step=1)
selector = altair.selection_single(name="SelectorName", fields=['field_name'],
                                   bind=slider, init={'score': 100})

altair.Chart(dataframe).mark_bar().encode(
    y='other_field_name',
    x='field_name',
    color=altair.condition('datum.field_name< SelectorName_ field_name', altair.value('lightblue'), alt.value('lightgray')),
).add_selection(selector)

You can use the same logic to filter data instead of change color:


slider = altair.binding_range(min=0, max=100, step=1)
selector = altair.selection_single(name="SelectorName", fields=['field_name'],
                                   bind=slider, init={'score': 100})

altair.Chart(dataframe).mark_bar().encode(
    y='other_field_name', 
    x='field_name').add_selection(
    selector
).transform_filter(
    'datum.field_name < SelectorName_ field_name'
)

Interesting... didn't know this was possible!

It might be useful to think about adding a high-level syntax for doing this; e.g.

chart.transform_filter(
   alt.datum.field_name < slider.value
)

or something along those lines, so that users don't have to worry about internal details surrounding names & formats.

Wow, cool @adrielvieira !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pabloinsente picture pabloinsente  路  3Comments

dzonimn picture dzonimn  路  3Comments

Juan-132 picture Juan-132  路  3Comments

bmcfee picture bmcfee  路  3Comments

nielsmde picture nielsmde  路  4Comments