微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击图表时在 Altair 交互式图例中保留多选

如何解决单击图表时在 Altair 交互式图例中保留多选

我在 altair 中创建了一个带有交互式图例的复合条形图,其中可以选择一个图表中的条形和图例元素来过滤另一个图表。当仅选择单个图例元素时,这很有效。 However,when multiple legend elements are selected,selecting a bar alters the legend selection.

请参阅下面的最小示例。 In this image,选择了 Crookston 和 Duluth 站点,结果是按年的产量总和图表过滤到这两个站点。但是,如果我然后单击品种图表产量总和中的 Manchuria 条,图例选择会发生变化,因此只选择了德卢斯,如图 in this image。 (如果我改为按住 Shift 键并单击 Manchuria 栏,则图例中只会选择 Crookston。)期望的结果是 Crookston 和 Duluth 在图例中保持选中状态。

是否可以防止点击图表时图例的选择发生变化?

import altair as alt
from vega_datasets import data

source = data.barley()

bar_selection = alt.selection_multi(encodings=["y"])
legend_selection = alt.selection_multi(encodings=["color"],bind="legend")

varieties = alt.Chart(source).mark_bar().encode(
    x="sum(yield)",y="variety",color="site",opacity=alt.condition(
        bar_selection,alt.value(1.0),alt.value(0.25)
    ),).add_selection(bar_selection)

years = alt.Chart(source).mark_bar().encode(
    x="sum(yield)",y="year:N",).transform_filter(
    bar_selection
).transform_filter(
    legend_selection
)

(varieties & years).add_selection(legend_selection)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。