gpt4 book ai didi

vega-lite - 将 domainMin 设置为数据中最大日期之前的 6 个月

转载 作者:行者123 更新时间:2023-12-05 02:35:53 25 4
gpt4 key购买 nike

我有以下 Vega-Lite 图表:

Open the Chart in the Vega Editor

目前我设置的比例如下:

"scale": {"domainMin": "2021-06-01"}

但是,我真正想要的是 domainMin 自动计算为数据中 notification_date 字段中的最新日期之前 6 个月。我看过 aggregateexpressions , 但还不是很清楚。

如何获取 notification_date 的最大值并从中减去 6 个月,然后在 "domainMin" 中使用它?

编辑:澄清一下,我不想过滤数据。我希望用户能够缩小或平移以查看初始 6 个月窗口之外的数据。我用 "scale": {"domainMin": "2021-06-01"} 得到了我想要的,但这很快就过时了。

最佳答案

我已经尝试将 paramsexpr 赋给 domainMin,但我无法通过 datum 使用 expr 中的数据字段

我尝试的第二种方法适合您,在此您需要使用joinaggregate/calculate/filter 转换。您将手动收集最大年份和最大月份,然后使用它来过滤您的数据。

以下是修改后的配置或引用editor网址:

{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": "container",
"height": "container",
"config": {
"group": {"fill": "#e5e5e5"},
"arc": {"fill": "#2b2c39"},
"area": {"fill": "#2b2c39"},
"line": {"stroke": "#2b2c39"},
"path": {"stroke": "#2b2c39"},
"rect": {"fill": "#2b2c39"},
"shape": {"stroke": "#2b2c39"},
"symbol": {"fill": "#2b2c39"},
"range": {
"category": [
"#2283a2",
"#003e6a",
"#a1ce5e",
"#FDBE13",
"#F2727E",
"#EA3F3F",
"#25A9E0",
"#F97A08",
"#41BFB8",
"#518DCA",
"#9460A8",
"#6F7D84",
"#D1DCA5"
]
}
},
"title": "South Western Sydney Cumulative and Daily COVID-19 Cases by LGA",
"data": {
"url": "https://davidwales.github.io/nsw-covid-19-data/confirmed_cases_table1_location.csv"
},
"transform": [
{
"filter": {
"and": [
{"field": "lhd_2010_name", "equal": "South Western Sydney"},
{"not": {"field": "lga_name19", "equal": "Penrith"}}
]
}
},
{"calculate": "utcyear(datum.notification_date)", "as": "yearNumber"},
{"calculate": "utcmonth(datum.notification_date)", "as": "monthNumber"},
{
"window": [
{"op": "count", "field": "notification_date", "as": "cumulative_count"}
],
"frame": [null, 0]
},
{
"joinaggregate": [
{"field": "monthNumber", "op": "max", "as": "max_month_count"},
{"field": "yearNumber", "op": "max", "as": "max_year"}
]
},
{"calculate": "abs(datum.max_month_count-6)", "as": "min_month_count"},
{
"filter": "datum.min_month_count < datum.monthNumber && datum.max_year === datum.yearNumber"
}
],
"layer": [
{
"selection": {
"date": {"type": "interval", "bind": "scales", "encodings": ["x"]}
},
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"x": {
"timeUnit": "yearmonthdate",
"field": "notification_date",
"type": "temporal",
"title": "Date"
},
"color": {
"field": "lga_name19",
"type": "nominal",
"title": "LGA",
"legend": {"orient": "top", "columns": 4}
},
"y": {
"aggregate": "count",
"field": "lga_name19",
"type": "quantitative",
"title": "Cases",
"axis": {"title": "Daily Cases by SWS LGA"}
}
}
},
{
"mark": "line",
"encoding": {
"x": {
"timeUnit": "yearmonthdate",
"field": "notification_date",
"title": "Date",
"type": "temporal"
},
"y": {
"aggregate": "max",
"field": "cumulative_count",
"type": "quantitative",
"axis": {"title": "Cumulative Cases"}
}
}
}
],
"resolve": {"scale": {"y": "independent"}}
}

关于vega-lite - 将 domainMin 设置为数据中最大日期之前的 6 个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70429932/

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