admin管理员组文章数量:1201558
I am trying to add a gradient to a bar chart in vega lite. I've managed to plot everything else okay, but after having tried a few things, I've just not been able to add a gradient. The gradient should be white to red for not booked The gradient should be white to green for booked
This is what I have currently:
{
"data": {
"name": "dataset"
},
"transform": [
{
"fold": ["Booked", "Not Booked"],
"as": ["Booking Status", "Value"]
}
],
"mark": {
"type": "bar",
"cornerRadiusEnd": 10
},
"encoding": {
"x": {
"field": "Event Due Date",
"type": "ordinal",
"timeUnit": "month",
"title": "",
"sort": "x"
},
"y": {
"field": "Value",
"aggregate": "sum",
"type": "quantitative",
"title": ""
},
"color": {
"field": "Booking Status",
"type": "nominal",
"scale": {
"domain": ["Booked", "Not Booked"],
"range": ["green", "red"]
},
"title": "Booking Status"
},
"xOffset": {
"field": "Booking Status"
}
}
}
I've tried to add a gradient with stops in the range, related to the scale in the colour block. It seems this is not possible. I've tried to add a layer over the top, but this does not seem to work either.
I am trying to add a gradient to a bar chart in vega lite. I've managed to plot everything else okay, but after having tried a few things, I've just not been able to add a gradient. The gradient should be white to red for not booked The gradient should be white to green for booked
This is what I have currently:
{
"data": {
"name": "dataset"
},
"transform": [
{
"fold": ["Booked", "Not Booked"],
"as": ["Booking Status", "Value"]
}
],
"mark": {
"type": "bar",
"cornerRadiusEnd": 10
},
"encoding": {
"x": {
"field": "Event Due Date",
"type": "ordinal",
"timeUnit": "month",
"title": "",
"sort": "x"
},
"y": {
"field": "Value",
"aggregate": "sum",
"type": "quantitative",
"title": ""
},
"color": {
"field": "Booking Status",
"type": "nominal",
"scale": {
"domain": ["Booked", "Not Booked"],
"range": ["green", "red"]
},
"title": "Booking Status"
},
"xOffset": {
"field": "Booking Status"
}
}
}
I've tried to add a gradient with stops in the range, related to the scale in the colour block. It seems this is not possible. I've tried to add a layer over the top, but this does not seem to work either.
Share Improve this question asked Jan 21 at 15:58 SeannyyboyyyyySeannyyboyyyyy 353 bronze badges1 Answer
Reset to default 1I would generally be opposed to using gradients in a bar chart instead of just using solid colors for the bars. But if you must, the code below will do so (Open the Chart in the Vega Editor). The idea is you want two layers, filtered to show opposite parts of the data, each of which uses the correct gradient for that filtered data. (I am using the Vega editor’s sample data here; you can easily adapt this to your own.)
Result:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "data/seattle-weather.csv"},
"layer": [
{
"transform": [{"filter": "month(datum.date) % 2 == 0"}],
"mark": {
"type": "bar",
"color": {
"x1": 1,
"y1": 1,
"x2": 1,
"y2": 0,
"gradient": "linear",
"stops": [
{"offset": 0, "color": "white"},
{"offset": 1, "color": "red"}
]
}
},
"encoding": {
"x": {"timeUnit": "month", "field": "date", "type": "temporal"},
"y": {"aggregate": "mean", "field": "precipitation"}
}
},
{
"transform": [{"filter": "month(datum.date) % 2 == 1"}],
"mark": {
"type": "bar",
"color": {
"x1": 1,
"y1": 1,
"x2": 1,
"y2": 0,
"gradient": "linear",
"stops": [
{"offset": 0, "color": "green"},
{"offset": 1, "color": "red"}
]
}
},
"encoding": {
"x": {"timeUnit": "month", "field": "date", "type": "temporal"},
"y": {"aggregate": "mean", "field": "precipitation"}
}
}
]
}
本文标签: jsonAdd gradient to bar chart in vega liteStack Overflow
版权声明:本文标题:json - Add gradient to bar chart in vega lite - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738619545a2103110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论