From e59d2b1ae18fb4e8aed7e3b6dfd4e7d6ab1d2827 Mon Sep 17 00:00:00 2001 From: "Weng, Chia-Ling" Date: Wed, 11 Oct 2023 14:23:33 +0800 Subject: [PATCH] [Doc] Add Label Position Based on Condition Example --- doc/user_guide/marks/text.rst | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/user_guide/marks/text.rst b/doc/user_guide/marks/text.rst index d7acc400d..4bae2f8b5 100644 --- a/doc/user_guide/marks/text.rst +++ b/doc/user_guide/marks/text.rst @@ -163,9 +163,39 @@ You can also use ``text`` marks as labels for other marks and set offset (``dx`` bar + text +Labels Position Based on Condition +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +By default, text mark as labels in Altair are positioned above or to the right of the value. +However, when dealing with negative values, this default positioning can lead to label overlap with the bar. +To address this issue, you can set label positions via :ref:`expressions`. +Here's an example demonstrating how to do this: + +.. altair-plot:: + import altair as alt + import pandas as pd + + source = pd.DataFrame({ + "a": ["A", "B", "C"], + "b": [28, -5, 10] + }) + + bar = alt.Chart(source).mark_bar().encode( + y="a:N", + x=alt.X("b:Q").scale(domain=[-10, 35]) + ) + + text_conditioned = bar.mark_text( + align="left", + baseline="middle", + dx=alt.expr(alt.expr.if_(alt.datum.b >= 0, 10, -20)) + ).encode(text="b") + + bar + text_conditioned + + Scatter Plot with Text ^^^^^^^^^^^^^^^^^^^^^^ -Mapping a field to ``text`` channel of text mark sets the mark’s text value. For example, we can make a colored scatter plot with text marks showing the initial character of its origin, instead of ``point`` marks. +Mapping a field to ``text`` channel of text mark sets the mark's text value. For example, we can make a colored scatter plot with text marks showing the initial character of its origin, instead of ``point`` marks. .. altair-plot:: import altair as alt