我有一个Vega-Lite bar chart和数据标签的条形图.问题是当 colored颜色 是动态的时,文本变得低对比度和不可读.我想给正文加一个白色的轮廓.然而,如果我只是将"stroke":"white"设置为文本标记,笔划会与字体重叠,使其看起来很小:
我希望笔划只在文本填充的外部.有可能吗?
我有一个Vega-Lite bar chart和数据标签的条形图.问题是当 colored颜色 是动态的时,文本变得低对比度和不可读.我想给正文加一个白色的轮廓.然而,如果我只是将"stroke":"white"设置为文本标记,笔划会与字体重叠,使其看起来很小:
我希望笔划只在文本填充的外部.有可能吗?
一个更好的技术是查看亮度,如这里所述:https://github.com/Giammaria/Vega-Lite-Techniques?tab=readme-ov-file#luminance-to-determine-font-color
如果你想用一个笔划,那么这个可以让你接近.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Vega-Lite bar chart from https://observablehq.com/@d3/learn-d3-scales.",
"width": 400,
"data": {
"values": [
{"name": "🍊", "count": 21},
{"name": "🍇", "count": 13},
{"name": "🍏", "count": 8},
{"name": "🍌", "count": 5},
{"name": "🍐", "count": 3},
{"name": "🍋", "count": 2},
{"name": "🍎", "count": 1},
{"name": "🍉", "count": 1}
]
},
"encoding": {
"y": {"field": "name", "type": "nominal", "sort": "-x", "title": null},
"x": {"field": "count", "type": "quantitative", "title": null}
},
"layer": [
{
"mark": "bar",
"encoding": {
"color": {
"field": "count",
"type": "quantitative",
"title": "Number of fruit"
}
}
},
{
"mark": {
"type": "text",
"align": "right",
"xOffset": -4,
"aria": false,
"fill": "transparent",
"stroke": "white",
"fontSize": 12,
"fontWeight": "bold",
"strokeWidth": 1.5
},
"encoding": {"text": {"field": "count", "type": "quantitative"}}
},
{
"mark": {
"type": "text",
"align": "right",
"xOffset": -4,
"aria": false,
"fill": "black",
"fontSize": 12
},
"encoding": {"text": {"field": "count", "type": "quantitative"}}
}
]
}