Vega-lite:当x通道为fieldT时条形图太细,如何更好地对齐并调整宽度或填充?
Vega-lite: the bar chart is too thin when x channel is a fieldT, how can align it better and adjust width or padding?
我正在用 vega-lite 制作一些条形图,使用 vega-lite-api;原始数据带有一个名为“月”的字段,其值类似于“2020/09”“2020/08”...“2019/06”...
fieldT
识别得很好,我可以将画笔应用于 select 更窄的时间范围;但是条形图看起来不太好,宽度似乎总是固定值,太细而且间距太宽,
但在此视觉效果中,更有意义的是使条形图与月的中心对齐,因为 y 轴上的数据是针对整个月而不是单个日期(该月的第一天)汇总的);
那么如何让这些条从每个月的开始到月底都覆盖起来,并且只留一点间隙(比如 5px 之间?就像下面的fieldO
)
如果将x通道改为使用fieldO
的Ordinal值,那么宽度会比想要的更好,并且当画笔select变化时它会很好地适应宽度;但是月份标签会保留原样,不太好;
听起来您正在寻找的功能是 Time Unit。如果将 timeUnit 应用于时间编码,它将导致特征的视觉表示填充给定的时间跨度。
例如,这里有一些与您的数据类似的使用原始时间编码 (view in editor) 的数据:
{
"mark": "bar",
"encoding": {
"x": {"field": "month", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
},
"data": {
"values": [
{"month": "2019/01", "value": 1}, {"month": "2019/02", "value": 2}, {"month": "2019/03", "value": 1},
{"month": "2019/04", "value": 4}, {"month": "2019/05", "value": 7}, {"month": "2019/06", "value": 3},
{"month": "2019/07", "value": 4}, {"month": "2019/08", "value": 6}, {"month": "2019/09", "value": 8},
{"month": "2019/10", "value": 10}, {"month": "2019/11", "value": 7}, {"month": "2019/12", "value": 5},
{"month": "2020/01", "value": 6}, {"month": "2020/02", "value": 9}, {"month": "2020/03", "value": 8},
{"month": "2020/04", "value": 10}, {"month": "2020/05", "value": 11}, {"month": "2020/06", "value": 9},
{"month": "2020/07", "value": 14}, {"month": "2020/08", "value": 15}, {"month": "2020/09", "value": 13},
{"month": "2020/10", "value": 10}, {"month": "2020/11", "value": 16}, {"month": "2020/12", "value": 18}
]
},
"width": 500
}
您可以像这样将 yearmonth
timeUnit 应用于 x 编码:
"x": {"field": "month", "type": "temporal", "timeUnit": "yearmonth"},
如果这样做,结果如下所示 (view in editor):
我正在用 vega-lite 制作一些条形图,使用 vega-lite-api;原始数据带有一个名为“月”的字段,其值类似于“2020/09”“2020/08”...“2019/06”...
fieldT
识别得很好,我可以将画笔应用于 select 更窄的时间范围;但是条形图看起来不太好,宽度似乎总是固定值,太细而且间距太宽,
但在此视觉效果中,更有意义的是使条形图与月的中心对齐,因为 y 轴上的数据是针对整个月而不是单个日期(该月的第一天)汇总的);
那么如何让这些条从每个月的开始到月底都覆盖起来,并且只留一点间隙(比如 5px 之间?就像下面的fieldO
)
如果将x通道改为使用fieldO
的Ordinal值,那么宽度会比想要的更好,并且当画笔select变化时它会很好地适应宽度;但是月份标签会保留原样,不太好;
听起来您正在寻找的功能是 Time Unit。如果将 timeUnit 应用于时间编码,它将导致特征的视觉表示填充给定的时间跨度。
例如,这里有一些与您的数据类似的使用原始时间编码 (view in editor) 的数据:
{
"mark": "bar",
"encoding": {
"x": {"field": "month", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"}
},
"data": {
"values": [
{"month": "2019/01", "value": 1}, {"month": "2019/02", "value": 2}, {"month": "2019/03", "value": 1},
{"month": "2019/04", "value": 4}, {"month": "2019/05", "value": 7}, {"month": "2019/06", "value": 3},
{"month": "2019/07", "value": 4}, {"month": "2019/08", "value": 6}, {"month": "2019/09", "value": 8},
{"month": "2019/10", "value": 10}, {"month": "2019/11", "value": 7}, {"month": "2019/12", "value": 5},
{"month": "2020/01", "value": 6}, {"month": "2020/02", "value": 9}, {"month": "2020/03", "value": 8},
{"month": "2020/04", "value": 10}, {"month": "2020/05", "value": 11}, {"month": "2020/06", "value": 9},
{"month": "2020/07", "value": 14}, {"month": "2020/08", "value": 15}, {"month": "2020/09", "value": 13},
{"month": "2020/10", "value": 10}, {"month": "2020/11", "value": 16}, {"month": "2020/12", "value": 18}
]
},
"width": 500
}
您可以像这样将 yearmonth
timeUnit 应用于 x 编码:
"x": {"field": "month", "type": "temporal", "timeUnit": "yearmonth"},
如果这样做,结果如下所示 (view in editor):