I'm looking to customize the tooltips in my Altair chart. I have two specific requirements:
Order the tooltip lines based on the value of the element, and hide certain tooltip lines depending on their value.
Here's an example:
import altair as alt
from vega_datasets import data
source = data.stocks()
base = alt.Chart(source).encode(x='date:T')
columns = sorted(source.symbol.unique())
selection = alt.selection_point(fields=['date'], nearest=True, on='mouseover', empty=False, clear='mouseout')
lines = base.mark_line().encode(y='price:Q', color='symbol:N')
points = lines.mark_point().transform_filter(selection)
rule = base.transform_pivot('symbol', value='price', groupby=['date']).mark_rule().encode(
opacity=alt.condition(selection, alt.value(0.3), alt.value(0)),
tooltip=[alt.Tooltip('date', type='temporal', format = "%b %Y")] + [alt.Tooltip(c, type='quantitative') for c in columns]
).add_params(selection)
lines + points + rule
I'd like to order the tooltip lines based on the X value, keeping the date's line first. I also aim to exclude the line representing Google's values in the tooltip due to the fact that Google's value is NaN. Furthermore, I'd like the tooltip to resemble the following example:
Date: July 2003 IBM: 74.28 AMZN: 41.64 MSFT: 21.56 AAPL: 10.54
Is there a way to achieve this? Any tips would be greatly appreciated!
