Custom (re)ordering of legend/bar segments in Einstein Analytics

Problem:

Recently I tacked with the problem of ordering the legend/bar segments in Einstein Analytics and this is how I solved it.

Using the Einstein Analytics user interface, I could sort bar segments of a stacked column chart in asc/desc order or keep it unsorted. However, I needed to sort them in a custom fashion like in the image. 

Apparently, its possible by using custom SAQL and with final touches using the extended meta data a.k.a XMD.

Solution:

if you are familiar with Einstein Analytics, you know this one 😉

Then you need to add a similar code like the following into the SAQL code:

q = foreach q generate (case 
when 'Fruits__c' == "Berry" then "03 Berry" 
when 'Fruits__c' == "Apple" then "02 Apple" 
when 'Fruits__c' == "Orange" then "01 Orange" 
end) as 'Fruits__c';

After this step, the legend is going to show its contents with numbers in front. To fix this using the eXtended Meta Data (XMD) .json file.

Go ahead download the XMD.json file after clicking “edit” dataset. As seen in the screenshot below, click “download” to get the latest .json file.

you will see it, when you click ‘edit’ dataset

Now you need to edit the “dimensions” part like in the example code below. Define the member and label fields, if you want to override the color as well then also color too.

Next step is to replace the XMD .json file. Click the ‘replace‘ button and upload the updated file.

{"dataset":{},"dates":[],"derivedDimensions":[],"derivedMeasures":[],
    "dimensions" : [
  {  
    "field":"Fruits__c",
    "linkTemplateEnabled": false,
    "members":[  
      {  
        "color":"#EB271F",
        "label":"Orange",
        "member":"01 Orange"
      },
      {  
         "color":"#F7B92B",
         "label":"Apple",
         "member":"02 Apple"
      },
      {  
         "color":"#FD9226",
         "label":"Berry",
         "member":"03 Berry"
      }
    ],
    "recordDisplayFields": [],
    "salesforceActions": [],
    "salesforceActionsEnabled": false,
    "showInExplorer": true
  }
],"measures":[],"organizations":[],"showDetailsDefaultFields":[]}

Niceness! Now you have an ordered/coloured legend/bar segments in the stacked column chart and no more misleading/confusing numbers in front the legend elements.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.