Skip to content

Instantly share code, notes, and snippets.

@PBI-DataVizzle
Created August 24, 2024 22:53
Show Gist options
  • Save PBI-DataVizzle/f280001bd4a4c4ee049a78ea3110a299 to your computer and use it in GitHub Desktop.
Save PBI-DataVizzle/f280001bd4a4c4ee049a78ea3110a299 to your computer and use it in GitHub Desktop.
dynamic field sort
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"a": "A", "b": 28},
{"a": "B", "b": 55},
{"a": "C", "b": 43},
{"a": "D", "b": 91},
{"a": "E", "b": 81},
{"a": "F", "b": 53},
{"a": "G", "b": 19},
{"a": "H", "b": 87},
{"a": "I", "b": 52}
]
},
"params": [
{
"name": "sortChoice",
"value": "x",
"bind": {"input": "select", "options": ["x", "y", "-x", "-y"]}
},
{
"name": "sortBy",
"expr": "sortChoice==='x'|| sortChoice==='-x' ? 'a':'b'"
},
{
"name": "sortOrder",
"expr": "sortChoice==='x'|| sortChoice==='y' ? 'ascending':'descending'"
}
],
"transform": [
{
"sort": [
{"field": {"signal": "sortBy"}, "order": {"signal": "sortOrder"}}
],
"window": [{"op": "rank", "as": "sorted"}]
}
],
"mark": "bar",
"encoding": {
"x": {"field": "a", "sort": {"field": "sorted"}},
"y": {"field": "b", "type": "quantitative"}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment