使用间隔选择时,堆积 Altair/Vega-Lite 图表中的工具提示消失

Tooltips in stacked Altair/Vega-Lite chart disappear when using interval selections

Altair 示例库包含 a nice example 如何使用区间选择创建两个图,其中一个允许您定义另一个的比例。我一直在尝试通过将工具提示定义为基础的一部分来将工具提示添加到堆叠图表的两个部分:

import altair as alt
from vega_datasets import data

source = data.sp500.url

brush = alt.selection(type='interval', encodings=['x'])

base = alt.Chart(source).mark_area().encode(
    x = 'date:T',
    y = 'price:Q',
    tooltip = 'price:Q'
).properties(
    width=600,
    height=200
)

upper = base

lower = base.properties(
    height=60
).add_selection(brush)

upper & lower

这样做,工具提示在 lower 上按预期工作,但在 upper 上完全不工作。

但是,如果我从 lower 中删除 .add_selection(brush),工具提示也适用于 upper(未更改),但这当然违背了示例的目的.我还可以通过将工具提示标记为交互式来使工具提示在 upper 上工作,但同样,这会破坏该示例的优点。将 upper 的定义更改为 upper = base.encode(tooltip='price:Q') 没有任何作用。

How would I define the tooltip target in a way that makes tooltips show up on both upper and lower?

工具提示停止工作的事实可能是一个错误,值得提交 Vega-Lite bug report

看来您可以通过在上面的图表中添加第二个空选择来解决此问题:

upper = base.add_selection(alt.selection_single())

您可以查看互动结果here