选择 zorder#

本示例演示了如何通过使用条件来改变被鼠标指针悬停的点的(z)顺序,从而将选定的点置于前面/前景。这可以防止被选定的点被未选定的点遮挡。

import altair as alt
from vega_datasets import data


cars = data.cars.url

hover = alt.selection_point(on='pointerover', nearest=True, empty=False)
when_hover = alt.when(hover)

chart = alt.Chart(cars, title='Selection obscured by other points').mark_circle(opacity=1).encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=when_hover.then(alt.value("coral")).otherwise(alt.value("lightgray")),
    size=when_hover.then(alt.value(300)).otherwise(alt.value(30))
).add_params(
    hover
)

chart | chart.encode(
    order=when_hover.then(alt.value(1)).otherwise(alt.value(0))
).properties(
    title='Selection brought to front'
)
import altair as alt
from vega_datasets import data


cars = data.cars.url

hover = alt.selection_point(on='pointerover', nearest=True, empty=False)
when_hover = alt.when(hover)

chart = alt.Chart(cars, title='Selection obscured by other points').mark_circle(opacity=1).encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=when_hover.then(alt.value("coral")).otherwise(alt.value("lightgray")),
    size=when_hover.then(alt.value(300)).otherwise(alt.value(30))
).add_params(
    hover
)

chart | chart.encode(
    order=when_hover.then(alt.value(1)).otherwise(alt.value(0))
).properties(
    title='Selection brought to front'
)
# No channel encoding options are specified in this chart
# so the code is the same as for the method-based syntax.