Search code examples
vega-litevega

Vega: transform array of values into accepted format


I'm using Vega for data visualization. Vega requires data which is shaped as an array of objects:

const accepted = [
    { num: 1, char: 'A' },
    { num: 2, char: 'B' },
    { num: 3, char: 'C' },
]

However, for sending data (e.g. via JSON), it is not efficient as the keys num and char are repeated multiple times. A more efficient way is sending the data as follows:

const efficient = {
   num: [1, 2, 3 ],
   char: ['A', 'B','C']
}

Right now I'm manually transforming the efficient structure above into the accepted structure before passing it into Vega. Is there any Vega transforms that I can use so taht I can pass efficient without having to transform it into accepted?


Solution

  • Use the flatten transform which should do what you need.

    https://vega.github.io/vega/docs/transforms/flatten/

    https://vega.github.io/vega-lite/docs/flatten.html