I am working on creating a funnel chart using vega lite but facing an issue while placing text at the center of each bar.
Below is the vega lite spec.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"y": {"field": "department", "type": "nominal", "sort": "-x"},
"x": {
"field": "daily_total_login",
"type": "quantitative",
"stack": "center"
}
},
"layer": [
{
"mark": {"tooltip": true, "type": "bar"},
"encoding": {
"x": {
"field": "daily_total_login",
"type": "quantitative",
"stack": "center"
},
"color": {"field": "department", "type": "nominal", "legend": null}
}
},
{
"mark": {"type": "text"},
"encoding": {
"text": {"field": "daily_total_login", "type": "quantitative"}
},
}
],
"data": {
"values": [
{
"date": "2024-08-09",
"department": "A",
"daily_total_login": 277,
"daily_active_user": 87
},
{
"date": "2024-08-10",
"department": "B",
"daily_total_login": 36,
"daily_active_user": 9
},
{
"date": "2024-08-11",
"department": "C",
"daily_total_login": 39,
"daily_active_user": 14
},
{
"date": "2024-08-12",
"department": "D",
"daily_total_login": 124,
"daily_active_user": 51
},
]
},
"height": "container",
"width": "container"
}
Could anyone please suggest what changes I could make in the above vega lite spec, so that the text could appear at the center of each bar?
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"y": {"field": "department", "type": "nominal", "sort": "-x"},
"x": {
"field": "daily_total_login",
"type": "quantitative",
"stack": "center"
}
},
"layer": [
{
"mark": {"tooltip": true, "type": "bar"},
"encoding": {
"color": {"field": "department", "type": "nominal", "legend": null}
}
},
{
"mark": {"type": "text"},
"encoding": {
"text": {"field": "daily_total_login", "type": "quantitative"},
"x": {
"datum":{"expr":"invert('x',width/2)"}
}
}
}
],
"data": {
"values": [
{
"date": "2024-08-09",
"department": "A",
"daily_total_login": 277,
"daily_active_user": 87
},
{
"date": "2024-08-10",
"department": "B",
"daily_total_login": 36,
"daily_active_user": 9
},
{
"date": "2024-08-11",
"department": "C",
"daily_total_login": 39,
"daily_active_user": 14
},
{
"date": "2024-08-12",
"department": "D",
"daily_total_login": 124,
"daily_active_user": 51
}
]
},
"height": "container",
"width": "container",
}