Hi,
I’ve tweaked the code very basically so the axis starts at Zero, this is because we want to display all positive NPS scores on one graph and the negatives on the other.
How do I amend the below code to flip the x-axis values (both the location & number) from horizontal to vertical alignment?
{
"data": {"name": "dataset"},
"transform": [
{
"stack": "No of Responses",
"sort": [
{
"field": "NPS Score",
"order": "descending"
}
],
"as": ["v1", "v2"],
"groupby": []
},
{
"calculate": "datum['v2'] - ((datum['v2'] - datum['v1']) / 2)",
"as": "category_mid"
}
],
"encoding": {
"x": {
"type": "quantitative",
"axis": null
},
"y": {
"type": "quantitative",
"axis": {"title": null},
"scale": {
"domain": [0, 100],
"range": [
{"expr": "height - 40"},
0
]
}
},
"tooltip": [
{
"field": "What is your primary location?",
"type": "nominal"
},
{
"field": "NPS Score",
"type": "quantitative",
"formatType": "pbiFormat",
"format": "#"
},
{
"field": "No of Responses",
"type": "quantitative"
}
]
},
"layer": [
{
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"x": {"field": "v1"},
"x2": {"field": "v2"},
"y": {"field": "NPS Score"},
"y2": {"datum": 0},
"color": {
"condition": [
{
"test": "datum['NPS Score'] <= 0",
"value": "#BA3A32"
}
],
"value": "#93B558"
}
}
},
{
"mark": {
"type": "text",
"baseline": "bottom"
},
"encoding": {
"x": {"field": "category_mid"},
"y": {
"value": {
"expr": "height - 15"
}
},
"text": {
"field": "What is your primary location?"
}
}
},
{
"mark": {
"type": "text",
"baseline": "bottom"
},
"encoding": {
"x": {"field": "category_mid"},
"y": {"value": "height"},
"text": {
"field": "No of Responses"
}
}
},
{
"transform": [
{
"calculate": "datum['NPS Score'] / 2",
"as": "y_midpoint"
}
],
"mark": {
"type": "text",
"color": "black"
},
"encoding": {
"text": {
"field": "NPS Score",
"formatType": "pbiFormat",
"format": "#"
},
"x": {"field": "category_mid"},
"y": {"field": "y_midpoint"}
}
}
]
}