Reputation: 195
I am trying to create a chart in Altair with dropdowns. Here's the code
df = pd.DataFrame([["Merc","US",500, "Car_A"], ["BMW","US" ,55, "Car_B"]
, ["BMW","US",40, "Car_C"], ["Merc", "China",650, "Car_D"]
, ["BMW","US",80, "Car_E"], ["Merc", "China",850, "Car_F"]], columns=list("ABCD"))
position_dropdown_Type = alt.binding_select(name = "Type:" , options=[None] + list(df["B"].unique()), labels = ['All'] + list(df["B"].unique()))
position_selection_Type = alt.selection_single(fields=['B'], bind=position_dropdown_Type)
position_dropdown_1_Car = alt.binding_select(name = "Company:", options=[None] + list(df["A"].unique()), labels = ['All'] + list(df["A"].unique()))
position_selection_1_Car = alt.selection_single(fields=['A'], bind=position_dropdown_1_Car, name = "__")
#interval = alt.selection_multi(fields=['GICS_SUB_IND'], bind='legend')
Car_bar=alt.Chart(df).mark_bar().encode(
color = 'A',
y = alt.Y('C', scale=alt.Scale(domain=[0, 1.2*df["C"].max()]),
title = 'Range'),
x = alt.X('D:O', sort = alt.EncodingSortField(field="C", order='ascending', op='max'), title = 'Care_Name')
).add_selection(interval).add_selection(position_selection_Type).transform_filter(position_selection_Type).add_selection(position_selection_1_Car).transform_filter(position_selection_1_Car)
(Car_bar).properties(width = 700)
the code works with a graph like this when all values are selected
However, when I make a selection, the bar width takes the entire width as seen below
Defining size inside mark_bar(size = 10) is not an option as the code will be accessing different datasets with wide range of sample size. Also, since the dropdown list will be quite long, selecting from the legend is also not ideal.
Is there a way to keep the width same for the bars with the selection from dropdown?
edited - removing the properties option at the end also does not solve the issue
Upvotes: 0
Views: 166
Reputation: 822
It seem the issue is with the properties(width = 700)
setting. It is forcing the bar width to be large enough to satisfy the width setting. If you remove that, it will give adapt the bar width accordingly.
Upvotes: 2