如何在哑铃图上显示两个值之间的绝对差

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

我正在 Deneb/Power BI 中绘制哑铃图,我想显示连接线上方每个组之间的差异(绝对值),如下所示:

enter image description here

我尝试使用计算差异的 PBI 度量来执行此操作,但是当我尝试实现它时,每个圆圈都标有值,而不是标有单个值的线。那么也许最好在 Deneb 内进行此计算?我只是不知道该怎么做。

这是 PBI 文件的链接: https://drive.google.com/file/d/1_AG5jF6PV1eHc2A4QMSSk2iXm2TRZVc3/view?usp=drive_link

生成下图的代码是:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Dumbbell chart with labels",
  "data":  {"name": "dataset"},
 
  "encoding": {
    "x": {
      "field": "Value",
      "type": "quantitative",
      "title": "Factor Value"//,
    },
    "y": {
      "field": "Categories",
      "type": "nominal",
      "title": null//,
    }
  },

  "layer": [
        {
      "mark": "line",
      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "color": {"value": "gray"}
      }
    },

    {
      "mark": {
        "type": "point",
        "filled": true
      },
      "encoding": {
        "color": {
          "field": "Factor",
          "type": "ordinal",
           "legend": {
                 "title": null,
                 "offset": 0
            },
          "scale": {
            "domain": ["One", "Two"],
            "range": ["#6ba5cd", "#8e946a"]
          },
          "title": "Legend"
        },
        "size": {"value": 300},
        "opacity": {"value": 1}
      }
    }
  ]
}
powerbi vega-lite vega deneb
1个回答
0
投票

好吧,我能弄清楚这一点。解决方案在这个文件中: https://drive.google.com/file/d/1pGR6s_Z7kAvCvCj7lKNHMIYpTDBmoj3j/view?usp=drive_link

代码在这里:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Dumbbell chart with labels",
  "data":  {"name": "dataset"},

  "encoding": {
    "x": {
      "field": "Value",
      "type": "quantitative",
      "title": "Factor Value"//,
    },
    "y": {
      "field": "Categories",
      "type": "nominal",
      "title": null//,
    }
  },
  
  "layer": [
        {
      "mark": "line",
      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "color": {"value": "gray"}
      }
    },

    {
      "mark": {
        "type": "point",
        "filled": true
      },
      "encoding": {
        "color": {
          "field": "Factor",
          "type": "ordinal",
           "legend": {
                 "title": null,
                 "offset": 0
            },
          "scale": {
            "domain": ["One", "Two"],
            "range": ["#6ba5cd", "#8e946a"]
          },
          "title": "Legend"
        },
        "size": {"value": 300},
        "opacity": {"value": 1}
      }
    },
     {
      "mark": {
        "type": "text",
        "align": "center",
        "dy": -10,
        "fontSize": 12
      },
      "encoding": {
        "text": {
          "field": "Difference",
          "type": "quantitative",
          "format": ".2f"
        },
        "x": {
          "field": "Midpoint",
          "type": "quantitative"
        },
        "y": {
          "field": "Categories",
          "type": "nominal"
        },
        "color": {
          "value": "black"
        }
      },
      "transform": [
        {
          "aggregate": [
            {"op": "max", "field": "Value", "as": "Value_Two"},
            {"op": "min", "field": "Value", "as": "Value_One"}
          ],
          "groupby": ["Categories"]
        },
        {
          "calculate": "(datum.Value_Two + datum.Value_One) / 2",
          "as": "Midpoint"
        },
        {
          "calculate": "abs(datum.Value_Two - datum.Value_One)",
          "as": "Difference"
        }
      ]
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.