Search code examples
prometheusvegavega-lite

Using 2D array data instead of table in Vega Lite


I am trying to read data from Prometheus (https://prometheus.io) into Vega Lite. Prometheus returns its data in a 2D array, like this:

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

Is it possible to transform this data in Vega Lite so that it turns into this?

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

I have looked at the documentation, and I can see that it's possible to flatten a 1D array, but I couldn't figure out a way to flatten a 2D array.

Thanks.


Solution

  • You can do this by combining a sequence of 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"}
      }
    }
    

    enter image description here