VEGA - 条形图中 y 轴的自定义最小值

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

我有数据要在条形图中显示。月份之间差异不大,通常在±2%左右。这就是为什么我需要为 y 轴上的最小值和最大值设置自定义比例,以便差异更加明显。

我能够检索 y 轴上所需的最大值和最小值。代码接受最大值。然而,最小值不是,并且轴始终从 0 开始。

您能否建议我在下面的代码中可能做错了什么?

非常感谢!

{
  "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)"
          }
        }
      }
    }
  ]
}
powerbi powerbi-desktop vega-lite vega deneb
1个回答
0
投票

您的规格对我不起作用,但尝试将以下内容添加到您的 yscale 中:

"zero":false
© www.soinside.com 2019 - 2024. All rights reserved.