Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Created October 27, 2025 18:45
Show Gist options
  • Select an option

  • Save cbaragao/0638c622399dd6830889c4efd3c11cfa to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/0638c622399dd6830889c4efd3c11cfa to your computer and use it in GitHub Desktop.
//SPEC
{
"data": {
"name": "dataset"
},
"title": "Sales Variance by Product",
"transform": [
{
//"calculate": "datum['Total Sales'] *.80","as": "target"
"calculate": "17000000",
"as": "target"
},
{
"calculate": "datum['Total Sales'] - datum['target']",
"as": "variance"
},
{
"calculate": "datum['Total Sales'] - datum['target']",
"as": "variance"
},
{
"calculate": "datum['variance'] > 0 ? datum['variance'] : 0",
"as": "labelX"
},
{
"calculate": "5",
"as": "dx"
},
{
"calculate": "'left'",
"as": "align"
},
{
"calculate": "datum['variance'] > 0 ? '✅':'🔻'", "as":"indicator"
},
{
"calculate": "format(datum['variance'],'.2s') + ' ' + datum.indicator",
"as": "labelText"
}
],
"encoding": {
"y": {
"field": "Product",
"type": "nominal",
"sort": "-x",
"axis": {
"title": null,
"domain": false,
"ticks": false
}
},
"x": {
"field": "variance",
"type": "quantitative",
"axis": {
"grid": false,
"ticks": false,
"labels": false,
"domain": false,
"title": null
}
}
},
"layer": [
{
"mark": "bar",
"encoding": {
"color": {
"condition": {
"test": "datum['Total Sales'] > datum['target']",
"value": "#009DDC"
},
"value": "#D8D2e1"
}
}
},
{
"mark": {
"type": "text",
"align": {
"expr": "datum.align"
},
"baseline": "middle",
"dx": {
"expr": "datum.dx"
}
},
"encoding": {
"x": {
"field": "labelX",
"type": "quantitative"
},
"y": {
"field": "Product",
"type": "nominal"
},
"text": {
"field": "labelText",
"type": "nominal"
}
}
}
]
}
//CONFIG
{
"view": {
"stroke": "transparent"
},
"tick": {
"thickness":3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment