You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vega-Lite多条件编码语法咨询:按val值设置箭头颜色

直接在Encoding中实现多条件颜色映射

不需要借助transform,Vega-Lite的encoding.color支持多条件数组,按顺序匹配第一个符合条件的规则,最后用兜底值覆盖剩余情况。

以下是修改后的完整规格,核心调整了color字段的条件逻辑:

{
  "data": {
    "values": [
      {"group": "I", "groupSort": 1, "segment": "A", "val": 1},
      {"group": "II", "groupSort": 2, "segment": "B", "val": -3},
      {"group": "III", "groupSort": 3, "segment": "A", "val": 2},
      {"group": "IV", "groupSort": 4, "segment": "B", "val": 3},
      {"group": "V", "groupSort": 5, "segment": "E", "val": -2}
    ]
  },
  "mark": {"type": "text", "size": 20, "text": "➟"},
  "encoding": {
    "y": {"field": "group", "type": "nominal"},
    "color": [
      {"condition": {"test": "datum.val < 0", "value": "red"}},
      {"condition": {"test": "datum.val < 3", "value": "gold"}},
      {"value": "green"}
    ],
    "angle": {
      "condition": {"test": "datum.val < 0", "value": 90},
      "value": -45
    }
  }
}

逻辑说明

  • 第一个条件匹配val < 0的情况,显示红色
  • 第二个条件在排除了val < 0后,匹配val < 3(即0 ≤ val < 3),显示金色(也可直接用"yellow")
  • 最后兜底的value自动匹配val ≥ 3的情况,显示绿色

条件的顺序很重要,必须从范围更明确的规则开始写,避免逻辑覆盖。

内容的提问来源于stack exchange,提问作者Tom Martens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 16:47:32