Search code examples
pythondata-visualizationvega-litealtair

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


The Altair example gallery contains a nice example of how to use interval selections to create two plots where one allows you to define the scale of the other. I have been trying to add tooltips to both parts of the stacked chart by defining the tooltips as part of the base:

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

Doing so, the tooltips work as expected on lower but not at all on upper.

enter image description here

If, however, I remove the .add_selection(brush) from lower, the tooltips also work on upper (which was unchanged), but that of course defeats the purpose of the example. I can also make the tooltips work on upper by marking it as interactive, but again, that ruins what's nice about the example. Changing the definition of upper to upper = base.encode(tooltip='price:Q') does nothing.

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


Solution

  • The fact that the tooltips stop working is probably a bug, and it would be worth filing a Vega-Lite bug report

    It appears you can work around this by adding a second empty selection to the upper chart:

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

    You can view the interactive result here.