Search code examples
powerbipowerbi-desktopvega-litevegadeneb

VEGA - custom minimum value for y-axis in a bar chart


I have data to display in a bar chart. There isn't much difference among the months, typically around ±2%. That's why I need to set a custom scale for the minimum and maximum values on the y-axis, so the differences are more evident.

I'm able to retrieve the desired maximum and minimum values on the y-axis. The maximum value is accepted by the code. However, the minimum value is not and the axis always starts from 0.

Could you please advise on what I might be doing wrong in the code below?

Many thanks!

.pbix available here : https://github.com/tomecsek21/pbix_file

{
  "autosize": "fit",
  "data": [
    {
      "name": "table",
      "values": [
        {"day": "1", "value": 41200},
        {"day": "2", "value": 39200},
        {"day": "3", "value": 40800},
        {"day": "4", "value": 39500},
        {"day": "5", "value": 38700}
      ],
      "transform": [
        {
          "type": "joinaggregate",
          "fields": ["value", "value"],
          "ops": ["max", "min"],
          "as": [
            "max_value",
            "min_value"
          ]
        },
        {
          "type": "formula",
          "expr": "datum.max_value*1.2",
          "as": "maximum"
        },
        {
          "type": "formula",
          "expr": "datum.min_value*0.8",
          "as": "minimum"
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "increment",
      "value": 1,
      "on": [
        {
          "events": "timer{25}",
          "update": "increment + 2000"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {
        "data": "table",
        "field": "day"
      },
      "range": "width"
    },
    {
      "name": "yscale",
      "type": "linear",
      "domain": {
        "data": "table",
        "fields": ["minimum", "maximum"]
      },
      "range": "height"
    }
  ],
  "axes": [
    {
      "orient": "left",
      "scale": "yscale",
      "tickCount": 5,
      "grid": true,
      "gridDash": [1, 3]
    },
    {
      "orient": "bottom",
      "scale": "xscale"
    }
  ],
  "marks": [
    {
      "type": "rect",
      "name": "potential",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {
            "scale": "xscale",
            "field": "day",
            "band": 0.35,
            "offset": 12
          },
          "width": {"value": 40},
          "fill": {"value": "#363636"}
        },
        "update": {
          "y": {
            "scale": "yscale",
            "value": 0
          },
          "y2": {
            "signal": "increment<=datum.value? scale('yscale',increment): scale('yscale',datum.value)"
          }
        }
      }
    }
  ]
}

Solution

  • Your spec doesn't work for me but trying adding the following to your yscale:

    "zero":false