Vega-Lite中可视化周围的间距

时间:2019-03-12 19:02:50

标签: vega-lite

我知道如何控制可视化中元素的间距,并且知道如何控制可视化的宽度和高度。但是,是否可以控制可视化与其他元素(例如标题或图例)之间的间距?

1 个答案:

答案 0 :(得分:0)

要自定义标题,可以使用对象而不是字符串。您可以在https://vega.github.io/vega-lite/docs/title.html上了解有关文档中可用选项的更多信息。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "description": "A simple bar chart with embedded data.",
  "title": {
    "text": "This is an awesome Chart!",
    "offset": 20
  },
  "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"},
    "y": {"field": "b", "type": "quantitative"}
  }
}

如果要使图表更紧凑,也可以使用负偏移量。

enter image description here

您可以类似地自定义图例。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v3.json",
  "data": {
    "url": "data/cars.json"
  },
  "mark": "point",
  "encoding": {
    "x": {
      "field": "Horsepower",
      "type": "quantitative"
    },
    "y": {
      "field": "Miles_per_Gallon",
      "type": "quantitative"
    },
    "color": {
      "field": "Origin",
      "type": "nominal",
      "legend": {
        "labelOffset": 30,
        "titleAnchor": "end",
        "offset": 30
      }
    }
  }
}

enter image description here