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

Binding documentation #1113

Merged
merged 12 commits into from
Aug 29, 2018
Merged

Binding documentation #1113

merged 12 commits into from
Aug 29, 2018

Conversation

kindofluke
Copy link
Contributor

@kindofluke kindofluke commented Aug 23, 2018

This pull request is to facilitate discussion of the binding documentation for which documentation help was requested in #1060

this has been my approach:

  • With adding input elements and bindings the page should just be called interactions.
  • Add simple dropdown example
  • add an example using filter
  • enumerate all of the avail input elements.
  • bind to "scales" magic for panning and zooming
  • headers are all out of wack
  • spelling and proofread

I am attaching a small screen recording of the two examples:

binding_small

Opening the PR for general feedback I don't think its ready but I need some ideas on next steps.

Copy link
Collaborator

@jakevdp jakevdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start overall! A few comments throughout.


- the :class:`selection` object which captures interactions from the mouse or through other inputs to effect the chart. Inputs can either be events like moust clicks or drags. Inputs can also be elemnts like a drop-down, radio button or slider. Selections can be used alone but if you want to have change any element of your chart you will need to connect them to a *condition*.
- the :class:`condition` function takes the selection input and changes an element of the chart based on that input.
- the :class:`bind` property of a selection which establishes a two-way binding between the selection and an input element of your chart.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace :class:`selection` with :func:`selection` and :class:`condition` with :func:`condition` .

bind is neither a class nor a function in Altair, so there's no appropriate sphinx directive to refer to it. You can just use ``bind``.

@@ -236,9 +251,101 @@ over them with your mouse:
multi_mouseover = alt.selection_multi(on='mouseover', toggle=False, empty='none')
make_example(multi_mouseover)

Binding: Adding Data Driven Inputs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With an understanding of the selection types and conditions, you can now add data-driven input elements to the charts using the :class:`bind`. As specified by `Vega-lite binding <https://vega.github.io/vega-lite/docs/bind.html#input-element-binding>`_, selections can be bound two-ways:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:class:`bind` -> ``bind`` attribute?

)


scatter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than assigning the chart to scatter and putting scatter below, just leave out the assignment altogether.

^^^^^^^^^^^^^^^^^^^^^
With single selections, an input element can be added to the chart to establish a binding between the input and the selection.

For instance, using our example from above a dropdown can be used to highlight cars from a specific :origin: :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with this :origin: syntax in RST. What does it do?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change :origin: to ``origin``? Or just origin?


For instance, using our example from above a dropdown can be used to highlight cars from a specific :origin: :

.. altair-plot::
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line is required after the altair-plot directive


Bindings and input elements can also be used to filter data on the client side. Reducing noise in the chart and allowing the user to see just certain selected elements:

.. altair-plot::
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch. I only found one other reference (on the weather chart which was funny given that I missed it before) I think we should be there.

)


scatter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't assign to scatter (as above)

^^^^^^^^^^^^^
With interval selections, the bind property can be set to the value of :"scales":. In these cases, the binding will automatically respond to the panning and zooming along the chart:

.. altair-plot::
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

alt.Color('Origin:N', legend=None),
alt.value('lightgray'))

scatter = alt.Chart(cars).mark_point().encode(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove assignment

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 23, 2018

If you're willing to tackle it, it might be useful to add some examples of the various input bindings in the gallery. The only one we have now is a bit more complicated than I'd like it to be.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 23, 2018

(That would also lead to fewer N/A entries in the table!)

@kindofluke
Copy link
Contributor Author

@jakevdp I also was going to add this as just a good interaction example. (you are the original author of the vega example but I can't quite get the interval select to work when you select from the scatter and the bars adjust.

GIST Seattle Weather

@kindofluke
Copy link
Contributor Author

I totally agree. I will try to make a compound chart that has drop down radio buttons, and the rest.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 23, 2018

We do have a version of that in the gallery: https://altair-viz.github.io/gallery/seattle_weather_interactive.html

@kindofluke
Copy link
Contributor Author

🤦 totally missed this.

@kindofluke
Copy link
Contributor Author

Do you think it is possible to change the x or y fields through a selection?

I was thinking of this example Scatter Plot With Varying Dimension.

I think this is possible with with an expression referring to the signal value. If it is possible it could be really helpful as example of a selection not referring to an encoding. If not, I am still going to use this movie dataset because it's rich scatter plot that benefits from filters and highlights based on inputs.

I can do radio buttons to highlight movies of a certain imdb rating. Maybe a slider to filter the gross total of each film. I probably won't recreate the null values though that is a nice feature.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 24, 2018

Do you think it is possible to change the x or y fields through a selection?

No, that's not possible in Vega-Lite.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 24, 2018

You can work around it by stacking the two columns, adding an indicator variable, and filtering on that indicator variable.

@kindofluke
Copy link
Contributor Author

Addressed most of your comments in the latest and added an example based on movies.

the example is having a problem with a radio buttons (at least on my browser) I am going to look at that tomorrow (getting late)

image

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 24, 2018

One of the issues with bindings is that they are not initialized properly, and do not apply any selection or filtering until the user interacts with them. This is particularly problematic in the case of multiple bindings, because you have to click all of them at least once before they will interact properly. That's one reason I haven't done much with docs or examples of bindings up to this point.

It's being addressed in vega-lite: see vega/vega-lite#4139

@kindofluke
Copy link
Contributor Author

Ok, I reformatted the example to avoid the initialization problem and focus on teaching how the bindings can work in different ways.

I recommend building the gallery page to view it.

The issue I ran into was that the dates in the data file are "4-Mar-74" which means i really needed a function to parse them and I couldn't use an expression. I thought I found a clever workaround to download the data into a dataframe first but it appears that did not work in CI (it did work locally).

I will try and fix the build error but for now you can sample it.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 24, 2018

Yeah, we try to avoid anything requiring HTTP access in the example scripts, so that tests can be run in contexts with no web access.

Vega-Lite will parse the date properly if it's used in a temporal encoding within the chart.

If you're not using the column in an encoding but you still want it parsed as a date, you can use the alt.UrlData(url, format=alt.DataFormat(parse={'Release_Date': 'date'))

@kindofluke
Copy link
Contributor Author

OK Everything is all set now I think.

We have a solid example of all the bindings and a decent write up of how the bindings actually work and connect to the selections.

Let me know what you think happy to keep tinkering or we can brainstorm some more examples.

Copy link
Collaborator

@jakevdp jakevdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A couple more minor comments and then we can merge.

@@ -1,19 +1,31 @@
.. currentmodule:: altair

.. _user-guide-selections:
.. _user-guide-interactions:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the user-guide-selections cross reference is used a couple times elsewhere in the docs. Since you're renaming it here, it should be renamed elsewhere as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this in previous. Updated found one reference as stated above.

^^^^^^^^^^^^^^^^^^^^^
With single selections, an input element can be added to the chart to establish a binding between the input and the selection.

For instance, using our example from above a dropdown can be used to highlight cars from a specific :origin: :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change :origin: to ``origin``? Or just origin?

Luke Shulman added 2 commits August 29, 2018 15:59
1. Find all references to `user-guide-selections` and update to refer to the new page name
2. change coding style on chart reference in interactions.
@kindofluke
Copy link
Contributor Author

All set on latest commit. If I am too much trouble you can throw me another one from #1060.

@jakevdp
Copy link
Collaborator

jakevdp commented Aug 29, 2018

Looks good – thanks so much for all your work on this!

@jakevdp jakevdp merged commit 701c656 into vega:master Aug 29, 2018
@jakevdp jakevdp mentioned this pull request Aug 29, 2018
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants