-
Notifications
You must be signed in to change notification settings - Fork 796
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
Clarify usage of initial values in selection intervals with dates #3643
Comments
Thanks for raising this @dsmedia
I agree with your assessment that this lacks documentation, I certainly didn't know this was possible! However, I must say I really dislike the solution provided on stackoverflow Given that was written over 4 years ago, I'd be more interested in either:
Confirmed solutionsThese are all the solutions I can confirm work so far: Code block
from datetime import date, datetime
import altair as alt
import pandas as pd
import polars as pl
from vega_datasets import data
source = data.sp500.url
# Define an initial date range as timestamps
window_pd = pd.to_datetime(["2005-01-01", "2009-01-01"]).astype(int) / 1e6
window_pl = pl.Series([date(2005, 1, 1), date(2009, 1, 1)]).dt.timestamp("ms")
window_stdlib = (
datetime(2005, 1, 1).timestamp() * 1e3,
datetime(2009, 1, 1).timestamp() * 1e3,
)
window_alt = alt.DateTime(year=2005), alt.DateTime(year=2009)
# Create a brush (interval) selection with initial range
brush = alt.selection_interval(encodings=["x"], value={"x": window_pd})
brush = alt.selection_interval(encodings=["x"], value={"x": window_pl})
brush = alt.selection_interval(encodings=["x"], value={"x": window_stdlib})
brush = alt.selection_interval(encodings=["x"], value={"x": window_alt})
base = (
alt.Chart(source, width=600, height=200).mark_area().encode(x="date:T", y="price:Q")
)
upper = base.encode(alt.X("date:T").scale(domain=brush))
lower = base.properties(height=60).add_params(brush)
chart = upper & lower
chart Since this is both the shortest, and doesn't introduce a dependency - I would lean towards it personally: window_alt = alt.DateTime(year=2005), alt.DateTime(year=2009)
I think it would make this functionality more discoverable if it were a separate example, with a more specific title like:
Or include some other time-series terminology, if that can provide a more accurate description? Would be similar to this pair of examples:
AFAIK, the description there is copy/pasted from the generated docs - which traces back via these two:
Probably a good idea, also maybe a mention in times_and_dates
I want to say no, simply because we can't do this for every datetime type across all libraries. It seems to me that it wants a POSIX timestamp with |
@dsmedia how you feel about this issue, following #3667? You mentioned 3 potential solutions, I think some narrative docs might work well here: cc @joelostblom as we recently updated interactions in #3544 and you have two other PRs open (#3629, #3628) that would add more content |
Sure. There was a section on this in the now-obsolete V4 Vega Lite docs (https://vega.github.io/vega-lite-v4/docs/init.html#:~:text=To%20initialize%20a%20selection%2C%20set,initial%20value%20of%20the%20selection.) I don't see this as clearly spelled out now in the vega-lite docs now that init is no longer a valid property. My instinct would normally be to apply what's in the vega-lite docs to the Altair documentation. Do we feel this also would benefit from better documentation upstream? |
What is your suggestion?
When setting initial values for a selection interval based on date values, users must manually convert dates to timestamps, as discussed here. This requirement does not appear to be clearly documented or exemplified in this repository. Solutions could be one or more of the following:
Potential Solutions
Questions:
Sample diff for current example gallery item
Have you considered any alternative solutions?
I've proposed three possible solutions above.
The text was updated successfully, but these errors were encountered: