The code below is an adaptation from the official panel vega guide. I wanted to add an additional dropdown filter, so I can select an Island
and then select different values based on the defined brush
, but it doesn’t work. Even after selecting an Island
I still see the values from other islands appearing in the table. Also when changing the brush to multi
or single
, no values are passed to the selection sub-object. I also changed the brushed values from quantitive to categorical.
The ask is: how can I use two filters with the combination of linked chart to table, 1st filter is the dropdown on some category in the chart and the 2nd filter is the brush itself based on categorical values on the X axis and quantitative values on the Y axis.
P.D. Attached is the gif with the panel’s output.
import pandas as pd
penguins_url = "https://raw.githubusercontent.com/vega/vega/master/docs/data/penguins.json"
df = pd.read_json(penguins_url)
input_dropdown = alt.binding_select(options=df.Island.unique().tolist(), name='Island ')
island_selection = alt.selection_single(fields=['Island'], bind=input_dropdown)
brush = alt.selection_interval(name='brush') #
chart = alt.Chart(df,height=150).mark_circle().encode(
x=alt.X('Species:N'),
y=alt.Y('min(Beak Length (mm)):Q', scale=alt.Scale(zero=False)),
color=alt.condition(brush, 'Species:N', alt.value('lightgray'))
).add_selection(
brush, island_selection
).transform_filter(island_selection)
def filtered_table(selection):
if not selection:
return '## No selection'
query = ' & '.join(
f'{values[0]} <= `{col}` <= {values[1]}'
if pd.api.types.is_numeric_dtype(df[col])
else f'`{col}` in {values}'
for col, values in selection.items()
)
return pn.Column(
f'Query: {query}', selection.items(),
pn.pane.DataFrame(df.query(query), width=600, height=300)
)
vega_pane = pn.pane.Vega(chart, debounce=10)
pn.Row(vega_pane, pn.bind(filtered_table, vega_pane.selection.param.brush))