BDenman24
09-05-2024Cloud Apps
Conditional Text in BloX Widget
Hello,
I'm still pretty new at BloX widgets, so hopefully this is a simple correction. I am trying to make a BloX widget have conditional text if a value is below another value, display text, else display another text. I am able to get the background color to change based upon that criteria, but not the text. Below is my code:
{
"style": "",
"script": "",
"title": "",
"showCarousel": true,
"carouselAnimation": {
"delay": 0
},
"conditions": [
{
"minRange": "-Infinity",
"maxRange": "{panel:DIR Company Avg}",
"backgroundColor": "#fd6e69"
},
{
"minRange": "{panel:DIR Company Avg}",
"maxRange": "Infinity",
"backgroundColor": "#3adcca"
}
],
"body": [
{
"spacing": "none",
"type": "Container",
"items": [
{
"spacing": "small",
"type": "TextBlock",
"class": "condition_data",
"text": "Your DIR score is {panel: DIR Score}",
"horizontalAlignment": "center",
"size": "large",
"weight": "bold",
"color": "white"
}
]
}
],
"conditionsTarget": [
{
"data-class": "condition_data",
"conditions": [
{
"minRange": "-Infinity",
"maxRange": "{panel:DIR Company Avg}",
"type": "TextBlock",
"text": "below avg"
},
{
"minRange": "{panel:DIR Company Avg}",
"maxRange": "Infinity",
"type": "TextBlock",
"text": "above avg"
}
]
}
]
}
Any help would be greatly appreciated! Thanks!
Hi BDenman24 ,
One solution is as follows:
Create a measure titled 'Is Above' that returns 1 if the value is "Above Avg" and 0 if it is "Below Avg." Your formula may look something like this:
case when sum([DIR Score]) > AVG([DIR Company Avg]) then 1 else 0 end
Update the BloX script with the following:
{ "style": "", "script": "", "title": "", "showCarousel": true, "carouselAnimation": { "delay": 0 }, "conditions": [ { "minRange": "-Infinity", "maxRange": "{panel:DIR Company Avg}", "backgroundColor": "#fd6e69" }, { "minRange": "{panel:DIR Company Avg}", "maxRange": "Infinity", "backgroundColor": "#3adcca" } ], "body": [ { "spacing": "none", "type": "Container", "items": [ { "spacing": "small", "type": "TextBlock", "class": "condition_data", "text": "Your DIR score is {panel: DIR Score}", "horizontalAlignment": "center", "size": "small", "weight": "bold", "color": "white" }, { "spacing": "small", "type": "TextBlock", "text": "{panel: Is Above}", "horizontalAlignment": "center", "size": "small", "weight": "bold", "color": "white" } ] } ] }ā
Add the widget script to the BloX widget:
widget.on('processresult', function(w, args){ args.result.forEach(item => { Object.keys(item).forEach(el => { if(item[el].Panel === 'Is Above') { if(item[el].Value === 0) item[el].Text = 'Below Avg' else item[el].Text = 'Above Avg' } }) }) })ā
Here's the result:
Please let me know if this works for you.
Thanks,
Hari
ā