简体   繁体   中英

Altair add selection to Layered chart

I have the code below. But the 'selection' does not work as expected. When a point is selected, the other lines are deselected. But the points for the selected line also hide/disappear.

I must be doing something wrong. Is this the proper way to add a selection to a layered chart?

import altair as alt
from vega_datasets import data

source = data.stocks()

selection1 = alt.selection_single()

line = alt.Chart(source).mark_line().encode(
    x='date',
    y='price',
    #color= 'symbol',
    color=alt.condition(selection1, 'symbol', alt.value('grey')),
    opacity=alt.condition(selection1, alt.value(0.8), alt.value(0.1)),
)

point = line.mark_point(size = 40, fill='white')

alt.layer(line, point).add_selection(selection1)

By default, the selection selects only the data directly associated with the mark you click on. If you want it to apply to a larger set of data, you can specify fields or encodings . In your case, it sounds like you want it to apply to all data with the same symbol , so you can do this:

selection1 = alt.selection_single(fields=['symbol'])

or, since your symbol maps to color in all cases, equivalently you can do this:

selection1 = alt.selection_single(encodings=['color'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM