在 Vega-Lite 中以恒定值显示水平线,不显示
Displaying a horizontal rule at constant value in Vega-Lite, does not show up
我试图在 Vega-Lite 中以恒定值显示水平线,但没有成功。这是代码(可以粘贴在https://vega.github.io/editor/#/):
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"x": {"field": "ts", "type": "temporal"},
"y": {"field": "a", "type": "quantitative"}
},
"layer": [
{
"data": {
"values": [
{"ts": "2021-11-26", "a": 0.16},
{"ts": "2021-11-28", "a": 0.12}
]
},
"mark": { "type": "point" }
},
{
"data": { "values": [] },
"mark": {"type": "rule", "color": "red"},
"encoding": {"y": {"datum": 0.15, "type": "quantitative"}}
}
]
}
在这段代码中,我使用第二层来覆盖常量值的规则,但它就是不显示。有什么想法吗?
这里有两个问题:
您的顶级编码将 x
映射到字段 ts
,但 "data": { "values": [] },
没有任何名为 ts
的字段,因此编码是未定义且未绘制标记。
datum
编码应用于关联数据集中的每个条目,但您的数据集的长度为 0,因此没有条目。您可以改用 "data": { "values": [{}] }
。
解决这两个问题会得到这个 (open in editor):
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"layer": [
{
"data": {
"values": [
{"ts": "2021-11-26", "a": 0.16},
{"ts": "2021-11-28", "a": 0.12}
]
},
"mark": {"type": "point"},
"encoding": {
"x": {"field": "ts", "type": "temporal"},
"y": {"field": "a", "type": "quantitative"}
}
},
{
"data": {"values": [{}]},
"mark": {"type": "rule", "color": "red"},
"encoding": {"y": {"datum": 0.15, "type": "quantitative"}}
}
]
}
我试图在 Vega-Lite 中以恒定值显示水平线,但没有成功。这是代码(可以粘贴在https://vega.github.io/editor/#/):
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"x": {"field": "ts", "type": "temporal"},
"y": {"field": "a", "type": "quantitative"}
},
"layer": [
{
"data": {
"values": [
{"ts": "2021-11-26", "a": 0.16},
{"ts": "2021-11-28", "a": 0.12}
]
},
"mark": { "type": "point" }
},
{
"data": { "values": [] },
"mark": {"type": "rule", "color": "red"},
"encoding": {"y": {"datum": 0.15, "type": "quantitative"}}
}
]
}
在这段代码中,我使用第二层来覆盖常量值的规则,但它就是不显示。有什么想法吗?
这里有两个问题:
您的顶级编码将
x
映射到字段ts
,但"data": { "values": [] },
没有任何名为ts
的字段,因此编码是未定义且未绘制标记。datum
编码应用于关联数据集中的每个条目,但您的数据集的长度为 0,因此没有条目。您可以改用"data": { "values": [{}] }
。
解决这两个问题会得到这个 (open in editor):
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"layer": [
{
"data": {
"values": [
{"ts": "2021-11-26", "a": 0.16},
{"ts": "2021-11-28", "a": 0.12}
]
},
"mark": {"type": "point"},
"encoding": {
"x": {"field": "ts", "type": "temporal"},
"y": {"field": "a", "type": "quantitative"}
}
},
{
"data": {"values": [{}]},
"mark": {"type": "rule", "color": "red"},
"encoding": {"y": {"datum": 0.15, "type": "quantitative"}}
}
]
}