Knowledge Base Article

Changing Measures In The Entire Dashboard Using Blox User Interface

Objective

Display several related values (KPIs/Measures) in multiple widgets in the dashboard based on the user selection, in a simple and easy way, without duplicating widgets/formulas or tables in the Elasticube. As a direct result - the queries behind the widgets are extremely faster - lead to shorter loading time when the end-user needs to change the whole dashboard KPIs.
 
Example files (works with Ecommerce Sample Elasticube):
Demonstration:

Preparation

  1. Create a new Indicator widget and save its WidgetId.
  2. Create a custom action with this code below and name it SwitchMeasure
var dimIndex = payload.data.selectVal - 1;

var dimToSwapTo = payload.widget.metadata.panels[1].items[dimIndex].jaql;

var widgetIds = payload.data.widgetToModify;

payload.widget.dashboard.widgets.$$widgets

   .filter(i=>widgetIds.includes(i.oid))

   .forEach(function(widget){

       if(widget.metadata.panels[1].$$widget.type == 'indicator')

       {

         widget.metadata.panels[0].items[0].jaql = dimToSwapTo;         

       }

       else

       {

         widget.metadata.panels[1].items[0].jaql = dimToSwapTo; 

       }

       widget.changesMade();

       widget.refresh();

       })

Implementation

1. ADDING MEASURES TO SWITCH BETWEEN

The formulas in the values panel define the measures your other widgets will switch between.
Create a new Blox widget and add all the formulas (Up to 20 Formulas) you want to switch between into the values panel in the selection of the button Blox widget. Please pay attention to the order (top value to bottom -> left to right buttons)

2. CUSTOMIZING THE BUTTONS SELECTION WIDGET

This step will define the values that are visible to the user in the button selection widget.
-Add choices that reflect the ORDER and intuitive name the formulas in the values panel.
-Add the widget IDs of the widgets you'd like to modify to the script of each option.
You can find the widget ids in the URL when in widget editing mode.
-Change the button color by editing the Background color for each option.
For example, we will be switching between Revenue, Cost, and Quantity, our Blox script would be:
{

   "style": "",

   "script": "",

   "title": "",

   "showCarousel": true,

   "titleStyle": [

       {

           "display": "none"

       }

   ],

   "carouselAnimation": {

       "delay": 0,

       "showButtons": false

   },

   "body": [

       {

           "type": "TextBlock",

           "id": "",

           "class": "",

           "text": "‎‎‏‏‎ ‎"

       }

   ],

   "actions": [

       {

           "type": "SwitchMeasure",

           "title": "Revenue",

           "style": {

               "backgroundColor": "#298C1A"

           },

           "data": {

               "widgetToModify": [

                   "5f1b462e336d9c242cb9e8ea",

                   "5f1b462e336d9c242cb9e8e5",

                   "5f1b462e336d9c242cb9e8e7",

                   "5f1b462e336d9c242cb9e8e9",

                   "5f1b462e336d9c242cb9e8e8"

               ],

               "selectVal": "1"

           }

       },

       {

           "type": "SwitchMeasure",

           "title": "Cost",

           "style": {

               "backgroundColor": "#A31818"

           },

           "data": {

               "widgetToModify": [

                   "5f1b462e336d9c242cb9e8ea",

                   "5f1b462e336d9c242cb9e8e5",

                   "5f1b462e336d9c242cb9e8e7",

                   "5f1b462e336d9c242cb9e8e9",

                   "5f1b462e336d9c242cb9e8e8"

               ],

               "selectVal": "2"

           }

       },

       {

           "type": "SwitchMeasure",

           "title": "Quantity",

           "style": {

               "backgroundColor": "#708AA5"

           },

           "data": {

               "widgetToModify": [

                   "5f1b462e336d9c242cb9e8ea",

                   "5f1b462e336d9c242cb9e8e5",

                   "5f1b462e336d9c242cb9e8e7",

                   "5f1b462e336d9c242cb9e8e9",

                   "5f1b462e336d9c242cb9e8e8"

               ],

               "selectVal": "3"

           }

       }

   ]

}

3. CUSTOMIZING THE BUTTON SELECTION ANIMATION

Add the script (widget script) below to the indicator you created in the preparation section:
  • Change the WidgetID to your indicator Widget ID
  • Change the formula names based on the value list from the buttons selection widget
  • Change the button’s titles based on the titles you selected the buttons selection widget
//Widget Script:

var ChooseYourUnselectColor = '#D3D3D3';

var Button1Color = '#298C1A';

var Button2Color = '#A31818'; 

var Button3Color = '#708AA5'; 




var widgetIndicator = '5f1b462e336d9c242cb9e8ea'




widget.on('ready',function(widget, args){

    

var widgetOID = widgetIndicator;

    //Get the selected KPI object

    var widget = prism.activeDashboard.widgets.$$widgets.find(w => w.oid === widgetOID)

if(widget.metadata.panels[0].items[0].jaql.title == 'Total Revenue'){

                var textOfButtonToFormat1 = 'Cost'; 

var textOfButtonToFormat2 = 'Quantity'; 

var selectedButton = 'Revenue'

   $('button.btn:contains('+textOfButtonToFormat1+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+textOfButtonToFormat2+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+selectedButton+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": Button1Color 

   });

            }

else if (widget.metadata.panels[0].items[0].jaql.title == 'Total Cost')

{

var textOfButtonToFormat1 = 'Revenue'; 

var textOfButtonToFormat2 = 'Quantity';

var selectedButton = 'Cost'

   $('button.btn:contains('+textOfButtonToFormat1+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+textOfButtonToFormat2+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+selectedButton+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": Button2Color 

   });

}

    else  

{

var textOfButtonToFormat1 = 'Revenue'; 

var textOfButtonToFormat2 = 'Cost';

var selectedButton = 'Quantity'

   $('button.btn:contains('+textOfButtonToFormat1+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+textOfButtonToFormat2+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": ChooseYourUnselectColor 

   });

$('button.btn:contains('+selectedButton+')').css({

   transition : 'background-color 50ms ease-in-out', "background-color": Button3Color 

   });

}

  





})
Updated 09-12-2024

9 Comments

  • Astroraf's avatar
    Astroraf
    Data Pipeline

    I tried to replicate the result using the E-Commerce data and all I am getting is this, with no option to click the other buttons intapiuser, do you know this is because this solution provided does not work with the latest update in Sisense?  

  • Liliia_DevX's avatar
    Liliia_DevX
    Sisense Employee

    Astroraf hi!

    Please try to use the following code in the Action "SwitchMeasure" and let me know if that woks. 

    var dimIndex = payload.data.selectVal - 1;
    var dimToSwapTo = payload.widget.metadata.panels[1].items[dimIndex].jaql;
    
    var widgetIds = payload.data.widgetToModify;
    
    
    payload.widget.dashboard.widgets.$$widgets
        .filter(i => widgetIds.includes(i.oid))
        .forEach(function (widget) {
            if (widget.metadata.panels[1].$$widget.type == 'indicator') {
                widget.metadata.panels[0].items[0].jaql = dimToSwapTo;
            }
            else {
                widget.metadata.panels[1].items[0].jaql = dimToSwapTo;
            }
    
            widget.changesMade('plugin-BloX', 'metadata');
            widget.refresh();
        })

    It worked for me on the L2024.2 version. 

    Best, 

    Lily

  • Astroraf's avatar
    Astroraf
    Data Pipeline

    Liliia_DevX 🙌🙌🙌🙌🙌🙌

    Thanks for being awesome, it works like a charm!

    But 

    the buttons do not go grey or the designated color 'ChooseUnselectorColor' when a button is pressed. It should work from what I can read from the code but it's not doing what is designated to do so. 

  • Liliia_DevX's avatar
    Liliia_DevX
    Sisense Employee

    Astroraf please check the following:

    1. The script is applied to the Indicator and "var widgetIndicator = '5f1b462e336d9c242cb9e8ea'" is updated accordingly with the widget id.

    2. If your values in the Blox widget are titled as "Revenue", "Cost" and "Quantity", please substitute it in the script accordingly. Please refer to the following lines:

    if(widget.metadata.panels[0].items[0].jaql.title == 'Total Revenue'){
    else if (widget.metadata.panels[0].items[0].jaql.title == 'Total Cost')

     3. Make sure the order of values in the Blox widget is exactly the same as you defined in Blox buttons: 

    Please let me know if that helps!

    Best, 

    Lily

  • @Liliia_DevX  intapiuser 

    I've been trying to modify this to work on a line chart (widget.type == 'lineChart' in the SwitchMeasure action) -- I don't want an indicator widget on the dashboard -- and I just can't get it to work without including an indicator widget.  The line chart changes between 3 values perfectly without the indicator widget type but the buttons won't show the designated selected color when I click on them.  The only button that shows the selected color is the default 3rd value, the other 2 just stay as the Unselected color.  Anyone have some ideas?  My javascript is beginner level if that.  Thanks for any help!

  • Hi NancySummit,

    Thank you for reaching out. Can you submit your question in the Help and How-To forum? More people will see it there.

    Link to this post or paste your code in the new thread so people can see the code.

    Thank you.