Using Vega Lite to display already-aggregated data

一曲冷凌霜 提交于 2021-02-10 16:50:18

问题


I'm trying to show a stacked bar chart of sums over time. The data looks something like this:

[
  {
    "date": 12345,
    "sumA": 100,
    "sumB": 150
  },
  ...
]

I'm encoding the x axis to the field "date". I need the bar at date 12345 to be stacked with one part being 100 high, and the other, shown in another color, being 150 high.

Vega Lite seems to expect the raw data, but this would be too slow. I do this aggregate on the server side to save time. Can I spoon-feed Vega Lite the aggregates like in my example above?


回答1:


You can use the fold transform to fold your two columns into one, and then the channel encodings take care of the rest. For example (vega editor):

{
  "data": {
    "values": [
      {"date": 1, "sumA": 100, "sumB": 150},
      {"date": 2, "sumA": 200, "sumB": 50},
      {"date": 3, "sumA": 80,  "sumB": 120},
      {"date": 4, "sumA": 120, "sumB": 30},
      {"date": 5, "sumA": 150, "sumB": 110}
    ]
  },
  "transform": [
    {"fold": ["sumA", "sumB"], "as": ["column", "value"]}
  ],
  "mark": {"type": "bar"},
  "encoding": {
    "x": {"type": "ordinal", "field": "date"},
    "y": {"type": "quantitative", "field": "value"},
    "color": {"type": "nominal", "field": "column"}
  }
}



来源:https://stackoverflow.com/questions/58212634/using-vega-lite-to-display-already-aggregated-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!