I am fairly new to Vega-Lite and am attempting to fulfil a request to create a rather unorthodox report, essentially combining 3 types of charts (stacked column, clustered column and line on 2nd Y axis). Whilst I got most of the layout desired: mock up
I wanted to know if there is a way to create a custom legend (and colors) to look something like this:
Any help would be greatly appreciated!!
You don't need a fold transform - just an explicit column value to refer to. e.g.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{
"Month": 1,
"Color": "Blue",
"Sales": 100,
"Sales LY": 90,
"Target": 95,
"Volume": 300
},
{
"Month": 1,
"Color": "Green",
"Sales": 560,
"Sales LY": 800,
"Target": 880,
"Volume": 1200
},
{
"Month": 2,
"Color": "Blue",
"Sales": 130,
"Sales LY": 170,
"Target": 190,
"Volume": 450
},
{
"Month": 2,
"Color": "Green",
"Sales": 600,
"Sales LY": 665,
"Target": 680,
"Volume": 1200
},
{
"Month": 3,
"Color": "Blue",
"Sales": 200,
"Sales LY": 150,
"Target": 165,
"Volume": 400
},
{
"Month": 3,
"Color": "Green",
"Sales": 750,
"Sales LY": 1000,
"Target": 1100,
"Volume": 1200
},
{
"Month": 4,
"Color": "Blue",
"Sales": 80,
"Sales LY": 105,
"Target": 120,
"Volume": 500
},
{
"Month": 4,
"Color": "Green",
"Sales": 800,
"Sales LY": 600,
"Target": 660,
"Volume": 1200
}
]
},
"transform": [{"calculate": "'Volume'", "as": "Volume Legend"}],
"width": 300,
"height": 300,
"layer": [
{
"name": "SALES",
"mark": {"type": "bar", "xOffset": 0, "size": 10, "color": "#81c784"},
"encoding": {
"x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
"y": {"field": "Sales", "type": "quantitative"},
"color": {"field": "Color", "type": "nominal"}
}
},
{
"name": "SALES LY",
"mark": {"type": "bar", "xOffset": -11, "size": 10, "color": "#1e88e5"},
"encoding": {
"x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
"y": {"field": "Sales LY", "type": "quantitative", "axis": null},
"color": {"field": "Color", "type": "nominal"}
}
},
{
"name": "TARGET",
"mark": {"type": "bar", "xOffset": 11, "size": 10, "color": "#1e88e5"},
"encoding": {
"x": {"field": "Month", "type": "ordinal", "axis": {"labelAngle": 0}},
"y": {"field": "Target", "type": "quantitative", "axis": null}
}
},
{
"name": "VOLUME",
"mark": {"type": "line"},
"encoding": {
"x": {"field": "Month", "type": "ordinal"},
"y": {"aggregate": "sum", "field": "Volume", "type": "quantitative"},
"stroke": {
"field": "Volume Legend",
"scale": {"range": ["green"]},
"legend": {"title": "",}
}
}
}
],
"resolve": {"scale": {"y": "independent"}}
}