Hi - I am struggling quite a bit with getting my labels to be placed dynamically “inside base” - My current visual looks like this:
What I need is the text labels to be in the center of each block in the column. How do I do this? - My code is below:
{
"data": {"name": "dataset"},
"transform": [
{
"calculate": "datum['ProductionOutput(Supply)'] >= 1000000 ? format(datum['ProductionOutput(Supply)'] / 1000000, '') + 'M' : datum['ProductionOutput(Supply)'] >= 1000 ? format(datum['ProductionOutput(Supply)'] / 1000, '') + 'K' : format(datum['ProductionOutput(Supply)'], '')",
"as": "formattedSupply"
},
{
"lookup": "Legend",
"from": {
"data": {"name": "dataset"},
"key": "Legend",
"fields": ["Hex_color_code"]
}
}
],
"layer": [
{
"mark": {
"type": "bar",
"stroke": "black",
"strokeWidth": 1
},
"encoding": {
"x": {
"field": "Year_Short",
"type": "ordinal",
"sort": {
"field": "Rank",
"op": "sum",
"order": "ascending"
},
"axis": {
"labelAngle": 0,
"labelFontWeight": "bold",
"labelColor": "black",
"title": null
}
},
"y": {
"field": "ProductionOutput(Supply)",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": null,
"labelExpr": "datum.value >= 1000000 ? format(datum.value / 1000000, '') + 'M' : datum.value >= 1000 ? format(datum.value / 1000, '') + 'K' : format(datum.value, '')",
"tickCount": 10,
"labelFontWeight": "bold",
"labelColor": "black"
}
},
"color": {
"field": "Hex_color_code",
"type": "nominal",
"scale": null
},
"detail": {
"field": "Legend",
"type": "nominal"
},
"order": {
"field": "Rank",
"type": "quantitative"
},
"strokeDash": {
"condition": {
"test": "datum['Capacity_type'] == 'REQUIRED CAPACITY'",
"value": [5, 5]
},
"value": [1, 0]
},
"tooltip": [
{"field": "Year", "type": "ordinal", "title": "Year"},
{"field": "Type_of_supply", "type": "nominal", "title":"Type of supply"},
{"field": "Capacity_type", "type": "nominal", "title":"Capacity type"},
{"field": "formattedSupply", "type": "nominal", "title": "Supply"},
{"field": "Supply version", "type": "nominal", "title":"Supply version"}
]
}
},
{
"mark": {
"type": "text",
"fontSize": 6,
"align": "center",
"baseline": "middle"
},
"encoding": {
"x": {"field": "Year_Short", "type": "ordinal"},
"y": {"field": "ProductionOutput(Supply)", "type": "quantitative", "stack": "zero", "band": 0.5},
"text": {"field": "formattedSupply", "type": "nominal"},
"detail": {"field": "Legend", "type": "nominal"},
"order": {
"field": "Rank",
"type": "quantitative"
}
}
}
]
}