基于条件的编码,有点像 else if

问题描述 投票:0回答:1

这是我的 vega-lite 规格

{
  
  "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"},
              "value": "green"},
     "angle": {
                  "condition": {"test": "datum['val'] < 0", "value": 90},
              "value": -45}     
  }
}

我想要的是

    如果 val 为
  • ,则为红色箭头 < 0
  • 如果 val
  • 则为黄色箭头 <= 0 && <3
  • 如果 val >= 3,则为绿色箭头
但我不知道语法:-( 这种逻辑在编码中是否可能,或者我是否必须将其移至转换中?

谢谢, 汤姆

json charts visualization vega-lite
1个回答
2
投票
将您的测试添加到数组中,如下所示。

{ "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"}, {"test": "datum['val'] >= 0 && datum['val'] < 3 ", "value": "yellow"}, {"test": "datum['val'] >= 3 ", "value": "green"} ] }, "angle": { "condition": {"test": "datum['val'] < 0", "value": 90}, "value": -45 } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.