-
Notifications
You must be signed in to change notification settings - Fork 793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not possible to view faceted charts with altair.selection
after #2702
#2713
Comments
Thanks for raising. This seems to be an issue in VL. Let me try to decipher: In vega-lite this works: Open the Chart in the Vega Editor: {
"data": {
"values": [
{"a": 1, "b": 1, "c": 1, "d": 1},
{"a": 2, "b": 2, "c": 1, "d": 2},
{"a": 3, "b": 1, "c": 2, "d": 3},
{"a": 4, "b": 2, "c": 2, "d": 4}
]
},
"facet": {"field": "c", "type": "nominal"},
"spec": {
"params": [
{
"name": "selected",
"select": {"type": "point", "fields": ["d"]},
"bind": "legend"
}
],
"mark": {"type": "line", "point": true},
"encoding": {
"color": {"field": "d", "type": "nominal"},
"x": {"field": "a", "type": "quantitative"},
"y": {"field": "b", "type": "quantitative"},
"opacity": {"condition": {"value": 1, "param": "selected"}, "value": 0.25}
}
},
"$schema": "https://vega.github.io/schema/vega-lite/v5.2.0.json"
} But when defining {
"data": {
"values": [
{"a": 1, "b": 1, "c": 1, "d": 1},
{"a": 2, "b": 2, "c": 1, "d": 2},
{"a": 3, "b": 1, "c": 2, "d": 3},
{"a": 4, "b": 2, "c": 2, "d": 4}
]
},
"facet": {"field": "c", "type": "nominal"},
"params": [
{
"name": "selected",
"select": {"type": "point", "fields": ["d"]},
"bind": "legend",
"views": ["view_1"]
}
],
"spec": {
"name": "view_1",
"mark": {"type": "line", "point": true},
"encoding": {
"color": {"field": "d", "type": "nominal"},
"x": {"field": "a", "type": "quantitative"},
"y": {"field": "b", "type": "quantitative"},
"opacity": {"condition": {"value": 1, "param": "selected"}, "value": 0.25}
}
},
"$schema": "https://vega.github.io/schema/vega-lite/v5.2.0.json"
} Resulting in the error as OP:
And it is the latter approach that Altair is currenlty adopting.. You see any strange things here @ChristopherDavisUCI? A temporal workaround for you @wence- might be to move the selector = alt.selection(type="point", fields=["d"], bind="legend", name="selected")
chart = (
alt.Chart(df)
.mark_line(point=True)
.encode(
x=alt.X("a:Q"),
y=alt.Y("b:Q"),
color="d:N",
opacity=alt.condition(selector, alt.value(1), alt.value(0.25)),
facet="c:N"
)
)
chart.add_params(selector) not sure if that works for your situation. |
Thanks, that works for this simple case, but not for my actual use case which is faceting a layered chart ( |
Yes, then the suggestion will not work. import altair as alt
from vega_datasets import data
cars = data.cars.url
highlight = alt.selection_point(fields=["Origin"], bind='legend', value='Europe')
base = alt.Chart(cars, width=160, height=130).encode(
x='Miles_per_Gallon:Q',
y='Horsepower:Q'
)
text = base.mark_text().encode(
text='Origin:N',
color=alt.condition(highlight, alt.value("black"), alt.value("transparent"))
)
dots = base.mark_circle().encode(
color=alt.condition(highlight, 'Origin:N', alt.value('lightgray')),
size=alt.condition(highlight, alt.value(250), alt.value(20))
)
(dots + text).facet('Origin:N').add_params(highlight) |
Oh wait, this is because you have defined Apparently when separating this in a import pandas as pd
import altair as alt
df = pd.DataFrame(
{"a": [1, 2, 3, 4], "b": [1, 2, 1, 2], "c": [1, 1, 2, 2], "d": [1, 2, 3, 4]}
)
selector = alt.selection(type="point", fields=["d"], bind="legend", name="selected")
base = alt.Chart(df).encode(
x=alt.X("a:Q"),
y=alt.Y("b:Q"),
color="d:N",
opacity=alt.condition(selector, alt.value(1), alt.value(0.25)),
)
chart = (
(base.mark_line() + base.mark_circle())
.facet(facet=alt.Text("c:N"))
.add_params(selector)
) |
Was raised previously at VL here: vega/vega-lite#7854 |
With the second release candidate of version 5 ( |
Since the merge of #2702, faceted charts with a selection (which used to work, I may have been doing something wrong), now result in an error:
Code to reproduce:
The text was updated successfully, but these errors were encountered: