Forum Discussion
Make a clustered bar chart with conditional formatted leged in Deneb.
- 10 months ago
Hi jaryszek,
You can assign a value to a color encoding by using the field operator in a scale's range after specifying the field to use for its domain, e.g.:
{ ... "encoding": { ... "color": { "field": "MeterCategory", "scale": { "range": {"field": "Color by Rank"} }, ... }, ... }, ... }This is also covered in the Vega-Lite documentation, with an example available here.
Full spec (based on yours above and corrected to work for this case). The visual data set consists of:
- Date (x encoding)
- Sum of CostInBillingCurrency (y encoding)
- MeterCategory (color encoding)
- Color by Rank (color range)
{ "data": { "name": "dataset" }, "mark": { "type": "bar", "tooltip": true }, "encoding": { "x": { "field": "Date", "type": "temporal", "timeUnit": "yearmonthdate", "axis": { "labelAngle": -40, "title": null } }, "y": { "field": "Sum of CostInBillingCurrency", "type": "quantitative", "axis": { "title": null } }, "color": { "field": "MeterCategory", "scale": { "range": {"field": "Color by Rank"} }, "legend": { "title": "Rank", "labelExpr": "datum.value === 'Others' ? 'Others' : 'Rank ' + datum.value", "orient": "right" } }, "order": { "field": "MeterCategory" } }, "config": { "view": { "stroke": null }, "axisY": { "grid": true }, "bar": { "size": 10 } } }I've also attached a copy of your workbook, along with this working visual.
Thanks for using Deneb!
Daniel
Thank you very much,
i tried with :
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"name": "dataset"},
"transform": [
{
"calculate": "(datum['Rank (Current Axis)'] == null || datum['Rank (Current Axis)'] > 5) ? 'Others' : toString(datum['Rank (Current Axis)'])",
"as": "RankBucket"
}
],
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"x": {
"field": "Date",
"type": "temporal",
"timeUnit": "yearmonthdate",
"axis": {"labelAngle": -40, "title": null}
},
"xOffset": {
"field": "MeterCategory",
"type": "nominal",
"title": null
},
"y": {
"field": "Sum of CostInBillingCurrency",
"type": "quantitative",
"aggregate": "sum",
"axis": {"title": null}
},
"color": {
"field": "RankBucket",
"type": "nominal",
"scale": {
"domain": ["1","2","3","4","5","Others"],
"range": ["#E81123","#107C10","#0078D4","#FFB900","#8E8CD8","#B3B3B3"]
},
"legend": {
"title": "Rank",
"labelExpr": "datum.value === 'Others' ? 'Others' : 'Rank ' + datum.value",
"orient": "right"
}
},
"order": {"field": "MeterCategory"}
},
"config": {
"view": {"stroke": null},
"axisY": {"grid": true},
"bar": {"size": 10}
}
}
but have no idea how to make this working.
Can i use dynamic color range from power bi measure instead of this ?:
"color": {
"field": "Rank",
"type": "nominal",
"scale": {
"domain": [1, 2, 3, 4, 5, "Others"],
"range": ["#E81123", "#107C10", "#0078D4", "#FFB900", "#8E8CD8", "#B3B3B3"]
},
Best wishes,
Jacek
Hi jaryszek,
You can assign a value to a color encoding by using the field operator in a scale's range after specifying the field to use for its domain, e.g.:
{
...
"encoding": {
...
"color": {
"field": "MeterCategory",
"scale": {
"range": {"field": "Color by Rank"}
},
...
},
...
},
...
}
This is also covered in the Vega-Lite documentation, with an example available here.
Full spec (based on yours above and corrected to work for this case). The visual data set consists of:
- Date (x encoding)
- Sum of CostInBillingCurrency (y encoding)
- MeterCategory (color encoding)
- Color by Rank (color range)
{
"data": {
"name": "dataset"
},
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"x": {
"field": "Date",
"type": "temporal",
"timeUnit": "yearmonthdate",
"axis": {
"labelAngle": -40,
"title": null
}
},
"y": {
"field": "Sum of CostInBillingCurrency",
"type": "quantitative",
"axis": {
"title": null
}
},
"color": {
"field": "MeterCategory",
"scale": {
"range": {"field": "Color by Rank"}
},
"legend": {
"title": "Rank",
"labelExpr": "datum.value === 'Others' ? 'Others' : 'Rank ' + datum.value",
"orient": "right"
}
},
"order": {
"field": "MeterCategory"
}
},
"config": {
"view": {
"stroke": null
},
"axisY": {
"grid": true
},
"bar": {
"size": 10
}
}
}
I've also attached a copy of your workbook, along with this working visual.
Thanks for using Deneb!
Daniel
- jaryszek10 months ago
Super User
Wow, you are the boss!!
Thank you so much, really appreciated.
Best,
Jacek