-
Notifications
You must be signed in to change notification settings - Fork 795
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
Is it possible to use alt.condition when passing options to text encodings? #1055
Comments
conditions are only supported for encodings, unfortunately. |
FYI: for the power user, often the best way to answer "is this possible" questions is to look at the vega-lite schema. For example, here it's clear that |
Thanks for the tip. Too bad Vega doesn't support this. In a perfect world such a feature would be nice. Short of that, is the pattern I developed above the one you would recommend. If so, perhaps it's worth enshrining it in an example? |
FYI, we do plan to support xOffset/yOffset as encoding channels in the future, but doing so require some design in terms of how the x/yOffset channels interact with x/y channels, so that's why it doesn't happen yet. Also, maybe reading the type descriptions in Vega-Lite docs is less painful than reading the JSON schema, which is more like a format for machine to read IMHO. |
Once the offset encoding is available, this will be easier. I think that should be in Altair once #2528 is merged and released. |
@qunderriner A workaround for this issue is to create text labels in all the positions where you want them for all the bars and then encode their opacity with a condition as in this SO answer. Another workaround would be to create a custom dataframe with the y-values in the right positions for where you want the text to be for each bar. Btw, I don't think the offset functionality is part of a Vega-Lite release yet, so it might only come to Altair after #2528 |
@joelostblom That worked perfectly - thanks so much! |
@palewire @qunderriner This is now possible to do via expressions https://altair-viz.github.io/user_guide/interactions.html#expressions. Using the same setup as above: text = base.encode(
x=alt.X("date:O", timeUnit="yearmonth"),
y="pct_change_rounded:Q",
text='pct_change_rounded',
).mark_text(
fontSize=14,
dy=alt.expr(alt.expr.if_(alt.datum.pct_change_rounded >= 0, -10, 10))
)
bars + text I think it would be helpful to have an example of this in the docs, maybe under the text mark label section https://altair-viz.github.io/user_guide/marks/text.html#labels? Happy to review a PR if someone wants to take that on. Another approach would be to use the yOffset channel like this |
Here's the chart I want to make. You can see that it has text placed above positive bars and below negative bars.
Here's the data I used.
Here's the base chart I wrote.
And here's how I handled the text.
It all works fine. But why can't I do this?
I get this error:
The text was updated successfully, but these errors were encountered: