首页 > 解决方案 > Vega-Lite:是否可以将图像作为折线图中的标签?

问题描述

我想将图像作为我的轴标签。我尝试使用图像标记,但这不起作用,这是意料之中的。标签表达也是我尝试过的,但如果我想让它成为一个图像,那它就不起作用了。我还能尝试什么或者有什么可能?

折线图示例

标签: vega-litevega

解决方案


在另一个答案中使用相同的技术,可以通过额外的层添加图像轴:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"x": 0.5, "y": 0.5, "img": "data/ffox.png"},
      {"x": 1.5, "y": 1.5, "img": "data/gimp.png"},
      {"x": 2.5, "y": 2.5, "img": "data/7zip.png"}
    ]
  },
  "layer": [{
    "mark": {"type": "image", "width": 50, "height": 50},
    "encoding": {
      "x": {"field": "x", "type": "quantitative"},
      "y": {"field": "y", "type": "quantitative"},
      "url": {"field": "img", "type": "nominal"}
    }
  }, {
    "transform": [{"calculate": "-.2", "as": "axis"}],
    "mark": {"type": "image", "width": 25, "height": 25},
    "encoding": {
      "x": {"field": "x", "type": "quantitative", "axis":{"labelExpr": "", "title": null}},
      "y": {"field": "axis", "type": "quantitative", "scale": {"domain": [0, 2.5]}},
      "url": {"field": "img", "type": "nominal"}
    }
  }],
  "resolve": {"scale": {"y": "shared"}}
}

织女星编辑器

===== 2021-07-20 =====

您的 BarChartx编码使用的x字段分布不均匀,因此未对齐。

如果您的编辑器中确实a显示了该字段,最简单的方法是将编码替换为"x": {"field": "a", "type": "ordinal", "axis": null}

织女星编辑器

即使该a字段不存在,您可能想要添加这样一个字段来对齐甚至排序图像轴。

我能想到的最后一个手段是window变换,这是一种过度杀伤,但也没有增加额外的价值:

织女星编辑器


推荐阅读