如何在 Vega LIte 中创建相对标记位置
How to create relative mark positions in Vega LIte
我有一个饼图,其宽度和高度设置为“容器”。我想给每一片馅饼贴上标签。因此,我包含了一个层,它创建了正确的文本。但是,我不知道如何实现相对半径大小。你会怎么做?
使用绝对半径(例如 30)可以,但我需要一个相对位置。
`"layer": [{"mark": {"type": "arc"}},
{"mark": {"type": "text", "radius":30},
"encoding": {"text": {"field": "*", "type": "nominal"}}
}]`
您可以使用 Expression Reference 作为半径值来创建相对半径。
比如这里有一个图表,饼图的半径设置为图表大小的40%,文字设置为图表大小的45%(这里我选择测量图表大小作为宽度和高度的最小值,这对于圆形图表来说似乎是合理的):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple pie chart with labels.",
"data": {
"values": [
{"category": "a", "value": 4},
{"category": "b", "value": 6},
{"category": "c", "value": 10},
{"category": "d", "value": 3},
{"category": "e", "value": 7},
{"category": "f", "value": 8}
]
},
"encoding": {
"theta": {"field": "value", "type": "quantitative", "stack": true}
},
"layer": [
{
"mark": {"type": "arc", "radius": {"expr": "0.4 * min(width, height)"}},
"encoding": {"color": {"field": "category", "type": "nominal", "legend": null}}
},
{
"mark": {"type": "text", "radius": {"expr": "0.45 * min(width, height)"}},
"encoding": {"text": {"field": "category", "type": "nominal"}}
}
],
"view": {"stroke": null}
}
我有一个饼图,其宽度和高度设置为“容器”。我想给每一片馅饼贴上标签。因此,我包含了一个层,它创建了正确的文本。但是,我不知道如何实现相对半径大小。你会怎么做?
使用绝对半径(例如 30)可以,但我需要一个相对位置。
`"layer": [{"mark": {"type": "arc"}},
{"mark": {"type": "text", "radius":30},
"encoding": {"text": {"field": "*", "type": "nominal"}}
}]`
您可以使用 Expression Reference 作为半径值来创建相对半径。
比如这里有一个图表,饼图的半径设置为图表大小的40%,文字设置为图表大小的45%(这里我选择测量图表大小作为宽度和高度的最小值,这对于圆形图表来说似乎是合理的):
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple pie chart with labels.",
"data": {
"values": [
{"category": "a", "value": 4},
{"category": "b", "value": 6},
{"category": "c", "value": 10},
{"category": "d", "value": 3},
{"category": "e", "value": 7},
{"category": "f", "value": 8}
]
},
"encoding": {
"theta": {"field": "value", "type": "quantitative", "stack": true}
},
"layer": [
{
"mark": {"type": "arc", "radius": {"expr": "0.4 * min(width, height)"}},
"encoding": {"color": {"field": "category", "type": "nominal", "legend": null}}
},
{
"mark": {"type": "text", "radius": {"expr": "0.45 * min(width, height)"}},
"encoding": {"text": {"field": "category", "type": "nominal"}}
}
],
"view": {"stroke": null}
}