I wanted to highlight the part of a map where mouse is hovering (or nearest to) - just like a tooltip behaves - by using selection_single
. But it always remains fixed at a seemingly random point.
For example, here the county region should be highlighted red when mouse is on top of it.
import altair as alt
from vega_datasets import data
counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url
highlight = alt.selection_single(on='mouseover', nearest=True, fields=['id'], empty='none')
alt.Chart(counties).mark_geoshape().encode(
color=alt.condition(highlight, alt.value('red'), 'rate:Q'),
tooltip=['id:Q', 'rate:Q']
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).add_selection(highlight).project(
type='albersUsa'
).properties(
width=900,
height=600
)
But wherever my mouse goes, both the highlight AS WELL AS THE TOOLTIP remain fixed at
id
= 22051 (look at bottom in Lousiana). When the selection is not added the tooltip works fine, but after adding the selection, even it fails to work correctly.
Will voronoi tesselation help? Like the nearest line example? I tried, it failed, but I am not sure that I applied it correctly.
It works if you remove nearest=True
:
import altair as alt
from vega_datasets import data
counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url
highlight = alt.selection_single(on='mouseover', fields=['id'], empty='none')
alt.Chart(counties).mark_geoshape().encode(
color=alt.condition(highlight, alt.value('red'), 'rate:Q'),
tooltip=['id:Q', 'rate:Q']
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).add_selection(highlight).project(
type='albersUsa'
).properties(
width=900,
height=600
)