按折叠尺寸排序?

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

我在 PowerBI 中通过 Deneb 使用 Vega Lite。请参阅遵循我的图表的一般格式/定义的附加示例

PowerBI pbix 示例

{
  "data": {"name": "dataset"},
  "width": 500,
  "height": {"step": 15},
  "layer":[
  {
    "name": "BAR",
    "mark": {"type": "bar", "size":14},  
    
  "transform": [
    {
      "fold": [
        "Good %",
        "Bad %"
      ],
      "as": [
        "key",
        "value"
      ]
    },
    {
      "calculate": "indexof(['Good %', 'Bad %'], datum.key)",
      "as": "order"
    }
  ],
 
  "encoding": {
    
    "y": {
      "field": "Question",
      "type": "nominal",
      "axis": {
        "labelAlign": "right",
        "labelLimit": 400,
        "labelFont": "DIN",
        "labelFontSize": 10,
        "orient" : "left",
        "title": null,
        "labelColor": "white",
        "minExtent": 400
      }
    },
    "x": {
      "field": "value",
      "type": "quantitative",
      "title": null,
      "axis": {"grid": false, "ticks": false, "labels": false}
    },
    
    
    "color":{
    "field": "key",
    "type" : "nominal",
      "legend": null,
    "scale": {
        "domain": ["Bad %",  "Good %"],
        "range": ["#DE3E33",  "#1AA14D"]
              }
            },
    
     "order": {"field": "order", "type": "ordinal"}
  }
  },
  
    {"mark": {"type": "text", "align": "right", "x":-10},
      "encoding": {
        "y": {"field": "Question", "type": "nominal"},
        "color": {
         "condition": {"test": "datum['Good %'] >= 0.5", "value": "green"},
          "value": "red"
        },        
        "text": {"field": "Question", "type": "nominal"}}
    },

      {"mark": {"type": "text", "align": "left", "x":2},
      "encoding": {
        "y": {"field": "Question", "type": "nominal"},
        "color": {"value": "white"},
        "text": {"aggregate": "sum", "field": "Good %", "type": "quantitative", "format": ".0%"}}
    }
  ]
}

我有两个计算的度量,我使用“折叠”将它们组合起来生成一个堆叠条。在所附示例中,我想按“Good %”分数对问题进行排序。我已经尝试过“排序”指导,但顺序没有改变 - 我怀疑这是因为在所有情况下总 x 值都是 100%。

是否可以将排序顺序设置为折叠值之一(“Good %”)?

提前非常感谢!

powerbi visualization powerbi-desktop vega-lite deneb
1个回答
0
投票

这符合你的要求吗?我将您的措施重命名为“好”和“坏”以删除 % 符号。

{
  "data": {
    "name": "dataset"
  },
  "width": 500,
  "height": {
    "step": 15
  },
  "encoding": {
    "y": {
      "field": "Question",
      "type": "nominal",
      "sort": {
        "field": "Good",
        "op": "max",
        "order": "ascending"
      },
      "axis": {
        "labelAlign": "right",
        "labelLimit": 400,
        "labelFont": "DIN",
        "labelFontSize": 10,
        "orient": "left",
        "title": null,
        "labelColor": "white",
        "minExtent": 400
      }
    }
  },
  "layer": [
    {
      "name": "BAR",
      "mark": {
        "type": "bar",
        "size": 14
      },
      "transform": [
        {
          "fold": [
            "Bad",
            "Good"
          ],
          "as": [
            "key",
            "value"
          ]
        },
        {
          "calculate": "indexof(['Good %', 'Bad %'], datum.key)",
          "as": "order"
        }
      ],
      "encoding": {
        "x": {
          "field": "value",
          "type": "quantitative",
          "title": null,
          "axis": {
            "grid": false,
            "ticks": false,
            "labels": false
          }
        },
        "color": {
          "field": "key",
          "type": "nominal",
          "legend": null,
          "scale": {
            "domain": [
              "Good",
              "Bad"
            ],
            "range": [
              "#DE3E33",
              "#1AA14D"
            ]
          }
        },
        "order": {
          "field": "Bad",
          "type": "quantitative",
          "sort": "descending"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "right",
        "x": -10
      },
      "encoding": {
        "color": {
          "condition": {
            "test": "datum['Bad'] >= 0.5",
            "value": "green"
          },
          "value": "red"
        },
        "text": {
          "field": "Question",
          "type": "nominal"
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "left",
        "x": 2
      },
      "encoding": {
        "color": {
          "value": "white"
        },
        "text": {
          "aggregate": "sum",
          "field": "Bad",
          "type": "quantitative",
          "format": ".0%"
        }
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.