PowerBI Deneb中Vega代码编译正常却无可视化,如何解决?
PowerBI Deneb(Vega)流图无可视化输出排查
我是Vega新手,因需在PowerBI中制作流图且其他方法受约束不可行,遂尝试用Vega创建自定义视觉。目前代码编译正常,但无法生成可视化输出。已排查所用字段无空值,使用的是PowerBI的Deneb扩展,核心代码如下:
//Call schema for JSON// { "vega": { "$schema": "https://vega.github.io/schema/vega/v5.json", "description": "A flow map showing volume transfer between facilities.", "width": 600, "height": 400, "style": "cell", "padding": 5, //Define source data// "data": [ { "name": "dataset", "transform": [ { "type": "project", "projection": "projection", "as": ["x", "y"], "fields": [{"field": "Origin Longitude"}, {"field": "Origin Latitude"}] }, { "type": "project", "projection": "projection", "as": ["x2", "y2"], "fields": [{"field": "Receiving Longitude"}, {"field": "Receiving Latitude"}] }, { "type": "linkpath", "shape": "line", "sourceX": {"expr": "datum.x"}, "sourceY": {"expr": "datum.y"}, "targetX": {"expr": "datum.x2"}, "targetY": {"expr": "datum.y2"} } ] } ], "projections": [{"name": "projection", "type": "alberson"}], "marks": [ { "type": "path", "from": {"data": "dataset"}, "encode": { "enter": { "path": {"field": "path"}, "stroke": {"value": "steelblue"}, "strokeWidth": {"field": "Sheet1_Volume Transfer"}, "tooltip": {"signal": "{'Origin': datum['Origin Facility'], 'Recipient': datum['Receiving Facility'], 'Volume': datum['sheet1_Volume Transfer']}"} } } }, { "type": "symbol", "from": {"data": "dataset"}, "encode": { "enter": { "x": {"field": "x"}, "y": {"field": "y"}, "size": {"value": 50}, "fill": {"value": "blue"}, "tooltip": {"signal": "{'Facility': datum['Origin Facility']}"} } } }, { "type": "symbol", "from": {"data": "dataset"}, "encode": { "enter": { "x": {"field": "x2"}, "y": {"field": "y2"}, "size": {"value": 50}, "fill": {"value": "red"}, "tooltip": {"signal": "{'Facility': datum['Receiving Facility']}"} } } } ] } }
以下是可能导致无输出的原因:
- 投影类型错误:使用的
alberson不是Vega支持的投影类型,正确的阿尔伯斯投影应该是albers。无效投影会导致经纬度无法转换为画布坐标,所有元素无法渲染。 - 字段名大小写不匹配:代码中
strokeWidth引用Sheet1_Volume Transfer,但tooltip里写的是sheet1_Volume Transfer,PowerBI字段名大小写敏感,不一致会导致字段无法识别,比如线宽被解析为0,导致线条不可见。 - 投影与数据转换顺序问题:
projections定义在data之后,虽然Vega可能允许,但投影初始化晚于数据转换,可能导致经纬度转换失败。建议将projections移到data配置之前。 - linkpath转换冗余且可能出错:如果只是绘制直线,无需
linkpath转换,直接在path mark中用x/x2、y/y2定义路径即可。该转换可能未生成有效的path字段,导致path mark无内容。 - 投影未配置必要参数:Albers投影需要适配你的经纬度范围,默认参数可能导致所有点被渲染到画布外。可以添加
scale和translate参数,或者设置fit让Vega自动适配数据范围。
内容的提问来源于stack exchange,提问作者Ian Normoyle
相关产品推荐
相关产品推荐

