将domainMin设置为数据中最长日期前6个月



我有以下Vega Lite图表:

在Vega编辑器中打开图表

目前,我的比例设置如下:

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

然而,我真正想要的是domainMin自动计算为数据中notification_date字段中的最新日期前6个月。我看过聚合和表达式,但并不完全清楚。

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

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

我曾尝试将paramsexpr赋给domainMin,但无法使用exprdatum中的数据字段。

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

以下是修改后的配置或参考编辑器url:

{
"$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"}}
}

过滤大约最后六个月数据的更简单的方法可能如下所示:

"transform": [
...,
{"joinaggregate": [{"op": "max", "field": "notification_date", "as": "last_date"}]},
{"filter": "datum.notification_date > datum.last_date - 6 * 30 * 24 * 60 * 60 * 1000"}
]

它利用了日期存储为毫秒时间戳的事实,并具有跨年份工作的好处。

这不是你问的问题的答案,但如果你的数据是最新的(也就是说,最近的数据点接近当前日期(,你可以这样做:

"scale": { "domainMin": { "expr": "timeOffset('month', now(), -6)" } }

最新更新