In the previous blogs in this series, we created a simple analytic application and added features such as popups and dynamic visibility. We also leveraged scripting, albeit on a very limited basis. Now let us explore scripting capabilities a bit more in detail.

The Need for Scripting

Scripting capabilities enable IT teams delivers a lot of custom capabilities to end users. While stories can deliver content with standardized interaction & navigation paths, analytic applications need to deliver customized user experiences, interactions, and navigation paths. This is primarily enabled through scripting.

For example, look at the visual below that outlines how scripting can be used to customize measures that are displayed on charts & tables. This can be accomplished only in applications, and not in stories.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

Stories vs. Applications – Deliver custom scenarios via. Scripting (Source: SAP)

The above is only the starting point for scripting. SAP Analytics Cloud will soon deliver advanced scripting capabilities to perform more complex operations such as inheriting widgets and accessing predictive frameworks, all of which can deliver custom experiences to users, something that cannot be accomplished by stories.

Basic Scripting

The scripting capabilities delivered by SAP Analytics Cloud is very similar to that of SAP Lumira Designer.

  • It leverages typescript, a subset of JavaScript, for coding
  • Content Assistance can be enabled using the keyboard combination Ctrl + Space which shows all the available commands (see image below)
  • Syntax check & commenting features are available
  • You can glance several scripts at once (an improvement from SAP Lumira Designer)

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

Here are some of the commonly used script functions by multiple widgets.

  • setVisible() – shows or hides a widget. A single Boolean parameter is used. For example, SetVisible(true); will set the visibility of table to true.
  • isVisible() – returns whether a widget is visible or not. The returned value is Boolean.
  • addDimension() – adds the specified dimension to the chart. It has three parameters dimension name, feed and position (optional). For example, addDimension(“State”, Feed.CategoryAxis); adds State values to the Category axis of the chart.
  • addMeasure() – adds the specified measure to the chart. The syntax is similar to that of addDimension(). Both addDimension() and addMeasure() functions are only available for chart widgets.
  • addDimensionToRows() – adds the specified dimension to the table row. It has two parameters – dimension name and position.
  • addDimensionToColumns() – adds the specified dimension to the table column. It has two parameters – dimension name and position.
  • removeDimension() & removeMeasure() – removes the specified dimension or measure from the chart.
  • getDataSource() – returns the data source used in the widget.
  • getSelections()– returns all the selections made in a chart or table.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

Commenting

To comment a single line of code we can include “//” at the beginning of the line. To comment multiple lines, we can enclose the code between “/*” and “*/”.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

Console Function

Console Function can be used to log a string in the console window of the browser’s developer tools. This will be useful when we try to debug the application.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

Control Statements

Control statements determine whether specific segments of code need to be executed. SAP Analytics Cloud supports the following control statements as part of analytic applications.

1. If Else Statement

The syntax for the if else statement is as follows:

if (condition) {

                Script to be executed if the condition is true

} else if (condition) {

                Script to be executed if the condition is true

} else {

                Script to be executed if all conditions fail

}

 

To have multiple conditions inside if statement we can use “||” for OR and “&&” for AND. See the example below that displays the appropriate chart based on the region.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

2. Loop Functions

Unlike SAP Lumira Designer which only supports For loop, analytic application of SAP Analytics Cloud supports For, While and Do While loops.

2.1. For Loop

For loop can be used to access and manipulate an array. In the following example, the list of members from a data source is added to a dropdown.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

2.2. While Loop

While loop can be used instead of a for loop in the example mentioned earlier.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

2.3. Do While Loop

The code snippet above can also be written using the do…while loop.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

2.4. Break and Continue Statements

In all the loop functions you can break the loop using a break statement and skip an element using continue statement. Here we have used the continue statement to skip any NULL values and break statement to come out of the loop when a member of the array has the string “_Closed”.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

3. Switch Function

The Switch statement can be used to run a specific block of code based on the condition. Here we are displaying specific charts based on appropriate region selected in the dropdown. In addition, there is also a ‘default’ behavior that executes when none of the conditions are met.

SAP Analytics Cloud – Application Design Series: 5 – Introduction to Scripting

* * *

In the subsequent blog, we will discuss string, math, date, array and conversion functions available in Analytic Applications.

Reach out to us here today if you are interested to evaluate if SAP Analytics Cloud is right for you.

Subscribe to our Newsletter

5920 Windhaven Pkwy
Plano TX 75093.

+1 888-227-2794

+1 972-232-2233

+1 888-227-7192

solutions@visualbi.com