什么时候在 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"}
}
}]
}
在这种情况下,我假设图层 属性 中的任何标记自动继承顶层的编码是否正确?
此外,我注意到我无法将 bar Mark 移动到 Layer 属性 之外(Vega Editor 提示这是不允许的 属性 并且 bars 无法渲染如果放在顶层)。
最后,在更复杂的示例中(参见:https://vega.github.io/vega-lite/examples/layer_line_mean_point_raw.html),编码在层中重复(尽管有冗余 x 编码)-> 所以在这种情况下,当将编码置于顶层与层中是否合适?
Vega-lite 文档详细介绍了这些属性的配置,但我无法找到这 3 个问题的概念性答案。
谢谢
Vega-Lite 提供了层次结构图表模型,其中层次结构中的每个级别都可以覆盖父级别中声明的各种属性。在层规范方面,相关概念是这样的:
- a
UnitSpec
就是你认为的单图:它它,你可以指定data
、mark
、encodings
、transforms
, 和其他属性。
- 一个
LayerSpec
,是一个容器,可以容纳layers
属性中的多个UnitSpec
或LayerSpec
规格。此外,您可以指定 data
、encodings
transforms
和其他属性(但不能指定 mark
)。
LayerSpec
或其他 top-level 对象中的 UnitSpec
将继承那里指定的任何属性(例如 data
、encodings
、transforms
, 等), 并且还可以通过指定自己的 data
, encodings
, 或 transforms
.
来覆盖它们
类似的分层概念适用于其他复合图表类型,例如 ConcatSpec
、VConcatSpec
、HConcatSpec
、FacetSpec
等
更具体地说,在您的示例中,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"}
}
}]
}
就父类的继承而言,这在功能上等同于以下内容,其中我已将 data
和 encodings
从 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"}
}
}]
}
在顶层指定共享属性是一种使图表规范更加简洁易懂的方法。
我想知道 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"}
}
}]
}
在这种情况下,我假设图层 属性 中的任何标记自动继承顶层的编码是否正确?
此外,我注意到我无法将 bar Mark 移动到 Layer 属性 之外(Vega Editor 提示这是不允许的 属性 并且 bars 无法渲染如果放在顶层)。
最后,在更复杂的示例中(参见:https://vega.github.io/vega-lite/examples/layer_line_mean_point_raw.html),编码在层中重复(尽管有冗余 x 编码)-> 所以在这种情况下,当将编码置于顶层与层中是否合适?
Vega-lite 文档详细介绍了这些属性的配置,但我无法找到这 3 个问题的概念性答案。
谢谢
Vega-Lite 提供了层次结构图表模型,其中层次结构中的每个级别都可以覆盖父级别中声明的各种属性。在层规范方面,相关概念是这样的:
- a
UnitSpec
就是你认为的单图:它它,你可以指定data
、mark
、encodings
、transforms
, 和其他属性。 - 一个
LayerSpec
,是一个容器,可以容纳layers
属性中的多个UnitSpec
或LayerSpec
规格。此外,您可以指定data
、encodings
transforms
和其他属性(但不能指定mark
)。
LayerSpec
或其他 top-level 对象中的 UnitSpec
将继承那里指定的任何属性(例如 data
、encodings
、transforms
, 等), 并且还可以通过指定自己的 data
, encodings
, 或 transforms
.
类似的分层概念适用于其他复合图表类型,例如 ConcatSpec
、VConcatSpec
、HConcatSpec
、FacetSpec
等
更具体地说,在您的示例中,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"}
}
}]
}
就父类的继承而言,这在功能上等同于以下内容,其中我已将 data
和 encodings
从 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"}
}
}]
}
在顶层指定共享属性是一种使图表规范更加简洁易懂的方法。