How can I put a line break in a Vega-lite axis title?

笑着哭i 提交于 2020-12-09 06:03:18

问题


I'm new to Vega-lite and I'm trying to figure out if line breaks are possible in axis titles. I have a long axis titles, e.g.:

"Long axis title is too long to fit under the graph"

I've tried:

"Long axis title is too\n long to fit under the graph" and
"Long axis title is too\
long to fit under the graph"

The "\n" doesn't seem to do anything. The "\[enter]" just adds extra space to the line.

My x and y encoding looks like this:

encoding: {
      x: {field: 'a',
          type: 'ordinal',
          sort: {"encoding": "x"},  
          axis: {"title": "Knowledge of the elder\
          categories would melt\
          your psyche",
          "titleFontSize": 30,
          }
          },
      y: {field: 'b', 
          type: 'quantitative',
          axis: {"title": "Your puny mortal mind\ncannot comprehend the units\nof the multiverse!",
          "titleFontSize": 14,
          }
          }
    }

I'm not getting error messages, but I'm not getting line breaks either. I either get no change (from \n), or weird spacing (from [enter]).

Thank you!


回答1:


In Vega-Lite 4.0 or newer, multiline text can be specified in titles by passing an array of strings. For example:

{"data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91},
      {"a": "E", "b": 81},
      {"a": "F", "b": 53},
      {"a": "G", "b": 19},
      {"a": "H", "b": 87},
      {"a": "I", "b": 52}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "a",
      "type": "ordinal",
      "title": ["First line of title", "second line of title"]
    },
    "y": {"field": "b", "type": "quantitative"}
  }
}



来源:https://stackoverflow.com/questions/57116167/how-can-i-put-a-line-break-in-a-vega-lite-axis-title

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!