Tooltip example doesn't work with data aggregate transforms
See original GitHub issueI was trying to add a scatterplot tooltip based on the Multi-Line Tooltip example and think I ran across a bug. The example works fine when data are plotted directly but exhibits weird behavior when an aggregation function is applied.
import altair as alt
import numpy as np
import pandas as pd
# Create data and labels
a = np.random.randint(-5, 5, 200)
b = a * np.random.random(200)
s = np.random.choice(['first', 'second', 'third', 'fourth'], 200)
data = pd.DataFrame.from_dict({'a': a, 'b': b, 's': s}, orient='columns')
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection_single(name='nearest', nearest=True, on='mouseover', empty='none')
# Plot the mean of a against the mean of b
points = alt.Chart().mark_point(filled=True, size=100).encode(
x='mean(a):Q',
y='mean(b):Q',
color='s:N'
)
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart().mark_point().encode(
x='mean(a):Q',
y='mean(b):Q',
opacity=alt.value(0),
).properties(
selection=nearest
)
# Draw text labels near the points, and highlight based on selection
text = points.mark_text(align='left', dx=5, dy=-5).encode(
text=alt.condition(nearest, 's:N', alt.value(' '))
)
alt.layer(points, selectors, text, data=data)
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How do I compute aggregates of aggregates for labels/tooltips?
The way to do this is with aggregate transforms, though it's a little bit tricky because you need to make sure to group...
Read more >Power BI - Problem, Design, Solution - Concatenated Tooltip
This happens because the Tooltip field requires that any column used in it will be able to aggregate or roll up the values...
Read more >Tooltip | Vega-Lite
Tooltips can provide details of a particular data point on demand. Tooltips for each single-view in Vega-Lite can be (1) generated based on...
Read more >Customizing tooltips in Power BI Desktop - Microsoft Learn
You can further customize a tooltip by selecting an aggregation function. Select the arrow beside the field in the Tooltips bucket. Then, select ......
Read more >Tableau Tooltips - Full beginners guide including Viz in Tooltips.
Tooltips are additional data details that display when you hover over one or more marks in the view. When you select one or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If your goal is to show tooltips, you could use the
tooltip
encoding channel. It’s not fully operational yet (there is some current work in the renderers that will make the tooltip implementation much better in the near future) but it works using the HTMLtitle
attribute now. For example:The whole thing about using selections and layers should only be used if you want to do something more sophisticated.
Additional documentation for anyone who comes across this.
From vega-lite documentation it looks like the transparent selectors are used because the
nearest
transform isn’t supported for continuous mark types (lines and area).