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

能否在Vega-Lite(Deneb/PowerBI)中计算对称线?(非回归线)

在Deneb(Power BI的Vega-Lite可视化)中实现对称线与比率线

当然可以在Deneb中实现对称线(y=x)和自定义比率线。以下是具体实现方案,基于你提供的配置代码修改:

1. 对称线(y=x)的实现

对称线是y等于x的直线,我们可以通过生成覆盖x轴范围的序列点,再计算对应的y值来绘制。在Vega-Lite中,使用sequence变换生成x值,再用calculate得到y=x的对应值。

2. 比率线(自定义斜率)的实现

比率线是y=k*x的直线,只需在计算y值时指定斜率k即可(比如y=0.8x或y=1.2x),用于划分不同区间。

修改后的完整配置代码

{
  "data": {"name": "dataset"},
  "encoding": {
    "x": {
      "field": "Actual",
      "type": "quantitative",
      "scale": {"zero": false},
      "axis": {
        "format": "0%",
        "formatType": "pbiFormat"
      }
    },
    "y": {
      "field": "Estimate",
      "type": "quantitative",
      "scale": {"zero": false},
      "axis": {
        "format": "0%",
        "formatType": "pbiFormat"
      }
    },
    "color": {
      "field": "Sign",
      "type": "nominal",
      "scale": {
        "range": ["black", "red", "blue"]
      },
      "legend": null
    }
  },
  "layer": [
    {"mark": {"type": "point"}},
    // 保留原回归线配置
    {
      "mark": {
        "type": "line",
        "color": "firebrick"
      },
      "transform": [
        {
          "regression": "Estimate",
          "on": "Actual"
        }
      ],
      "encoding": {
        "x": {"field": "Actual", "type": "quantitative"},
        "y": {"field": "Estimate", "type": "quantitative"}
      }
    },
    // 对称线(y=x)
    {
      "mark": {
        "type": "line",
        "color": "gray",
        "strokeDash": [4, 2] // 虚线样式区分
      },
      "data": {"sequence": {"start": {"signal": "scale('x').domain[0]"}, "stop": {"signal": "scale('x').domain[1]"}, "step": 0.01}},
      "encoding": {
        "x": {"field": "data", "type": "quantitative"},
        "y": {"calculate": "datum.data", "type": "quantitative"}
      }
    },
    // 比率线示例:y=0.8x
    {
      "mark": {
        "type": "line",
        "color": "green",
        "strokeDash": [2, 2]
      },
      "data": {"sequence": {"start": {"signal": "scale('x').domain[0]"}, "stop": {"signal": "scale('x').domain[1]"}, "step": 0.01}},
      "encoding": {
        "x": {"field": "data", "type": "quantitative"},
        "y": {"calculate": "datum.data * 0.8", "type": "quantitative"}
      }
    },
    // 比率线示例:y=1.2x
    {
      "mark": {
        "type": "line",
        "color": "blue",
        "strokeDash": [2, 2]
      },
      "data": {"sequence": {"start": {"signal": "scale('x').domain[0]"}, "stop": {"signal": "scale('x').domain[1]"}, "step": 0.01}},
      "encoding": {
        "x": {"field": "data", "type": "quantitative"},
        "y": {"calculate": "datum.data * 1.2", "type": "quantitative"}
      }
    }
  ]
}

关键说明

  • 使用sequence生成覆盖当前x轴范围的连续数据点,signal scale('x').domain自动获取x轴的最小值和最大值,保证线条覆盖整个图表区域。
  • 通过calculate计算对应的y值:对称线用datum.data(即y=x),比率线用datum.data * k(k为自定义斜率)。
  • 用strokeDash设置虚线样式,方便区分不同线条类型;也可调整color属性自定义颜色。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 15:39:58