使用vega/vega-lite可以实现这种聚合吗



我有一个格式的数据列表

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

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

{
$schema: https://vega.github.io/schema/vega/v3.0.json
title: Sum amount Per id
data: [
{
"name": "table",
"values": [
{"id": 100, "y": 28, "c":0},
{"id": 1, "y": 20, "c":1},
{"id": 2, "y": 43, "c":0},
{"id": 3, "y": 35, "c":1},
{"id": 4, "y": 81, "c":0},
{"id": 5, "y": 10, "c":1},
{"id": 6, "y": 19, "c":0},
{"id": 7, "y": 15, "c":1},
{"id": 8, "y": 52, "c":0},
{"id": 9, "y": 48, "c":1}
]
transform: [
{
type: aggregate
ops: ["sum","sum"]
fields: ["c", "y"]
as: ["sumc","sumy"]
}

]
}
]
marks: [
{
type: text
from: {data: "table"}
encode: {
update: {
text: {signal: "datum.sumc"}
align: {value: "center"}
baseline: {value: "middle"}
xc: {signal: "width/4"}
yc: {signal: "height/2"}
fontSize: {signal: "min(width/10, height)/1.3"}
}
}
}
{
type: text
from: {data: "table"}
encode: {
update: {
text: {signal: "datum.sumy"}
align: {value: "center"}
baseline: {value: "middle"}
xc: {signal: "width*3/4"}
yc: {signal: "height/2"}
fontSize: {signal: "min(width/10, height)/1.3"}
}
}
}
]
}

请帮助我如何实现从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, "c": 1},
{"id": 4, "y": 81, "c": 0},
{"id": 5, "y": 10, "c": 1},
{"id": 6, "y": 19, "c": 0},
{"id": 7, "y": 15, "c": 1},
{"id": 8, "y": 52, "c": 0},
{"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",
"from": {"data": "table"},
"encode": {
"update": {
"text": {"signal": "datum.y-datum.sumy+datum.y"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"xc": {"signal": "width*3/4"},
"yc": {"signal": "height/2"},
"fontSize": {"signal": "min(width/10, height)/1.3"}
}
}
}
]
}

最新更新