Filter date with interval selector in a mark_geoshape (ALTAIR)

I want to do a choropleth map that you can filter the date range with an interval selector (It has to be an interval selector because it also changes other charts in the dashboard). I do the interval selection on the following chart:

date selector interval

This works well for me with filtering dates in line charts, as I can change the domain directly in the axis. However, in the mark_geoshape I do not have that option.

counties = alt.Chart(county_data).transform_lookup(
    lookup='CountyFIPS',
    from_=alt.LookupData(gdf_us_counties, 'id', ['type', 'geometry'])
).mark_geoshape(
    stroke='black')
....
).project(
    type='albersUsa'
).transform_aggregate(
    ss = 'sum(Shootings)',
    groupby = ['CountyFIPS', 'geometry', 'type', 'State', 'County']
).encode(
    tooltip = [alt.Tooltip('State:N'), alt.Tooltip('County:N'), alt.Tooltip('ss:Q', title = 'Total Shootings')],
    color = alt.Color('ss:Q', scale = alt.Scale(scheme='viridis')),
).properties(
    width=600,
    height=450,
    title='Shootings per County'
)

I have tried accessing the endpoints of the iterval selector following the steps in https://github.com/vega/altair/issues/1765 using transform calculate: However, it didn't work. I tried to acces the value and printing it with mark text. At first, i get the non-defined value and as soon as I used the filter the text dissappeared, so I assume it is not getting any value.

The data has the following format:

Data sample

What I want to achieve (and I did by manually filering it) is to plot something like this:

desired plot

code to generate data sample:

data = {
    'Date': pd.to_datetime(['2024-01-15', '2024-02-20', '2024-03-10'] * 6 + ['2024-04-05']),
    'State': ['California', 'Texas', 'Florida'] * 6 + ['New York'],
    'StateFIPS': np.random.randint(10, 57, size=20),
    'Year': [2024] * 20,
    'Month': np.random.randint(1,13,size=20),
    'region': ['West', 'South', 'South'] * 6 + ['Northeast'],
    'Shootings': np.random.randint(0, 20, size=20),
    'County': ['Los Angeles', 'Houston', 'Miami'] * 6 + ['New York City'],
    'CountyFIPS': np.random.randint(1001, 57000, size=20)
}

county_data = pd.DataFrame(data)

Upvotes: 0

Views: 28

Answers (0)

Related Questions