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

如何在VegaLite中配置饼图区间并实现类似仪表盘效果?

用VegaLite实现区间划分的饼图仪表盘

完全可以通过数据转换和编码配置实现你要的效果,具体步骤如下:

  • 添加区间划分的计算字段
    使用transform中的calculate变换,根据x值生成区间标识字段:

    "transform": [
      {
        "calculate": "datum.x < 1 ? '0≤x<1' : 'x≥1'",
        "as": "range"
      }
    ]
    
  • 按区间聚合数据(按需)
    如果需要按区间统计x的累计值而非数据条数,添加聚合变换:

    "transform": [
      {
        "calculate": "datum.x < 1 ? '0≤x<1' : 'x≥1'",
        "as": "range"
      },
      {
        "aggregate": [{"op": "sum", "field": "x", "as": "total"}],
        "groupby": ["range"]
      }
    ]
    
  • 配置饼图/仪表盘样式
    基于生成的区间字段和聚合结果,配置弧形图的编码,同时调整角度范围模拟仪表盘效果:

    "mark": {"type": "arc", "innerRadius": 60, "outerRadius": 100},
    "encoding": {
      "theta": {
        "field": "total",
        "type": "quantitative",
        "stack": true,
        "scale": {"range": [1.5708, 4.7124]} // 对应半圆仪表盘的角度范围(π/2到3π/2)
      },
      "color": {"field": "range", "type": "nominal", "legend": null},
      "tooltip": [{"field": "range"}, {"field": "total", "format": ".2f"}]
    }
    

通过以上配置,就能将数据划分为0≤x<1和x≥1两个区间,生成类似截图的仪表盘展示效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:48:22