Following up on this question I have two additional options to implement:
- Set placement of label to be in middle of chart, regardless of bar height
- Format label to appear as part of string including parenthesis
My code currently looks like this:
df = pd.DataFrame({'name':['bar','foo'],
'presented_value':[2,20],
'coloring_value':[1,25]})
base = (alt.Chart(df, height=250, width=375).mark_bar()
.encode(
x='name',
y=alt.Y('presented_value', axis=alt.Axis(orient='right')),
color='name'
)
)
bars = base.mark_bar().encode(color=alt.condition(
alt.datum.presented_value > alt.datum.coloring_value,
alt.value('lightgreen'),
alt.value('darkred')
))
text_sub_brand = base.mark_text(
align='center', baseline='bottom',
dy=35, fontSize=24
).encode(
text='presented_value'
)
text_cluster = base.mark_text(
align='center', baseline='bottom',
dy=50, fontSize=16
).encode(
text='coloring_value'
).transform_calculate(label='"Cluster value: " + datum.coloring_value')
(bars + text_sub_brand + text_cluster).properties(width=700)
Regarding the placement I tried different parameters of the MarkDef
using the docs here but didn't find an option that allows placement relative to chart and not to bar.
As seen in the image above for foo
bar I would like to avoid cases where the label is presented outside the Y axis area.
Regarding the formatting I tried implementing the solution here but for some reason didn't work in my case.
Ideally I would like the format to be label='"(" + datum.coloring_value + ")"')
but using parenthesis caused a JavaScript error:
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
Can this be done? Thanks!