Mostafa Bouzari
Mostafa Bouzari

Reputation: 10240

filter chart based on hovering over values in altair

I'm trying to filter my data using Mouse Hover.

so far i can hover over them but honestly don't know how when i hover over a value that belongs to 1st series all values in that Series would show up and the rest would be grey.

here's my code:

import pandas as pd
import altair as alt
from vega_datasets import data
anscombe = data.anscombe()

    single_nearest = alt.selection_single(on="mouseover",nearest=False,clear='mouseout')

alt.Chart(anscombe).mark_circle(size=100).encode(
        alt.X('X'),
        alt.Y('Y'),
        color=alt.condition(single_nearest, 'Series', alt.value('lightgray'))
        #tooltip= ['X', 'Y']
    ).transform_filter({'field': 'Series', 'oneOf': ['I', 'II','III','IV']}).add_selection(single_nearest)#.interactive()

it looks like this

enter image description here

to sum up when i hover over Orange( the second Series), only orange (Second Series) would show as Orange and others will be geryed out.

Upvotes: 2

Views: 948

Answers (2)

joelostblom
joelostblom

Reputation: 48964

Setting field=['Series'] will work with either multi or single select:

import pandas as pd
import altair as alt
from vega_datasets import data
anscombe = data.anscombe()


single_nearest = alt.selection_single(on="mouseover", fields=['Series'])

alt.Chart(anscombe).mark_circle(size=100).encode(
    alt.X('X'),
    alt.Y('Y'),
    color=alt.condition(single_nearest, 'Series', alt.value('lightgray'))
).transform_filter(
    {'field': 'Series', 'oneOf': ['I', 'II','III','IV']}
).add_selection(
    single_nearest
)

enter image description here

Upvotes: 3

Mostafa Bouzari
Mostafa Bouzari

Reputation: 10240

i had to use Multi select instead of single select

single_nearest = alt.selection_multi(on="mouseover",nearest=False,clear='mouseout',fields=['Series'])

alt.Chart(anscombe).mark_circle(size=100).encode(
        alt.X('X'),
        alt.Y('Y'),
        color=alt.condition(single_nearest, 'Series', alt.value('lightgray'))
        #tooltip= ['X', 'Y']
    ).transform_filter({'field': 'Series', 'oneOf': ['I', 'II','III','IV']}).add_selection(single_nearest)#.interactive()

Upvotes: 0

Related Questions