2

Just like we can highlight point(s) in one of the charts of a concatenated chart or a faceted chart and the corresponding point(s) will also get highlighted in the other chart, I was wondering if the same can be done with a tooltip.

I have been able to come up with a demo using mark_text as you can see below. But the biggest challenge is not being able to show multiple encodings as text. Tooltips make that really easy by just mentioning all the encodings in a list. So I was thinking if there is a way to do that in Altair.

Or is this not the expected behavior of Tooltips, and they are ONLY supposed to highlight the point over which the mouse is hovering even if other charts may have a common selection?

Whatever I have tried with Tooltips only works in a single chart, the other point despite being highlighted does not also show a tooltip.

Code:

import altair as alt
from vega_datasets import data

source = data.cars()

highlight = alt.selection(type='single', on="mouseover", empty='none')

base = alt.Chart(source).encode(
    y='Miles_per_Gallon',
    color=alt.condition(highlight, 'Origin', alt.ColorValue('gray')),
    
)

mpg = base.mark_point().encode(
   x='Horsepower',
   size=alt.condition(highlight, alt.value(150), alt.value(50))).add_selection(
    highlight
)
acc = base.mark_point().encode(
   x='Acceleration',
   size=alt.condition(highlight, alt.value(150), alt.value(50))).add_selection(
    highlight
)

text_mpg = base.mark_text(dx=5, dy=-10, size=15).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    text=alt.condition(highlight, 'Horsepower:Q', alt.value('')),
)
text_acc = base.mark_text(dx=5, dy=-10, size=15).encode(
    x='Acceleration',
    y='Miles_per_Gallon',
    text=alt.condition(highlight, 'Acceleration:Q', alt.value('')),
)

(mpg+text_mpg)|(acc+text_acc)

Demo: enter image description here

Expected Output:
Show tooltips instead of text, with multiple encodings

I am however starting to feel that this is not the intended behavior of tooltip.

jar
  • 2,646
  • 1
  • 22
  • 47

0 Answers0