vega-lite:如何将 JSON 中的日期指定为包含年、月和日数字的单独字段?
vega-lite: how to specify date in JSON as separate fields containing year, month, and day numbers?
我的 JSON 数据是一个对象列表,每个对象都包含以下格式的日期:
"date" : {
"year" : 2019,
"month" : 2,
"day" : 17
},
如何告诉 Vega-lite 这是约会对象?
我通过创建另一个 day 字段来解决这个问题,该字段是一个连接这三个字段的字符串,并使用:
"format" : {
"parse" : {
"day" : "date: '%Y %m %d'"
}
但我希望能够只使用现有的 3 个字段....
您可以使用计算转换以及 datetime
表达式来执行此操作。例如 (vega-editor link):
{
"data": {
"values": [
{"date": {"year": 2019, "month": 2, "day": 15}, "val": 1},
{"date": {"year": 2019, "month": 2, "day": 16}, "val": 2},
{"date": {"year": 2019, "month": 2, "day": 17}, "val": 4},
{"date": {"year": 2019, "month": 2, "day": 18}, "val": 3},
{"date": {"year": 2019, "month": 2, "day": 19}, "val": 5},
{"date": {"year": 2019, "month": 2, "day": 20}, "val": 6}
]
},
"transform": [
{
"calculate": "datetime(datum.date.year, datum.date.month, datum.date.day)",
"as": "combined"
}
],
"mark": "area",
"encoding": {
"x": {"field": "combined", "type": "temporal"},
"y": {"field": "val", "type": "quantitative"}
}
}
我的 JSON 数据是一个对象列表,每个对象都包含以下格式的日期:
"date" : {
"year" : 2019,
"month" : 2,
"day" : 17
},
如何告诉 Vega-lite 这是约会对象? 我通过创建另一个 day 字段来解决这个问题,该字段是一个连接这三个字段的字符串,并使用:
"format" : {
"parse" : {
"day" : "date: '%Y %m %d'"
}
但我希望能够只使用现有的 3 个字段....
您可以使用计算转换以及 datetime
表达式来执行此操作。例如 (vega-editor link):
{
"data": {
"values": [
{"date": {"year": 2019, "month": 2, "day": 15}, "val": 1},
{"date": {"year": 2019, "month": 2, "day": 16}, "val": 2},
{"date": {"year": 2019, "month": 2, "day": 17}, "val": 4},
{"date": {"year": 2019, "month": 2, "day": 18}, "val": 3},
{"date": {"year": 2019, "month": 2, "day": 19}, "val": 5},
{"date": {"year": 2019, "month": 2, "day": 20}, "val": 6}
]
},
"transform": [
{
"calculate": "datetime(datum.date.year, datum.date.month, datum.date.day)",
"as": "combined"
}
],
"mark": "area",
"encoding": {
"x": {"field": "combined", "type": "temporal"},
"y": {"field": "val", "type": "quantitative"}
}
}