我已经try 了ChatGPT提出的所有建议以及我能想到的每一种变体,以使以下规范发挥作用,以便文本值将改变图表顶部的 colored颜色 . 如果您使用vega数据查看工具,您可以看到 for each 日期定义了价格/预算值,您还可以将一个或所有值显示为文本,但无论默认 colored颜色 是什么,文本始终是彩色的(本例中为蓝色) 即使使用1=1也无法正确计算.

idea ?

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "Monthly stock prices of a Tech Company.",
  "background": "white",
  "padding": 5,
  "width": 400,
  "height": 200,
  "data": [
    {
      "name": "source_0",
      "values": [
        {"date": "2021-01-01", "symbol": "AAPL", "price": 150, "budget": 110},
        {"date": "2021-02-01", "symbol": "AAPL", "price": 100, "budget": 50},
        {"date": "2022-03-01", "symbol": "AAPL", "price": 90, "budget": 90},
        {"date": "2022-04-01", "symbol": "AAPL", "price": 130, "budget": 150}
      ],
      "transform": [
        {
          "type": "filter",
          "expr": "datum.symbol === 'AAPL'"
        },
        {
          "type": "formula",
          "expr": "slice(datum.date, 0, 7)",
          "as": "year_month"
        }
        ,
        {
          "type": "formula",
            "expr": "(datum.price>=datum.budget) ? 1 : 0"
            ,"as":"isgreen"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {"data": "source_0", "field": "year_month"},
      "range": "width",
      "padding": 0.1
    },
    {
      "name": "y",
      "type": "linear",
      "domain": [0, 200],
      "range": [200, 0],
      "nice": true
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "title": "Month"
    },
    {
      "scale": "y",
      "orient": "left",
      "title": "Price ($)"
    }
  ],
  "marks": [
    {
      "type": "text",
      "from": {"data": "source_0"},
      "encode": {
        "enter": {
          "x": {"scale": "x", "field": "year_month", "band": 0.5},
          "y": {"scale": "y", "field": "budget"},
         "text": {"field":"budget"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"} 
           , "fill": {
          "condition": {
            "test": "1=1",
            "value": "green"
          },
          "value": "blue"
        }

        
      }
      }
    }
  ]
}

推荐答案

使用信号更容易.

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "Monthly stock prices of a Tech Company.",
  "background": "white",
  "padding": 5,
  "width": 400,
  "height": 200,
  "data": [
    {
      "name": "source_0",
      "values": [
        {"date": "2021-01-01", "symbol": "AAPL", "price": 150, "budget": 110},
        {"date": "2021-02-01", "symbol": "AAPL", "price": 100, "budget": 50},
        {"date": "2022-03-01", "symbol": "AAPL", "price": 90, "budget": 90},
        {"date": "2022-04-01", "symbol": "AAPL", "price": 130, "budget": 150}
      ],
      "transform": [
        {"type": "filter", "expr": "datum.symbol === 'AAPL'"},
        {
          "type": "formula",
          "expr": "slice(datum.date, 0, 7)",
          "as": "year_month"
        },
        {
          "type": "formula",
          "expr": "(datum.price>=datum.budget) ? 1 : 0",
          "as": "isgreen"
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "domain": {"data": "source_0", "field": "year_month"},
      "range": "width",
      "padding": 0.1
    },
    {
      "name": "y",
      "type": "linear",
      "domain": [0, 200],
      "range": [200, 0],
      "nice": true
    }
  ],
  "axes": [
    {"scale": "x", "orient": "bottom", "title": "Month"},
    {"scale": "y", "orient": "left", "title": "Price ($)"}
  ],
  "marks": [
    {
      "type": "text",
      "from": {"data": "source_0"},
      "encode": {
        "enter": {
          "x": {"scale": "x", "field": "year_month", "band": 0.5},
          "y": {"scale": "y", "field": "budget"},
          "text": {"field": "budget"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "fill": {
            "signal":  "datum.isgreen==1?'green':'blue'",
          
          }
        }
      }
    }
  ]
}

Json相关问答推荐

手动解开没有可编码的SON- Swift

Jolt转换问题—使用键查找匹配对象

JOLT将对象名作为新属性添加到主体中

如何使用JQ将JSON字符串替换为解析后的类似功能?

Azure Data Factory JSON输出格式问题

如何使用JQ打印每个根级对象键一行?

PowerShell女士:如何处理json对象?

VBA json按特定属性名称提取所有数据

Moshi:序列化 List 时出现问题

带有 API 测试的 Typescript JSON 模式验证

jq: Select 何时来自另一个数组的值与此 json 中的值匹配

无法使用 vue.js 访问 JSON 数组的项目

golang递归json来构造?

使用 Jolt 变换将平面 json 转换为具有多个数组的嵌套 Json

在 json 嵌入的 YAML 文件中 - 使用 Python 仅替换 json 值

SyntaxError:Object.parse(本机)AngularJS中的意外标记o

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符串?

区分字符串和列表的 Pythonic 方式是什么?

使用 axios 在 POST multipart/form-data 请求中发送文件和 json