az5112
az5112

Reputation: 642

How to add new rows to Vega input data

Is there a Vega transform that would allow replacing a row in the input by multiple rows in the output?

In my particular use case I would like to transform the following input data

[
    {"open": "2019-01-01", "close": "2021-10-11"},
    {"open": "2020-22-22", "close": null}
]

into the following output:

[
    {"date": "2019-01-01", "type": "open"},
    {"date": "2021-10-11", "type": "close"},
    {"date": "2020-22-22", "type": "open"}
]

Note how the first row of input data is morphed into two rows in the output.

Here is a stub in the Vega editor.

Upvotes: 1

Views: 284

Answers (1)

Roy Ing
Roy Ing

Reputation: 797

The Vega transform fold can do that:

"transform": [
    {
      "type": "fold",
      "fields": ["open", "close"],
      "as": ["type", "date"]
    },
    {
      "type": "filter",
      "expr": "datum['date']"
    }
  ]
}

]

View in Vega on-line editor

enter image description here

Upvotes: 1

Related Questions