Vega-Lite中添加固定值水平参考线未正常显示问题求助
问题根因
你参考线图层的data字段设置了{"values": []}空数组,Vega-Lite会基于数据条目数量生成对应数量的标记,空数据下不会生成任何rule元素,因此参考线无法渲染。
解决方案
仅需要给参考线图层的values数组添加一条空数据条目即可,你指定的y.datum已经固定了纵坐标值,rule标记会自动适配整个x轴的数值范围渲染横向参考线:
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "encoding": { "x": {"field": "ts", "type": "temporal"}, "y": {"field": "a", "type": "quantitative"} }, "layer": [ { "data": { "values": [ {"ts": "2021-11-26", "a": 0.16}, {"ts": "2021-11-28", "a": 0.12} ] }, "mark": { "type": "point" } }, { "data": { "values": [{}] }, "mark": {"type": "rule", "color": "red"}, "encoding": {"y": {"datum": 0.15, "type": "quantitative"}} } ] }
如果需要同时绘制多条固定值参考线,可以直接把阈值放到参考线图层的数据中,维护更清晰:
{ "data": { "values": [ {"threshold": 0.15}, {"threshold": 0.13} ] }, "mark": {"type": "rule", "color": "red"}, "encoding": {"y": {"field": "threshold", "type": "quantitative"}} }
内容的提问来源于stack exchange,提问作者crazypeter
相关产品推荐
相关产品推荐

