多行高亮#

此多行图表使用不可见的 Voronoi 细分来处理鼠标悬停,以识别最近的点,然后高亮该点所在的线条。它改编自 Vega-Lite 的示例,可以在以下链接找到 https://bl.ocks.org/amitkaps/fe4238e716db53930b2f1a70d3401701

import altair as alt
from vega_datasets import data

source = data.stocks()

highlight = alt.selection_point(on='pointerover', fields=['symbol'], nearest=True)

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
)

points = base.mark_circle().encode(
    opacity=alt.value(0)
).add_params(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.when(~highlight).then(alt.value(1)).otherwise(alt.value(3))
)

points + lines
import altair as alt
from vega_datasets import data

source = data.stocks()

highlight = alt.selection_point(on='pointerover',
                          fields=['symbol'], nearest=True)

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
)

points = base.mark_circle().encode(
    opacity=alt.value(0)
).add_params(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.when(~highlight).then(alt.value(1)).otherwise(alt.value(3))
)

points + lines