The Wayback Machine - https://web.archive.org/web/20201122143856/https://github.com/altair-viz/altair/issues/2274
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

What exactly does "fields" mean in selections? #2274

Open
armsp opened this issue Sep 1, 2020 · 1 comment
Open

What exactly does "fields" mean in selections? #2274

armsp opened this issue Sep 1, 2020 · 1 comment

Comments

@armsp
Copy link

@armsp armsp commented Sep 1, 2020

I have been working with bindings and selections and while fiddling with an example I had in mind I noticed something peculiar in the docs which I don't understand - the usage of fields in selections.

Take this example from docs -
Important code -

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

We will focus on name in slider and fields in selector.
Q) I think name is just a string placeholder used for displaying purposes?

Based on what I read here, fields are nothing but dataframe columns and encodings are self-explanatory.

I looked at the doc for selection_single and corresponding doc from vega-lite. But it still doesn't make sense to me.

Why?
There is NO cutoff field in the dataframe we pass to Chart.

Moreover, if you run the code below, where we replace cutoff with xval which is a field in the dataframe, then we see the exact same output.

slider = alt.binding_range(min=0, max=100, step=1)
selector = alt.selection_single(fields=['xval'], bind=slider, init={'xval': 50})

alt.Chart(df).mark_point().encode(
    x='xval',
    y='yval',
    color=alt.condition(
        alt.datum.xval < selector.xval,
        alt.value('red'), alt.value('blue')
    )
).add_selection(
    selector
)

Can you please tell me what is the role of cutoff (I know its purported function) and fields here?

@jakevdp
Copy link
Member

@jakevdp jakevdp commented Sep 1, 2020

fields specifies the field names that the selector projects over. A point is considered in the selection if its value for the specified field or fields is equal to the selection field (for single selections), is one of the selection's fields (for multiple selections), or is within the range of the selections fields (for interval selections).

That example is a bit weird, because the selection field does not correspond to any column in the data, but rather is specified as the name of a value that is referenced in a calculate transform. In this case the field can be an arbitrary string.

Does that answer your question?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.