Power BI中创建双层环形图:需自定义视觉或Deneb(VegaLite)方案
实现Power BI双层环形图(自定义视觉/Deneb方案)
给定数据集
| Year | Category | value |
|---|---|---|
| 2021 | Category 1 | 25 |
| 2021 | Category 2 | 50 |
| 2021 | Category 3 | 25 |
| 2022 | Category 1 | 50 |
| 2022 | Category 2 | 25 |
| 2022 | Category 3 | 25 |
预期效果:双层同心环形图,内外层分别对应2021、2022两个年份,每个环形按类别划分扇区,展示对应数值的占比情况。
方案一:使用Power BI自定义视觉实现
- 打开Power BI Desktop,点击「主页」选项卡的「获取视觉对象」→「从AppSource获取」。
- 搜索并添加支持双层环形图的自定义视觉(例如Donut Chart Dual类工具)。
- 导入你的数据集到Power BI,确认
Year、Category、value字段已正常加载。 - 将自定义视觉添加到报表画布后,配置字段:
- 把
Year拖到环形分组字段(不同视觉工具的字段命名可能不同,比如「Ring Group」); - 把
Category拖到类别字段; - 把
value拖到值字段。
- 把
- 根据需求调整样式:设置环形厚度、类别配色、数据标签显示等。
方案二:使用Deneb(Vega-Lite)实现
1. 数据准备
确保数据集已导入Power BI,字段结构与给定表格一致。
2. Deneb视觉配置
添加Deneb视觉到报表画布后,点击「编辑」进入Vega-Lite编辑器,替换为以下代码:
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "description": "双层环形图展示不同年份各类别数值占比", "data": {"name": "dataset"}, "transform": [ { "aggregate": [{"op": "sum", "field": "value", "as": "total_value"}], "groupby": ["Year", "Category"] }, { "window": [{"op": "sum", "field": "total_value", "as": "year_total"}], "groupby": ["Year"] }, { "calculate": "datum.total_value / datum.year_total", "as": "percentage" } ], "layer": [ { "mark": {"type": "arc", "innerRadius": 60, "outerRadius": 100}, "encoding": { "theta": {"field": "percentage", "type": "quantitative", "stack": true}, "color": {"field": "Category", "type": "nominal", "legend": {"title": "类别"}}, "opacity": {"condition": {"param": "hover", "value": 1}, "value": 0.7}, "tooltip": [ {"field": "Year", "title": "年份"}, {"field": "Category", "title": "类别"}, {"field": "total_value", "title": "数值"}, {"field": "percentage", "title": "占比", "format": ".0%"} ] }, "params": [{"name": "hover", "select": {"type": "point", "on": "mouseover"}}], "transform": [{"filter": "datum.Year == '2021'"}] }, { "mark": {"type": "arc", "innerRadius": 20, "outerRadius": 50}, "encoding": { "theta": {"field": "percentage", "type": "quantitative", "stack": true}, "color": {"field": "Category", "type": "nominal"}, "opacity": {"condition": {"param": "hover", "value": 1}, "value": 0.7}, "tooltip": [ {"field": "Year", "title": "年份"}, {"field": "Category", "title": "类别"}, {"field": "total_value", "title": "数值"}, {"field": "percentage", "title": "占比", "format": ".0%"} ] }, "transform": [{"filter": "datum.Year == '2022'"}] } ], "config": { "arc": {"stroke": "white", "strokeWidth": 2}, "legend": {"orient": "bottom"} } }
3. 字段绑定
在Deneb视觉的字段面板中,将:
Year字段拖入「Dataset」区域;Category字段拖入「Dataset」区域;value字段拖入「Dataset」区域。
保存配置后,即可生成符合预期的双层环形图。
内容的提问来源于stack exchange,提问作者Uday G Raikar
相关产品推荐
相关产品推荐

