什么时候在 Layer 和 Top-Level Vega-lite 规范中嵌套标记 属性?

When to nest mark property in Layer versus Top-Level Vega-lite spec?

我想知道 Vega-lite 如何将标记绑定到相关编码。

在下面的例子中,编码和标记都在规范的“顶层”:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "A simple bar chart with embedded data.",
  "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": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative"}
  }
}

以最简单的层为例,条形标记和文本标记都嵌套在层中属性

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Bar chart with text labels. Apply scale padding to make the frame cover the labels.",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43}
    ]
  },
  "encoding": {
    "y": {"field": "a", "type": "nominal"},
    "x": {"field": "b", "type": "quantitative", "scale": {"padding": 10}}
  },
  "layer": [{
    "mark": "bar"
  }, {
    "mark": {
      "type": "text",
      "align": "left",
      "baseline": "middle",
      "dx": 3
    },
    "encoding": {
      "text": {"field": "b", "type": "quantitative"}
    }
  }]
}

Vega-lite 文档详细介绍了这些属性的配置,但我无法找到这 3 个问题的概念性答案。

谢谢

Vega-Lite 提供了层次结构图表模型,其中层次结构中的每个级别都可以覆盖父级别中声明的各种属性。在层规范方面,相关概念是这样的:

  • a UnitSpec就是你认为的单图:它它,你可以指定datamarkencodingstransforms , 和其他属性。
  • 一个LayerSpec,是一个容器,可以容纳layers属性中的多个UnitSpecLayerSpec规格。此外,您可以指定 dataencodings transforms 和其他属性(但不能指定 mark)。

LayerSpec 或其他 top-level 对象中的 UnitSpec 将继承那里指定的任何属性(例如 dataencodingstransforms, 等), 并且还可以通过指定自己的 data, encodings, 或 transforms.

来覆盖它们

类似的分层概念适用于其他复合图表类型,例如 ConcatSpecVConcatSpecHConcatSpecFacetSpec

更具体地说,在您的示例中,data 和一些 encodings 定义在 top-level 层中:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Bar chart with text labels. Apply scale padding to make the frame cover the labels.",
  "data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43}
    ]
  },
  "encoding": {
    "y": {"field": "a", "type": "nominal"},
    "x": {"field": "b", "type": "quantitative", "scale": {"padding": 10}}
  },
  "layer": [{
    "mark": "bar"
  }, {
    "mark": {
      "type": "text",
      "align": "left",
      "baseline": "middle",
      "dx": 3
    },
    "encoding": {
      "text": {"field": "b", "type": "quantitative"}
    }
  }]
}

就父类的继承而言,这在功能上等同于以下内容,其中我已将 dataencodings 从 top-level 移动到每个包含的 UnitSpec:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Bar chart with text labels. Apply scale padding to make the frame cover the labels.",
  "layer": [{
    "data": {
      "values": [
        {"a": "A", "b": 28},
        {"a": "B", "b": 55},
        {"a": "C", "b": 43}
      ]
    },
    "mark": "bar"
    "encoding": {
      "y": {"field": "a", "type": "nominal"},
      "x": {"field": "b", "type": "quantitative", "scale": {"padding": 10}}
    },
  }, {
    "data": {
      "values": [
        {"a": "A", "b": 28},
        {"a": "B", "b": 55},
        {"a": "C", "b": 43}
      ]
    },
    "mark": {
      "type": "text",
      "align": "left",
      "baseline": "middle",
      "dx": 3
    },
    "encoding": {
      "y": {"field": "a", "type": "nominal"},
      "x": {"field": "b", "type": "quantitative", "scale": {"padding": 10}}
      "text": {"field": "b", "type": "quantitative"}
    }
  }]
}

在顶层指定共享属性是一种使图表规范更加简洁易懂的方法。