簡體   English   中英

如何將具有恆定值的垂直規則添加到 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
        }
    }
}

我也嘗試過 types: "type": "temportal""type": "quantitative", "aggregate": "distinct" ,但沒有運氣。

我的目標是能夠在圖表中添加多個具有顯式/恆定 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'"}
      }
    },
  },
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM