gpt4 book ai didi

转换中的 Vega-lite 多重聚合

转载 作者:行者123 更新时间:2023-12-05 05:04:41 26 4
gpt4 key购买 nike

我想在转换中实现两个 diff 聚合,因为它们具有不同的 groupby 条件,但使用 vega-lite 似乎不可能

{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"data": {

{"response":200,"request":"/ST"},
{"response":500,"request":"/ST"},
{"response":200,"request":"/PP"},
{"response":500,"request":"/PP"},
{"response":200,"request":"/CP"},
{"response":200,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":500,"request":"/CP"},
{"response":503,"request":"/CP"},
{"response":503,"request":"/CP"},
{"response":503,"request":"/CP"}

"transform": [
{
"aggregate": [{
"op": "count",
"as": "response_count"
}],
"groupby": ["response","request"]
},
{
"aggregate": [{
"op": "count",
"as": "response_c"
}],
"groupby": ["request"]
}
],

{"mark": "bar",
"encoding": {
"x": {"field": "response_count", "type": "quantitative", "stack": "zero"},
"y": {"field": "request", "type": "nominal"},
"color": {"field": "response", "type": "nominal"}}
}

有什么办法可以实现吗?是否支持这样的多个聚合?

最佳答案

是的,支持这样的多个聚合,但是你的图表最后有未定义的字段,因为你没有在你的聚合中引用它们。您从这些数据开始:

[
{"response": 200, "request": "/ST"},
{"response": 500, "request": "/ST"},
{"response": 200, "request": "/PP"},
{"response": 500, "request": "/PP"},
{"response": 200, "request": "/CP"},
{"response": 200, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"}
]

第一个聚合组按"response""request",并在每个组中添加"response_count",看起来像这样:

[
{"response": 200, "request": "/ST", "response_count": 1},
{"response": 500, "request": "/ST", "response_count": 1},
{"response": 200, "request": "/PP", "response_count": 1},
{"response": 500, "request": "/PP", "response_count": 1},
{"response": 200, "request": "/CP", "response_count": 2},
{"response": 500, "request": "/CP", "response_count": 6},
{"response": 503, "request": "/CP", "response_count": 3},
]

您的第二个聚合采用此方法,按 "request" 分组,并在每个组中添加 "response_c",如下所示:

[
{"request": "/ST", "response_c": 2},
{"request": "/PP", "response_c": 2},
{"request": "/CP", "response_c": 3},
]

请注意,您未在聚合中引用的任何字段都将被删除。

然后您的规范引用了数据集中不再存在的字段,这导致了空白图表。

您可以通过在第二个聚合中指定您希望删除的字段发生什么来解决此问题;例如,您可以保留“response_count”的总和和“response”的最小值 (open in editor):

{
"data": {
"values": [
{"response": 200, "request": "/ST"},
{"response": 500, "request": "/ST"},
{"response": 200, "request": "/PP"},
{"response": 500, "request": "/PP"},
{"response": 200, "request": "/CP"},
{"response": 200, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"}
]
},
"transform": [
{
"aggregate": [{"op": "count", "as": "response_count"}],
"groupby": ["response", "request"]
},
{
"aggregate": [
{"op": "count", "as": "response_c"},
{"op": "sum", "field": "response_count", "as": "response_count"},
{"op": "min", "field": "response", "as": "response"}
],
"groupby": ["request"]
}
],
"mark": "bar",
"encoding": {
"x": {"field": "response_count", "type": "quantitative", "stack": "zero"},
"y": {"field": "request", "type": "nominal"},
"color": {"field": "response", "type": "nominal"}
}
}

enter image description here

在这种特殊情况下,更好的做法可能是完全省略第二个聚合,在这种情况下,第二个聚合基本上是通过条形图的堆叠在视觉上发生的 (editor):

{
"data": {
"values": [
{"response": 200, "request": "/ST"},
{"response": 500, "request": "/ST"},
{"response": 200, "request": "/PP"},
{"response": 500, "request": "/PP"},
{"response": 200, "request": "/CP"},
{"response": 200, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 500, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"},
{"response": 503, "request": "/CP"}
]
},
"transform": [
{
"aggregate": [{"op": "count", "as": "response_count"}],
"groupby": ["response", "request"]
}
],
"mark": "bar",
"encoding": {
"x": {"field": "response_count", "type": "quantitative", "stack": "zero"},
"y": {"field": "request", "type": "nominal"},
"color": {"field": "response", "type": "nominal"}
}
}

enter image description here

关于转换中的 Vega-lite 多重聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60929534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com