Power BI Deneb双面积图:数值格式化与极值高亮需求
Power BI Deneb双面积图问题解决方案
问题1:实现全数值范围的K格式显示(含1000以下)
原代码中.2s格式仅对1000以上数值生效,要让所有数值(如700→0.7K)都显示为x.xK格式,需通过自定义表达式生成文本:
- 替换原text编码中的
text字段,用expr将数值除以1000后格式化并拼接后缀:
"text": {"expr": "format(datum['Variable 2'] / 1000, '.1f') + 'K'"}
对Variable 1的文本标记做同样修改即可。
问题2:为最大值/最小值标注添加颜色区分
在文本标记的color属性中添加条件判断,匹配字段最大值时设为绿色,最小值设为红色,其余保持黑色:
- 修改text标记的
color配置为条件表达式:
"color": { "condition": [ {"test": "datum['Variable 2'] == max('Variable 2')", "value": "#2ecc71"}, {"test": "datum['Variable 2'] == min('Variable 2')", "value": "#e74c3c"} ], "value": "#000000" }
对Variable 1的文本标记做同样修改即可。
修改后的完整代码
{ "data": {"name": "dataset"}, "layer": [ { "mark": { "type": "area", "line": {"color": "#063970"}, "color": { "x1": 1, "y1": 1, "gradient": "linear", "stops": [ {"offset": 0, "color": "white"}, {"offset": 1, "color": "#063970"} ] } } }, { "mark": { "type": "area", "line": {"color": "#2596be"}, "color": { "x1": 1, "y1": 1, "gradient": "linear", "stops": [ {"offset": 0, "color": "white"}, {"offset": 1, "color": "#2596be"} ] } }, "encoding": { "y": {"field": "Variable 1", "type": "quantitative"} } }, { "mark": { "type": "text", "yOffset": -10, "size": 10 }, "encoding": { "text": {"expr": "format(datum['Variable 2'] / 1000, '.1f') + 'K'"}, "opacity": { "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1}, "value": 1 }, "y": {"field": "Variable 2", "type": "quantitative", "axis": null}, "color": { "condition": [ {"test": "datum['Variable 2'] == max('Variable 2')", "value": "#2ecc71"}, {"test": "datum['Variable 2'] == min('Variable 2')", "value": "#e74c3c"} ], "value": "#000000" } } }, { "mark": { "type": "text", "yOffset": -10, "size": 10 }, "encoding": { "text": {"expr": "format(datum['Variable 1'] / 1000, '.1f') + 'K'"}, "opacity": { "condition": {"test": {"field": "MONTH", "equal": "off"}, "value": 0.1}, "value": 1 }, "y": {"field": "Variable 1", "type": "quantitative", "axis": null}, "color": { "condition": [ {"test": "datum['Variable 1'] == max('Variable 1')", "value": "#2ecc71"}, {"test": "datum['Variable 1'] == min('Variable 1')", "value": "#e74c3c"} ], "value": "#000000" } } } ], "encoding": { "x": { "field": "MONTH", "type": "ordinal", "axis": {"labelPadding": 0}, "title": null }, "y": { "field": "Variable 2", "type": "quantitative", "axis": null } } }
内容的提问来源于stack exchange,提问作者userrr
相关产品推荐
相关产品推荐

