Is there a way to word wrap or insert a line break with Altair labels?
Asked Answered
P

3

5

Is there a way to insert a line break when displaying longer labels in Altair?

d = {'source': ['short label', 'a longer label that needs a line break', 'short label', 'a longer label that needs a line break'], 'variable': ['begin','begin','end','end'], 'value':[75, 25, 20, 80]}
df = pd.DataFrame(data=d)

slope = alt.Chart(df).mark_line().encode(x='variable:N',y=alt.Y('value:Q',scale=alt.Scale(domain=(0,100))),color='source')
text = slope.mark_text(align='left',baseline='middle').encode(text='source:N')
(slope + text).properties(width=200)`

I see a list of arrays in an option for a title but I haven't been able to get that to work for labels.

Platy answered 4/6, 2020 at 21:55 Comment(1)
So I guess there is not a solution for this?Vlaminck
E
5

For this particular example, you could add a new line character to the label that needs the new line: 'a longer label that \nneeds a line break' and then change the parameters inside mark_text to include lineBreak=r'\n'

d = {'source': ['short label', 'a longer label that \nneeds a line break', 'short label', 'a longer label that \nneeds a line break'], 'variable': ['begin','begin','end','end'], 'value':[75, 25, 20, 80]}
df = pd.DataFrame(data=d)

slope = alt.Chart(df).mark_line().encode(x='variable:N',y=alt.Y('value:Q',scale=alt.Scale(domain=(0,100))),color='source')
text = slope.mark_text(align='left',baseline='middle', lineBreak=r'\n').encode(text='source:N')
(slope + text).properties(width=500)

enter image description here

Essive answered 23/10, 2020 at 1:25 Comment(0)
J
2

I had issues with getting lineBreak to work... but as pointed out in https://github.com/altair-viz/altair/issues/2376 you can also just pass a list of strings and they will be printed on separate lines.

(That's an hour of my life I'll never get back :P - hopefully this comment saves someone else)

Janettejaneva answered 15/11, 2022 at 7:30 Comment(0)
P
0

I was able to find a workaround that worked for my purpose. I tweaked the dataframe to break up the longer label into two and used the dx/dy adjustment to move them into place.

d = {'source1': ['short label', 'a longer label that', 'short label', 'a longer label that']
,'source2': ['', 'needs a line break', '', 'needs a line break']
,'variable': ['begin','begin','end','end']
,'value':[75, 25, 20, 80]}

df = pd.DataFrame(data=d)

print(df)

               source1             source2 variable  value
0          short label                        begin     75
1  a longer label that  needs a line break    begin     25
2          short label                          end     20
3  a longer label that  needs a line break      end     80



slope = alt.Chart(df).mark_line().encode(x='variable:N',y=alt.Y('value:Q',scale=
        alt.Scale(domain=(0,100)),axis=alt.Axis(grid=False)),
        color=alt.Color('source1',legend=None))
text1 = slope.mark_text(align='left',baseline='middle', dx=10).encode(text='source1:N')
text2 = slope.mark_text(align='left',baseline='middle', dx=10, dy=10).encode(text='source2:N')

(slope + text1 + text2).properties(width=500)

Text

Platy answered 18/7, 2020 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.