Vega:在数据中添加丢失的信息

时间:2018-08-01 09:28:07

标签: elasticsearch kibana vega-lite


在kibana中,在Vega-lite可视化中,我想随时间创建应用程序(AAAA)的过渡状态图。 (时间轴)
状态具有固定值(init,start,running ...)

在x轴上的时间戳记,
在州一级,
用于跟踪状态级别的矩形或条形。

| ====
| ======
| ===========
______________________________
 t0 t1 t2 ....

我的数据来自ElasticSearch中的查询,并具有以下格式。

timestamp  app  state
  t0    AAAA  Init
  t1    AAAA  start
  t2    AAAA  Running
  t3    AAAA  stopped

在vega中,我想用条形或矩形表示状态, 例如:Init状态将由从t0开始到t1结束的矩形表示。但是我没有这个信息! t1在下一条数据行中

是否有可能通过利用下一行来计算该值。 数据将是这样。

timestamp  app  state    ends 
  t0    AAAA  Init         t1
  t1    AAAA  start        t2 
  t2    AAAA  Running      t3
  t3    AAAA  stopped      t4 


任何帮助都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

您可以使用lead操作通过窗口变换来找到下一个值。例如:

{
  "data": {
    "values": [
      {"timestamp": 0, "app": "A", "state": "init"},
      {"timestamp": 10, "app": "A", "state": "start"},
      {"timestamp": 25, "app": "A", "state": "run"},
      {"timestamp": 30, "app": "A", "state": "stop"},
      {"timestamp": 40, "app": "B", "state": "init"},
      {"timestamp": 55, "app": "B", "state": "start"},
      {"timestamp": 75, "app": "B", "state": "run"},
      {"timestamp": 85, "app": "B", "state": "stop"},
      {"timestamp": 90, "app": null, "state": "init"}
    ]
  },
  "transform": [
    {"window": [{"op": "lead", "field": "timestamp", "as": "t_stop"}]},
    {"filter": "datum.app != null"}
  ],
  "encoding": {
    "color": {"type": "nominal", "field": "app"},
    "x": {"type": "quantitative", "field": "timestamp"},
    "x2": {"type": "quantitative", "field": "t_stop"},
    "y": {
      "type": "nominal",
      "field": "state",
      "sort": {"op": "sum", "field": "timestamp", "order": "ascending"}
    }
  },
  "mark": "bar",
  "$schema": "https://vega.github.io/schema/vega-lite/v2.6.0.json"
}

enter image description here