在 Vega Lite 中使用二维数组数据代替 table

Using 2D array data instead of table in Vega Lite

我正在尝试将 Prometheus (https://prometheus.io) 中的数据读入 Vega Lite。 Prometheus returns 它的数据在一个二维数组中,像这样:

[[1, 10], [3, 6], [5, 0], [9, 4], [11, 2]]

是否可以在 Vega Lite 中转换这些数据,使其变成这样?

[
  {
    "time": 1,
    "value": 10
  },
  {
    "time": 3,
    "value": 6,
  },
...
]

我查看了文档,发现可以展平一维数组,但我想不出展平二维数组的方法。

谢谢。

您可以通过组合 flatten transforms and calculate transforms. For example (view in editor):

的序列来做到这一点
{
  "data": {"values": {"data": [[1, 10], [3, 6], [5, 0], [9, 4], [11, 2]]}},
  "transform": [
    {"flatten": ["data"]},
    {"calculate": "datum.data[0]", "as": "time"},
    {"calculate": "datum.data[1]", "as": "value"}
  ],
  "mark": "line",
  "encoding": {
    "x": {"field": "time", "type": "quantitative"},
    "y": {"field": "value", "type": "quantitative"}
  }
}