In the Vega documentation for transforms, this is given as an example:
{
"data": [
{
"name": "table",
"transform": [
{ "type": "filter", "expr": "datum.value > 5" },
{ "type": "stack", "field": "value", "groupby": ["category"] }
]
}
]
}
Let two boolean signals be called is_filter
and is_stack
. Is it possible to conditionally execute transforms based on these signals? Something like this (which doesn't actually work):
{
"data": [
{
"name": "table",
"transform": [
{ "type": "filter", "expr": "datum.value > 5", "run_if": "is_filter"},
{ "type": "stack", "field": "value", "groupby": ["category"], "run_if": "is_stack"}
]
}
]
}
It would be good to avoid creating a dataset for each combination of transforms and using conditional logic in the marks.
Include all the logic in the expression. Changing the is_filter signal will change the filter here:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic bar chart example, with value labels shown upon pointer hover.",
"width": 400,
"height": 200,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"category": "A", "amount": 28},
{"category": "B", "amount": 55},
{"category": "C", "amount": 43},
{"category": "D", "amount": 91},
{"category": "E", "amount": 81},
{"category": "F", "amount": 53},
{"category": "G", "amount": 19},
{"category": "H", "amount": 87}
],
"transform": [
{"type": "filter", "expr": "is_filter?datum.amount > 40:datum.amount < 40"}
]
}
],
"signals": [
{
"name": "tooltip",
"value": {},
"on": [
{"events": "rect:pointerover", "update": "datum"},
{"events": "rect:pointerout", "update": "{}"}
]
},
{"name": "is_filter", "value": true}
],
"scales": [
{
"name": "xscale",
"type": "band",
"domain": {"data": "table", "field": "category"},
"range": "width",
"padding": 0.05,
"round": true
},
{
"name": "yscale",
"domain": {"data": "table", "field": "amount"},
"nice": true,
"range": "height"
}
],
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "left", "scale": "yscale"}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "xscale", "field": "category"},
"width": {"scale": "xscale", "band": 1},
"y": {"scale": "yscale", "field": "amount"},
"y2": {"scale": "yscale", "value": 0}
},
"update": {"fill": {"value": "steelblue"}},
"hover": {"fill": {"value": "red"}}
}
},
{
"type": "text",
"encode": {
"enter": {
"align": {"value": "center"},
"baseline": {"value": "bottom"},
"fill": {"value": "#333"}
},
"update": {
"x": {"scale": "xscale", "signal": "tooltip.category", "band": 0.5},
"y": {"scale": "yscale", "signal": "tooltip.amount", "offset": -2},
"text": {"signal": "tooltip.amount"},
"fillOpacity": [
{"test": "datum === tooltip", "value": 0},
{"value": 1}
]
}
}
}
]
}