Search code examples
jsonchartsvisualizationvega-litevega

How to add an additional field to the dataset in Vega-Lite


I am working on preparing a vega chart and my dataset is as follows:

data: {
"values": [
  {"key": "title1", "value": "100"},
  {"key": "title2", "value": "500"},
  {"key": "title3", "value": "400"},
  {"key": "title4", "value": "200"},
  {"key": "title5", "value": "600"},
  {"key": "title6", "value": "50"},
  {"key": "title7", "value": "10"}
]}

I want to add another field 'result' to this array whose value is based on the existing value field.
For example:
result['title1'] = value['title1'] / value['title2']
result['title2'] = value['title2'] / value['title3']

Output:

data: {
"values": [
  {"key": "title1", "value": "100", "result": 0.2},
  {"key": "title2", "value": "500", "result": 1.25},
  ...
]}

I tried using transform in vega-lite but was not able to achieve it. How can I achieve it?


Solution

  • Use a window transform as per comments.