可以将规则指定为数据编码。但有时它太冗长了。有没有更短的方法来仅用一个数字来指定它?
在下面的示例中,我想绘制一条水平线y=1,它需要我指定一个计算变换。想知道是否可以使用更紧凑的东西,例如:
"layer": [
...,
{ "mark": { "type": "rule", "y": 1 }, // Specify ruler with just a single line
...
]
Run Code Online (Sandbox Code Playgroud)
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"transform":[
{ "calculate": "1", "as": "one" }
],
"layer": [
{
"mark": { "type": "bar" },
"encoding": {
"x": { "field": "diff", "type": "quantitative" },
"y": { "field": "diff", "type": "quantitative" }
}
},
{
"mark": { "type": "rule" },
"encoding": {
"y": { "field": "one", "type": "quantitative" }
}
}
],
"data": {
"values": [
{ "diff": 1 },
{ "diff": 2 },
{ "diff": 3 }
]
}
}
Run Code Online (Sandbox Code Playgroud)
"value"您可以使用编码中的字段指定单个值;例如
{
"mark": { "type": "rule" },
"encoding": {
"y": { "value": 133 }
}
}
Run Code Online (Sandbox Code Playgroud)
(游乐场)
但要注意这个值是axis域中的值,而不是data域中的值;即它代表图表左上角的像素。
如果要在数据域中指定一个值,则没有这样的简写。您必须使用转换(就像您在问题中所做的那样)或为您的图层定义一个新的数据集。我认为最紧凑的方法是这样的:
{
"mark": { "type": "rule" },
"data": {"values": {"y": 1}},
"encoding": {
"y": { "field": "y" }
}
}
Run Code Online (Sandbox Code Playgroud)