You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Altair的selection_single单选组件无法触发图表更新?

下拉框无法控制图表内容的修复方案

问题原因

你仅完成了下拉选择器的创建与绑定操作,未给图表配置对应的数据筛选规则,选择器的取值变化无法作用到图表数据源上,因此不会触发展示内容的更新。

修复步骤

  • 在图表配置链中新增transform_filter参数,传入你定义的selection变量,建立选择器和数据筛选的关联
  • 若series字段本身为单个行业值,可删除多余的split、explode操作,直接取唯一值作为下拉选项即可;如果确实是多值逗号分隔格式,保留原拆分逻辑即可。

修复后完整代码

source = data.unemployment_across_industries.url
df = data.unemployment_across_industries()
series_val = df.series.unique().tolist()
print(series_val)
input_dropdown = alt.binding_select(options=series_val, name='Industry: ')
selection = alt.selection_single(fields=['series'], bind=input_dropdown)

fig3 = alt.Chart(source).mark_area().encode(
    alt.X('yearmonth(date):T',
        axis=alt.Axis(format='%Y', domain=False, tickSize=0)
    ),
    alt.Y('sum(count):Q', stack='center', axis=None),
    alt.Color('series:N',
        scale=alt.Scale(scheme='category20b')
    )
).add_selection(
    selection
).transform_filter(
    selection
)
fig3

内容的提问来源于stack exchange,提问作者E Fly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 06:54:01