Vega - setting y Axis on bar chart/gauge to a dynamic min value based on a calculation

23 views Asked by At

I have tried a few variations on this, firstly in Vega-Lite and all things started to point to Vega due the complexity of what I am trying to do. I should say I am not a coder but do know how to use ChatGPT to develop complex Power BI dashboards. I am developing a vertical gauge/bar visual that is unique to trading FX accounts, and for the most part I can create it using third party visual but it is not 100% so I turned to Vega in the end and have managed to create the visual and now what I would like to do is set the min y axis level to be based on a calculation by taking the Starting Balance measure and X .92, and for the max y axis setting taking the "Starting Balance" X 1.08. The visual/gauge should be only work in this range. as you will from the screen capture using "0" as the starting point for the Y axis is not helpful. This way the visual will be useable for the trader. I have tried various different techniques using Vega and Deneb which hasn't worked. The key data fields that are being used in the visual include: "Starting Balance", "Sum of Profit Target Value", "MLL 8%", "WLL 5%", and "CumulativeBalance". I have included a screenshot of the dashboard using a third party visual which is not quite right and looks terrible, along with visual that I have created so far with Vega, I should note that this visual is per "login ID" trading account.[[

{
  
  "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"}
        }
      }
    }
  ]
}

(https://i.stack.imgur.com/CBH0F.png)](https://i.stack.imgur.com/UPAVN.png)

Defining a Transform with calculate for both Min and Max levels, examples just has min, Vega-lite doesn't seemt to work and everything that I have looked up says you have to define min and max implicitly which is not going to work in this case.

0

There are 0 answers