是否可以使用 vega/vega-lite 实现这种聚合?

问题描述

我有一个这种格式的数据列表

import random

user_ids = [1,2,3,1,2]
item_ids = [8,9,10,5,8]
val_user_ids = sorted(set(user_ids))
random.shuffle(item_ids)
val_item_ids = [item_ids.pop(i) for i in range(len(val_user_ids))]

我的目标是实现除id=100之外的所有文档的x和y字段的总和聚合,然后从id=100的文档的x和y值中减去这个聚合结果,并将这个结果显示为文本类型标记. 我试过以下:

[
    {"id": 100,"y": 28,"c":0},{"id": 1,"y": 20,"c":1},{"id": 2,"y": 43,{"id": 3,"y": 35,{"id": 4,"y": 81,{"id": 5,"y": 10,{"id": 6,"y": 19,{"id": 7,"y": 15,{"id": 8,"y": 52,{"id": 9,"y": 48,"c":1}
]

请帮助我如何实现 id=100 的减法

解决方法

我能够使用 Vega 的 JoinAggregate 转换解决这个问题,方法是将聚合值作为附加列添加到数据集中,然后过滤以获得具有所需值的单行!

{
  "$schema": "https://vega.github.io/schema/vega/v3.0.json","title": "Sum amount Per id","data": [
    {
      "name": "table","values": [
        {"id": 100,"y": 2800,"c": 1000},{"id": 1,"y": 20,"c": 1},{"id": 2,"y": 43,"c": 0},{"id": 3,"y": 35,{"id": 4,"y": 81,{"id": 5,"y": 10,{"id": 6,"y": 19,{"id": 7,"y": 15,{"id": 8,"y": 52,{"id": 9,"y": 48,"c": 1}
      ],"transform": [
        {
          "type": "joinaggregate","ops": ["sum","sum"],"fields": ["c","y"],"as": ["sumc","sumy"]
        },{
        "type":"filter"
        "expr":"datum.id==100"
        }
      ]
    }
  ],"marks": [
    {
      "type": "text","from": {"data": "table"},"encode": {
        "update": {
          "text": {"signal": "-datum.sumc+datum.c*2"},"align": {"value": "center"},"baseline": {"value": "middle"},"xc": {"signal": "width/4"},"yc": {"signal": "height/2"},"fontSize": {"signal": "min(width/10,height)/1.3"}
        }
      }
    },{
      "type": "text","encode": {
        "update": {
          "text": {"signal": "datum.y-datum.sumy+datum.y"},"xc": {"signal": "width*3/4"},height)/1.3"}
        }
      }
    }
  ]
}