Search code examples
word-wrapvega-lite

Vega-lite: Line break or word wrap for labels in axis


I was trying to wrap the labels of x-axis on next line as the labels length is quite long. So is there any way to achieve it. I tried using calculate and labelExpr by breaking the label string and then concatenating them using '\n' but that doesn't seem to work.

Is there any other config which will help me achieve this use case or any workaround ? You can refer the config here - vega-lite.


Solution

  • Newline characters in strings cannot be used to display line breaks. You can generate a line break in most Vega-Lite strings by instead passing a list of strings.

    For your chart, you can generate line breaks using the split() operation in a calculate transform:

      "transform": [{"calculate": "split(datum.address, ' ')", "as": "address"}],
    

    The result looks like this (view in editor):

    enter image description here