单击一个州以在 Altair 图表中显示其县
Clicking a state to reveal its counties in Altair chart
我希望能够单击一个州(单个或多个 selection)并显示 selected 州的县,同时仍继续显示非 select编辑状态。我已经能够通过构建一个数据框来实现这一点,在该数据框中我将州形状的 geojson 文件附加到县形状的 geojson 中。
census_area centroid_lat centroid_lon county_fips fips geometry geomlist id iso_3166_2 lsad name state state_fips state_only_fips
state_fips 存在于每个元组中,其中 state_only_fips 仅存在于状态形状中。然后我使用以下代码构建我的地图:
state_selection = alt.selection_single(empty='none', fields=['properties.state_only_fips'])
alt.Chart(combined_geo).mark_geoshape(stroke='black').encode(
).add_selection(
state_selection
).transform_filter(
{'not': state_selection}
).properties(
width=900,
height=700
).project("albersUsa")
这正确地隐藏了州的形状并显示了下面的县,但效果不佳。我不想渲染所有 和 州的形状并仅隐藏 I select 的州形状,我希望过程看起来像这样:
初始化图表,仅显示状态形状(表示为具有 state_only_fips 值,或值 'lsad' == 'State')
如果单击某个状态,隐藏 selected 状态,同时继续显示所有其他状态
显示 selected 州的县形状。
Display Selected State's Counties
我觉得这是可能的,并且会使图表表现得更好,但我不确定如何构建 geojson 文件 and/or 我的 transform_filter
我终于能够通过有 2 个选择来实现这一点:1 个用于检查选定的州,一个用于检查选定州的县。为了显示状态,我只显示 分类为 'State' 且尚未被点击的形状。选择在 state_only_fips 上,它仅适用于状态形状。
state_selection = alt.selection_multi(empty='none', fields=['properties.state_only_fips'], clear='dblclick')
{'and' : [('datum[\'properties.lsad\'] == \'State\''), {'not': state_selection}]}
我在 state_fips 上添加了第二个选择,它包含在每个形状中。在状态形状中,这与 state_only_fips 具有相同的值。第二个过滤器还过滤掉所有未分类为 'State'.
的形状
county_selection = alt.selection_multi(empty='none', fields=['properties.state_fips'], clear='dblclick')
{'and' : [('datum[\'properties.lsad\'] !== \'State\''), county_selection]}
最后,我们将这两个语句与 or 谓词结合起来。最终产品如下所示:
county_selection = alt.selection_multi(empty='none', fields=['properties.state_fips'], clear='dblclick')
state_selection = alt.selection_multi(empty='none', fields=['properties.state_only_fips'], clear='dblclick')
base_chart = alt.Chart().mark_geoshape(stroke='black').encode(
).add_selection(
state_selection,
county_selection
).transform_filter(
{'or': [{'and' : [('datum[\'properties.lsad\'] == \'State\''), {'not': state_selection}]}
,{'and' : [('datum[\'properties.lsad\'] !== \'State\''), county_selection]}]}
).properties(
width=900,
height=700
).project("albersUsa")
这可能可以以更有效的方式完成,但它比以前更好地工作负载。
我希望能够单击一个州(单个或多个 selection)并显示 selected 州的县,同时仍继续显示非 select编辑状态。我已经能够通过构建一个数据框来实现这一点,在该数据框中我将州形状的 geojson 文件附加到县形状的 geojson 中。
census_area centroid_lat centroid_lon county_fips fips geometry geomlist id iso_3166_2 lsad name state state_fips state_only_fips
state_fips 存在于每个元组中,其中 state_only_fips 仅存在于状态形状中。然后我使用以下代码构建我的地图:
state_selection = alt.selection_single(empty='none', fields=['properties.state_only_fips'])
alt.Chart(combined_geo).mark_geoshape(stroke='black').encode(
).add_selection(
state_selection
).transform_filter(
{'not': state_selection}
).properties(
width=900,
height=700
).project("albersUsa")
这正确地隐藏了州的形状并显示了下面的县,但效果不佳。我不想渲染所有 和 州的形状并仅隐藏 I select 的州形状,我希望过程看起来像这样:
初始化图表,仅显示状态形状(表示为具有 state_only_fips 值,或值 'lsad' == 'State')
如果单击某个状态,隐藏 selected 状态,同时继续显示所有其他状态
显示 selected 州的县形状。 Display Selected State's Counties
我觉得这是可能的,并且会使图表表现得更好,但我不确定如何构建 geojson 文件 and/or 我的 transform_filter
我终于能够通过有 2 个选择来实现这一点:1 个用于检查选定的州,一个用于检查选定州的县。为了显示状态,我只显示 分类为 'State' 且尚未被点击的形状。选择在 state_only_fips 上,它仅适用于状态形状。
state_selection = alt.selection_multi(empty='none', fields=['properties.state_only_fips'], clear='dblclick')
{'and' : [('datum[\'properties.lsad\'] == \'State\''), {'not': state_selection}]}
我在 state_fips 上添加了第二个选择,它包含在每个形状中。在状态形状中,这与 state_only_fips 具有相同的值。第二个过滤器还过滤掉所有未分类为 'State'.
county_selection = alt.selection_multi(empty='none', fields=['properties.state_fips'], clear='dblclick')
{'and' : [('datum[\'properties.lsad\'] !== \'State\''), county_selection]}
最后,我们将这两个语句与 or 谓词结合起来。最终产品如下所示:
county_selection = alt.selection_multi(empty='none', fields=['properties.state_fips'], clear='dblclick')
state_selection = alt.selection_multi(empty='none', fields=['properties.state_only_fips'], clear='dblclick')
base_chart = alt.Chart().mark_geoshape(stroke='black').encode(
).add_selection(
state_selection,
county_selection
).transform_filter(
{'or': [{'and' : [('datum[\'properties.lsad\'] == \'State\''), {'not': state_selection}]}
,{'and' : [('datum[\'properties.lsad\'] !== \'State\''), county_selection]}]}
).properties(
width=900,
height=700
).project("albersUsa")
这可能可以以更有效的方式完成,但它比以前更好地工作负载。