如何将具有常量值的垂直规则添加到 Vega Lite 图表?

How to add vertical Rule with constant value to Vega Lite chart?

我想在我的图表中添加垂直规则线作为日期里程碑指示器(如图中的红线)。 X 轴是日期(时间),y 轴值是数字。

图像中是我在规则层中使用显式数据值 属性 可以获得的最接近值:

{
    "mark": "rule",
    "data": {
        "values": [
            "{\"x\":\"2020/04/10\"}"
        ]
    },
    "encoding": {
        "x": {
            "field": "x",
            "type": "ordinal",
        },
        "color": {
            "value": "red"
        },
        "size": {
            "value": 1
        }
    }
}

我也尝试过以下类型:"type": "temportal""type": "quantitative", "aggregate": "distinct",但没有成功。

我的目标是能够将具有 explicit/constant x 值的多个红色垂直规则线添加到图表。

Datum 用于指定文字固定值。您可以通过将规则与主要数据分层来添加多个规则。这种方法适用于在 x 通道中编码的定量数据:

"layer": [
  {
    "mark": { "type": "line" },
    "encoding": { "y": {...}, },
  },
  {
    "mark": { "type": "rule", "color": "red", "size": 1, },
    "encoding": {"x": {"datum": 42}},
  },
  {
    "mark": { "type": "rule", "color": "blue", "size": 1, },
    "encoding": {"x": {"datum": 100}},
  },
]

为了处理时间数据,您还必须指定应该如何解析它。这种方法对我有用:

"layer": [
  {
    // First layer: spec of your main linear plot.
  },
  {
    // Second layer: spec of the vertical rulers.
    "mark": { "type": "rule", "color": "red", "size": 2, },
    "encoding": {
      "x": { "field": "date", "type": "temporal", },
    },
    "data": {
      "values": [
        {"date": "25 May 2020 14:15:00"},
        {"date": "25 May 2020 14:20:59"},
      ],
      "format": {
        "parse": {"date": "utc:'%d %b %Y %H:%M:%S'"}
      }
    },
  },
]