transform - timeUnit 在扁平化和泛化转换后不起作用
问题描述
扁平化和泛化转换后是否可以使用 timeUnit ?在下面的示例中它不起作用!
如果我从它绘制的 x 轴上删除 timeUnit,但没有 timeUnit 带来的好处。
谢谢
这是一个示例代码,可以在下面的链接中执行 https://vega.github.io/editor/#/edited
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Sales in a Year.",
"width": 500,
"height": 200,
"data": {
"values": [
{"timestamp": ["2019-01-01","2019-02-01","2019-03-01","2019-04-01","2019-05-01","2019-06-01",
"2019-07-01","2019-08-01","2019-09-01","2019-10-01","2019-11-01","2019-12-01"],
"cars" : [55, 43, 91, 81, 53, 19, 87, 52, 52, 44, 52, 52],
"bikes" : [12, 6, 2, 0, 0, 0, 0, 0, 0, 3, 9, 15]}
]
},
"transform": [
{"flatten": ["timestamp", "cars", "bikes"]},
{"fold": ["cars", "bikes"]}
],
"mark": {"type":"bar", "tooltip": true, "cornerRadiusEnd": 4},
"encoding": {
"x": {"field": "timestamp",
"timeUnit": "month",
"type": "ordinal",
"title": "",
"axis": {"labelAngle": 0}},
"y": {"field": "value",
"type": "quantitative",
"title": "Soiling Loss"},
"color":{"field": "key",
"type": "nominal"}
}
}
解决方案
为方便起见,输入数据中具有简单时间编码的字符串会自动解析为日期,但这种解析不适用于转换结果的数据。
在这种情况下,您可以使用计算转换(在编辑器中查看)手动进行解析:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Sales in a Year.",
"width": 500,
"height": 200,
"data": {
"values": [
{
"timestamp": [
"2019-01-01",
"2019-02-01",
"2019-03-01",
"2019-04-01",
"2019-05-01",
"2019-06-01",
"2019-07-01",
"2019-08-01",
"2019-09-01",
"2019-10-01",
"2019-11-01",
"2019-12-01"
],
"cars": [55, 43, 91, 81, 53, 19, 87, 52, 52, 44, 52, 52],
"bikes": [12, 6, 2, 0, 0, 0, 0, 0, 0, 3, 9, 15]
}
]
},
"transform": [
{"flatten": ["timestamp", "cars", "bikes"]},
{"fold": ["cars", "bikes"]},
{"calculate": "toDate(datum.timestamp)", "as": "timestamp"}
],
"mark": {"type": "bar", "tooltip": true, "cornerRadiusEnd": 4},
"encoding": {
"x": {
"field": "timestamp",
"timeUnit": "month",
"type": "ordinal",
"title": "",
"axis": {"labelAngle": 0}
},
"y": {"field": "value", "type": "quantitative", "title": "Soiling Loss"},
"color": {"field": "key", "type": "nominal"}
}
}
推荐阅读
- python - groupby+sum/mean/ect 然后分组值返回到原始数据帧中的原始未分组索引吗?
- rust - 如何防止 impl Trait 在仍然借用时被删除?
- ruby-on-rails - Rails 中的三重嵌套属性形式
- json - 在 viewDidLoad() swift 中后续使用之前强制(要求)json API 加载完成(完成)
- javascript - javascript数组推送不断重复自己无法找出原因
- python - 滑动窗口问题的时间复杂度
- python - 如何使用 Python 图像库中的“图像”函数中的像素数据写入二进制文件?
- python - 使用 python 在 Barchart 网站上抓取表格
- machine-learning - XGBoost 特征具有更高的特征重要性,但准确性更低
- git - 如何仅在我的工作忽略合并的情况下运行 difftool