我已经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"
}
}
}
}
]
}