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

Vega技术求助:为外汇账户条形/仪表图设置动态Y轴极值

问题描述

我开发Power BI仪表盘时,最初使用Vega-Lite,后来因需求复杂度改用Vega。现在要制作外汇交易账户的垂直仪表/条形可视化组件,第三方工具无法满足需求,已完成基础可视化,但卡在Y轴范围设置上:

  • 需将Y轴最小值设为「Starting Balance」指标×0.92,最大值设为「Starting Balance」×1.08,仅在该范围内展示内容——当前Y轴从0开始的展示对交易者毫无意义
  • 尝试过Vega和Deneb的多种方法均未成功,包括在Transform中计算极值,但找到的示例仅涉及最小值;查阅资料提到的隐式定义极值方案也不适用于当前场景

可视化按「Login ID」交易账户单独展示,用到的关键数据字段包括:「Starting Balance」、「Sum of Profit Target Value」、「MLL 8%」、「WLL 5%」和「CumulativeBalance」。
附两张截图:

  • 第三方工具效果不佳的展示
  • 当前Vega实现的效果
当前Vega配置代码
{  
  "data": [{"name": "dataset"}],
  "transform": [
        {
          "type": "formula",
          "expr": "datum['Starting Balance'] * 0.92",
          "as": "MLL8"
        }
  ],
  "scales": [
  {
    "name": "xscale",
    "type": "band",
    "domain": {
      "data": "dataset",
      "field": "Login"
    },
    "range": "width",
    "padding": 0.1,
    "round": true
  },
  {
    "name": "yscale",
    "domain": {
      "data": "dataset",
      "fields": ["MLL8", "MLL 8%", "Sum of Profit Target Value", "CumulativeBalance", "Starting Balance", "WLL 5%"]
    },
    "nice": true, 
    "range": "height"
  }
],
  "axes": [
    {
      "scale": "yscale",
      "orient": "left",
      "title": "Value"
    },
    {
      "scale": "xscale",
      "orient": "bottom",
      "title": "Login",
      "tickCount": 5
    }
  ],
  "marks": [
    {
      "type": "rect",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "tooltip": {
            "signal": "{'Login': datum['Login'], 'Sum of Profit Target Value': datum['Sum of Profit Target Value'], 'CumulativeBalance': datum['CumulativeBalance']}"
          },
          "y": {
            "scale": "yscale",
            "field": "Sum of Profit Target Value"
          },
          "y2": {
            "scale": "yscale",
            "field": "MLL 8% "
          },
          "x": {
            "scale": "xscale",
            "field": "Login"
          },
          "width": {
            "scale": "xscale",
            "band": 1
          },
          "opacity": {"value": 0.3},
          "fill": {"value": "lightgrey"} 
        }
      }
    },
    {
      "type": "rect",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "tooltip": {
            "signal": "{'Login': datum['Login'], 'Sum of Profit Target Value': datum['Sum of Profit Target Value'], 'CumulativeBalance': datum['CumulativeBalance']}"
          },
          "y": {
            "scale": "yscale",
            "field": "CumulativeBalance"
          },
          "y2": {
            "scale": "yscale",
            "value": 0
          },
          "x": {
            "scale": "xscale",
            "field": "Login",
            "offset": {"scale": "xscale", "band": 0.25} 
          },
          "width": {
            "scale": "xscale",
            "band": 0.5 
          },
          "opacity": {"value": 0.3},
          "fill": {"value": "red"} 
        }
      }
    },
    {
      "type": "rule",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "y": {"scale": "yscale", "field": "Starting Balance"},
          "x": {"value": 0},
          "x2": {"signal": "width"},
          "stroke": {"value": "blue"},
          "strokeWidth": {"value": 2},
          "tooltip": {"signal": "'Starting Balance: ' + datum['Starting Balance']"}
        }
      }
    },
    {
      "type": "rule",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "y": {"scale": "yscale", "field": "WLL 5%"},
          "x": {"value": 0},
          "x2": {"signal": "width"},
          "stroke": {"value": "red"},
          "strokeWidth": {"value": 1},
          "tooltip": {"signal": "'WLL 5%: ' + datum['WLL 5%']"}
        }
      }
    },
    {
      "type": "rule",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "y": {"scale": "yscale", "field": "Sum of Profit Target Value"},
          "x": {"value": 0},
          "x2": {"signal": "width"},
          "stroke": {"value": "green"},
          "strokeWidth": {"value": 2},
          "tooltip": {"signal": "'Sum of Profit Target Value: ' + datum['Sum of Profit Target Value']"}
        }
      }
    },
    {
      "type": "rule",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "y": {"scale": "yscale", "field": "MLL 8%"},
          "x": {"value": 0},
          "x2": {"signal": "width"},
          "stroke": {"value": "red"},
          "strokeWidth": {"value": 2},
          "tooltip": {"signal": "'MLL 8%: ' + datum['MLL 8%']"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "x": {"signal": "width", "offset": 20},
          "y": {"scale": "yscale", "field": "Starting Balance", "offset": 0},
          "text": {"value": "SB"},
          "align": {"value": "right"},
          "baseline": {"value": "middle"},
          "fill": {"value": "black"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "x": {"signal": "width", "offset": 30},
          "y": {"scale": "yscale", "field": "WLL 5%", "offset": 0},
          "text": {"value": "WLL"},
          "align": {"value": "right"},
          "baseline": {"value": "middle"},
          "fill": {"value": "black"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "x": {"signal": "width", "offset": 30},
          "y": {"scale": "yscale", "field": "MLL 8%", "offset": 0},
          "text": {"value": "MLL"},
          "align": {"value": "right"},
          "baseline": {"value": "middle"},
          "fill": {"value": "red"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "dataset"},
      "encode": {
        "enter": {
          "x": {"signal": "width", "offset": 20},
          "y": {"scale": "yscale", "field": "Sum of Profit Target Value", "offset": 0},
          "text": {"value": "PT"},
          "align": {"value": "right"},
          "baseline": {"value": "middle"},
          "fill": {"value": "black"}
        }
      }
    }
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:15:55