【发布时间】:2021-08-16 19:23:35
【问题描述】:
我的示例源代码如下
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 302},
{"a": "B", "b": 2794},
{"a": "C", "b": 96237},
{"a": "D", "b": 766995},
{"a": "E", "b": 7691230},
{"a": "F", "b": 59755899},
{"a": "G", "b": 229910863},
{"a": "H", "b": 9342989068},
{"a": "I", "b": 19617657788},
{"a": "J", "b": 140800000001}
]
},
"encoding": {
"x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
"y": {"field": "b", "type": "quantitative"}
},
"layer": [{
"mark": "bar"
},
{
"mark":{
"type":"text",
"align":"center",
"baseline":"middle",
"dx":0,
"dy":-5
} ,
"encoding":{
"text":{"field":"b","type":"quantitative"}
}
}
]
}
我希望 Vega-lite 按照逻辑为文本标记生成标签
if absoluteValue(b)>1 and if absoluteValue(b)<999 then format""
else
if absoluteValue(b)>1000 and if absoluteValue(b)<9999 then format.2s
else
if absoluteValue(b)>10000 and if absoluteValue(b)<99999 then format.3s
else
if absoluteValue(b)>100000 and if absoluteValue(b)<999999 then format.4s
else
if absoluteValue(b)>1000000 and if absoluteValue(b)<9999999 then format.2s
else
if absoluteValue(b)>10000000 and if absoluteValue(b)<99999999 then format.3s
else
if absoluteValue(b)>100000000 and if absoluteValue(b)<999999999 then format.4s
else
if absoluteValue(b)>1000000000 and if absoluteValue(b)<9999999999 then format.2s
else
if absoluteValue(b)>10000000000 and if absoluteValue(b)<99999999999 then format.3s
else
format.4s
以下是我想要的结果
| a | b | desired format |
|--- |-------------- |---------------- |
| A | 302 | 302 |
| B | 2794 | 2.8k |
| C | 96237 | 96.2k |
| D | 766995 | 767.0k |
| E | 7691230 | 7.7M |
| F | 59755899 | 59.8M |
| G | 229910863 | 229.9M |
| H | 9342989068 | 9.3G |
| I | 19617657788 | 19.6G |
| | 140800000001 | 140.8G |
Wahab Memon 在另一个问题Vega-lite to show text values in SI units 中向我展示了如何使用条件语句,但我不知道如何将其扩展到多个语句。我也找不到任何证明它的文档。我想知道是否可以使用if 或switch 语句来实现这一点。
有人可以指点我正确的方向吗?提前谢谢你!!!
【问题讨论】:
标签: vega-lite