我有一个Vega-Lite bar chart和数据标签的条形图.问题是当 colored颜色 是动态的时,文本变得低对比度和不可读.我想给正文加一个白色的轮廓.然而,如果我只是将"stroke":"white"设置为文本标记,笔划会与字体重叠,使其看起来很小:

enter image description here

我希望笔划只在文本填充的外部.有可能吗?

推荐答案

一个更好的技术是查看亮度,如这里所述:https://github.com/Giammaria/Vega-Lite-Techniques?tab=readme-ov-file#luminance-to-determine-font-color

如果你想用一个笔划,那么这个可以让你接近.

enter image description here

{
  "$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"}}
    }
  ]
}

Json相关问答推荐

使用jolt删除空对象

如何使用JQ有条件 Select 值

时间序列的Vega Lite分组条形图

从Postgres表中的JSON中提取值

如何在JSONata对象中迭代并向数组添加新字段?

使用Kotlin限制序列化类属性的允许整数值

将 REST API - json 输出转换为表 Power BI

使用 jq 重新格式化 JSON 输出

Golang jsonrpc2 服务器在哪里监听?

将不带正文的 CURL POST 转换为 RESTRequest Delphi 代码 / 为 Dropbox API /get_current_account 端点编写 Delphi 代码

Vega-Lite规范:尽管在规范中提供了数据,但显示空图表

如何使用 jq 在连续的 json 记录流上调用操作

jq json - 按键名 Select

如何在linux中用jq过滤json数组?

字典和对象的模型创建问题

C#扁平化json struct

Spring Security 和 JSON 身份验证

如何使用 SwiftyJSON 将字符串转换为 JSON

你如何在 Arrays of Arrays 上 OPENJSON

添加json文件注释