Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blank dropdown option that doesn't filter #2096

Closed
ZaxR opened this issue Apr 17, 2020 · 6 comments
Closed

Add blank dropdown option that doesn't filter #2096

ZaxR opened this issue Apr 17, 2020 · 6 comments
Labels

Comments

@ZaxR
Copy link

ZaxR commented Apr 17, 2020

I have a chart with a dropdown selector, and the selection is responsible for filtering the data displayed in the chart. I'm trying to add a "blank" option to the binding_select that will result in no filtering (i.e. "display all data"). Is there an easy way to do that?

I was looking at alt.condition and filter predicates, but wasn't able to figure it out. I was playing with the following example, where i'd want an "All" selection in the binding_select:

import altair as alt
from vega_datasets import data


cars = data.cars.url

input_dropdown = alt.binding_select(options=['All', 'Europe','Japan','USA'])
selection = alt.selection_single(fields=['Origin'],
                                 bind=input_dropdown,
                                 name='Country of ',
                                 init={'Origin': 'Europe'})
color = alt.condition(selection,
                    alt.Color('Origin:N', legend=None),
                    alt.value('lightgray'))

alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    tooltip='Name:N'
).add_selection(
    selection
).transform_filter(
    selection
   # Looking for something like <no filter> if selection == 'All' else selection
)

Thanks for the library and for the help!

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 18, 2020

It's a bit hacky, but this works:

alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    tooltip='Name:N'
).add_selection(
    selection
).transform_filter(
   f"({selection.Origin}[0] == 'All') || ({selection.Origin}[0] == datum.Origin)"
)

It would be nice to update the altair expression syntax so that this condition could be written like this instead:

(selection.Origin[0] == 'All') | (selection.Origin[0] == alt.datum.Origin)

@ZaxR
Copy link
Author

ZaxR commented Apr 18, 2020

Thanks for the quick response.

That syntax doesn't seem to be working for me - am I doing something wrong? Here's what I get when I try (w/ Altair 4.1.0, vega 3.2.0):
Screen Shot 2020-04-18 at 2 18 57 PM

Screen Shot 2020-04-18 at 2 24 29 PM

Text for reproducability:

import altair as alt
from vega_datasets import data

cars = data.cars.url

input_dropdown = alt.binding_select(options=['All','Europe','Japan','USA'])
selection = alt.selection_single(fields=['Origin'],
                                 bind=input_dropdown,
                                 name='Country of ',
                                 init={'Origin': 'Europe'})

alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    tooltip='Name:N'
).add_selection(
    selection
).transform_filter(
   f"({selection.Origin}[0] == 'All') || ({selection.Origin}[0] == datum.Origin)"
)

@ZaxR
Copy link
Author

ZaxR commented Apr 18, 2020

Looks like adding None as an option works, and the associated label is null (idea came from https://vega.github.io/vega-lite/docs/bind.html#input-element-binding). Not ideal, but does the right thing it looks like:

Screen Shot 2020-04-18 at 2 39 08 PM

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 18, 2020

Oh, sorry, you have to use selection names that are valid identifiers. Try this:

import altair as alt
from vega_datasets import data

cars = data.cars.url

input_dropdown = alt.binding_select(options=['All','Europe','Japan','USA'], name="Country")
selection = alt.selection_single(fields=['Origin'],
                                 bind=input_dropdown,
                                 init={'Origin': 'Europe'})

alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    tooltip='Name:N'
).add_selection(
    selection
).transform_filter(
   f"({selection.Origin}[0] == 'All') || ({selection.Origin}[0] == datum.Origin)"
)

@ZaxR
Copy link
Author

ZaxR commented Apr 18, 2020

Awesome - thanks!

@joelostblom
Copy link
Contributor

@ZaxR I am going through Altair issues to find those that have been resolved and can be closed. It looks to me like this issue has been solved so I am closing it, but please feel free to reopen and add a comment if there is something you don't think is resolved yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants