使用 Vega Lite 显示已经聚合的数据

Using Vega Lite to display already-aggregated data

我正在尝试显示总和随时间变化的堆积条形图。数据看起来像这样:

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

我正在将 x 轴编码到字段 "date"。我需要将日期为 12345 的柱堆叠起来,一部分高度为 100,另一部分以另一种颜色显示,高度为 150。

Vega Lite seems to expect the raw data,但这太慢了。我在服务器端进行聚合以节省时间。我可以像上面的示例一样用勺子喂 Vega Lite 聚合体吗?

您可以使用 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"}
  }
}